ios - 如何在 iOS 中从 UITableView 播放选定的媒体项目?

标签 ios cocoa-touch mpmediaitem

我正在尝试播放从 iOS 中的 UITableView 中选择的 Media Item

我正在使用 MediaQuery 并从我的 UITableView 中的 iPod 库加载歌曲列表。

但我不知道如何播放从 UITableView 中选择的歌曲。

我知道非常基本的简单 AudioPlayer,代码如下。

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"sample" 
                                                              ofType: @"mp3"];
    NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: soundFilePath] autorelease];
    self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error: nil] 
                   autorelease];

这是使用已位于资源中的 ContentOfURL。无需从 iPod 库中检索歌曲。

这是我的 CellForRowAtIndexPath 方法代码

MPMediaItemCollection *songs = [self.arrayOfSongs objectAtIndex:indexPath.row];

    cell.textLabel.text = [songs.items[0] valueForProperty:MPMediaItemPropertyTitle];

现在我正在尝试播放使用 MediaQuery 从 iPod 库中检索到的歌曲以及从 UITableView

中选择的歌曲

那么我如何检索 URL 并播放从 UITableView 中选择的 iPod 库中的歌曲? 感谢您的阅读。

最佳答案

您需要像下面的代码一样实现您的didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   MPMediaItemCollection *songs = [self.arrayOfSongs objectAtIndex:indexPath.row];
   MPMediaItem *item = [songs representativeItem];
   self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:[item valueForProperty:MPMediaItemPropertyAssetURL] error: nil] 
                   autorelease];

}

关于ios - 如何在 iOS 中从 UITableView 播放选定的媒体项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14109779/

相关文章:

objective-c - Objective C - 将 mpmediaitem 转换为 ogg vorbis

iphone - 保存查询值

ios - swift中的延迟函数

ios - Soundcloud iOS API - 从链接播放声音

ios - 当我们尝试使用 Appium 脚本导航到 iPhone safari 浏览器中的框架时,获取异常阻止了具有原点的框架

objective-c - 从 NSString 对象中提取字符

iphone - 如何复制 Mail.app 的垃圾动画

objective-c - NSNotification 发送一次,但被多次接收

ios - 如何在 iOS 中获取 iPod 库中歌曲的文件位置

ios - 如何获取 MPMediaItem(歌曲)收听时间(日期)