所以我想用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
}
这是我的播放列表tableview的截图
我从MPMediaCollection获得的是mpmediaitem字典和歌曲信息(标题,艺术家,网址)
点击uitableview
行时如何播放歌曲?
答案 0 :(得分:1)
所以,是的,我自己已经解决了这个问题。它真的很简单
我做的第一件事是:
将 MPMediaItemcollection 声明为全局变量(放入 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()
}
就像那样
答案 1 :(得分:0)
我建议使用AVAudioPlayer播放这首歌。
声明一个这样的属性:
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
像这样初始化您的播放器:
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
}
播放歌曲使用:
[audioPlayer play];