我正在尝试播放从 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/