如何在 MPMusicplayerController 上播放歌曲 URL?
How to play song URL on MPMusicplayerController?
所以我想用 url 使用 MPMusicPlayer
播放歌曲。我已经能够填充从 MPMediaCollection
到 UITableView
的音乐信息。我想要做的是,当我点击 tableview
行时,它会播放 tableview
行中具有相同标题的歌曲。
这是我如何填充音乐信息的代码
func mediaPicker(mediaPicker: MPMediaPickerController!,
didPickMediaItems mediaItemCollection: MPMediaItemCollection!){
for thisItem in mediaItemCollection!.items as! [MPMediaItem]{
var mediaitem = mediaItemCollection!.items
playlistItem = ["music" : mediaitem]
playlistItemArray.addObject(playlistItem!)
let itemUrl = thisItem.valueForProperty(MPMediaItemPropertyAssetURL)
as? NSURL
let itemTitle =
thisItem.valueForProperty(MPMediaItemPropertyTitle)
as? String
let itemArtist =
thisItem.valueForProperty(MPMediaItemPropertyArtist)
as? String
var itemArtwork: AnyObject! = thisItem.valueForProperty(MPMediaItemPropertyArtwork)
if (itemArtwork != nil) {
var listArtwork = itemArtwork.imageWithSize(CGSizeMake (198, 198))
var listArtworkData = UIImagePNGRepresentation(listArtwork)
playlistArtwork = ["picture" : listArtworkData]
playlistArtworkImage.addObject(playlistArtwork!)
}else{
var listNulArtwork = UIImage(named: "noArtworkImage.png")
var listNulArtworkData = UIImagePNGRepresentation(listNulArtwork)
playlistArtwork = ["picture" : listNulArtworkData]
playlistArtworkImage.addObject(playlistArtwork!)
}
playlist = ["title" : itemTitle! , "artist" : itemArtist! , "songUrl" : itemUrl!]
playlistArray.addObject(playlist!)
}
}
这是代码,我如何将数据填充到 tableview
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("playlistCell", forIndexPath: indexPath) as! UITableViewCell
let object = User.playList[indexPath.row] as! NSDictionary
let artworkObject = User.playlistArt[indexPath.row] as! NSDictionary
let base64String = artworkObject["picture"]!.base64EncodedStringWithOptions(.allZeros)
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
var decodedimage = UIImage(data: decodedData!)
(cell.contentView.viewWithTag(1) as! UIImageView).image = decodedimage
//(cell.contentView.viewWithTag(1) as! UIImageView).image = image.timelinecircleMask
(cell.contentView.viewWithTag(2) as! UILabel).text = object["title"] as? String
(cell.contentView.viewWithTag(3) as! UILabel).text = object["artist"] as? String
return cell
}
这是我的播放列表表格视图的屏幕截图
我从 MPMediaCollection 得到的是 mpmediaitem 和歌曲信息的字典(标题,艺术家,url)
点击uitableview
行时如何播放歌曲?
我建议使用AVAudioPlayer来播放歌曲。
像这样声明一个属性:
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
像这样初始化您的播放器:
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
}
要播放歌曲,请使用:
[audioPlayer play];
所以,是的,我已经自己解决了这个问题。其实很简单
我做的第一件事是:
declare MPMediaItemcollection as a global variable(put in a struct)
然后:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var mediaItemCollection = User.mdci
MPMusicPlayerController().nowPlayingItem = mediaItemCollection!.items[indexPath.row] as! MPMediaItem
MPMusicPlayerController().play()
getPlayingSongInfo()
}
就这样
所以我想用 url 使用 MPMusicPlayer
播放歌曲。我已经能够填充从 MPMediaCollection
到 UITableView
的音乐信息。我想要做的是,当我点击 tableview
行时,它会播放 tableview
行中具有相同标题的歌曲。
这是我如何填充音乐信息的代码
func mediaPicker(mediaPicker: MPMediaPickerController!,
didPickMediaItems mediaItemCollection: MPMediaItemCollection!){
for thisItem in mediaItemCollection!.items as! [MPMediaItem]{
var mediaitem = mediaItemCollection!.items
playlistItem = ["music" : mediaitem]
playlistItemArray.addObject(playlistItem!)
let itemUrl = thisItem.valueForProperty(MPMediaItemPropertyAssetURL)
as? NSURL
let itemTitle =
thisItem.valueForProperty(MPMediaItemPropertyTitle)
as? String
let itemArtist =
thisItem.valueForProperty(MPMediaItemPropertyArtist)
as? String
var itemArtwork: AnyObject! = thisItem.valueForProperty(MPMediaItemPropertyArtwork)
if (itemArtwork != nil) {
var listArtwork = itemArtwork.imageWithSize(CGSizeMake (198, 198))
var listArtworkData = UIImagePNGRepresentation(listArtwork)
playlistArtwork = ["picture" : listArtworkData]
playlistArtworkImage.addObject(playlistArtwork!)
}else{
var listNulArtwork = UIImage(named: "noArtworkImage.png")
var listNulArtworkData = UIImagePNGRepresentation(listNulArtwork)
playlistArtwork = ["picture" : listNulArtworkData]
playlistArtworkImage.addObject(playlistArtwork!)
}
playlist = ["title" : itemTitle! , "artist" : itemArtist! , "songUrl" : itemUrl!]
playlistArray.addObject(playlist!)
}
}
这是代码,我如何将数据填充到 tableview
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("playlistCell", forIndexPath: indexPath) as! UITableViewCell
let object = User.playList[indexPath.row] as! NSDictionary
let artworkObject = User.playlistArt[indexPath.row] as! NSDictionary
let base64String = artworkObject["picture"]!.base64EncodedStringWithOptions(.allZeros)
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
var decodedimage = UIImage(data: decodedData!)
(cell.contentView.viewWithTag(1) as! UIImageView).image = decodedimage
//(cell.contentView.viewWithTag(1) as! UIImageView).image = image.timelinecircleMask
(cell.contentView.viewWithTag(2) as! UILabel).text = object["title"] as? String
(cell.contentView.viewWithTag(3) as! UILabel).text = object["artist"] as? String
return cell
}
这是我的播放列表表格视图的屏幕截图
我从 MPMediaCollection 得到的是 mpmediaitem 和歌曲信息的字典(标题,艺术家,url)
点击uitableview
行时如何播放歌曲?
我建议使用AVAudioPlayer来播放歌曲。
像这样声明一个属性:
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
像这样初始化您的播放器:
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
}
要播放歌曲,请使用:
[audioPlayer play];
所以,是的,我已经自己解决了这个问题。其实很简单
我做的第一件事是:
declare MPMediaItemcollection as a global variable(put in a struct)
然后:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var mediaItemCollection = User.mdci
MPMusicPlayerController().nowPlayingItem = mediaItemCollection!.items[indexPath.row] as! MPMediaItem
MPMusicPlayerController().play()
getPlayingSongInfo()
}
就这样