【问题标题】:How to play selected Media Item from UITableView in iOS?如何在 iOS 中从 UITableView 播放选定的媒体项?
【发布时间】:2013-01-01 10:49:10
【问题描述】:

我正在尝试在 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 中选择的歌曲

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

【问题讨论】:

  • 没人回答我的问题吗?
  • 请在 indexpath 方法中发布你的 cell for row,你需要在 indexpath 方法中实现 didselect 行
  • 是的,我也想知道代码应该写在 didselectedrow 方法中。如何播放选定的歌曲?

标签: ios cocoa-touch mpmediaitem


【解决方案1】:

你需要像下面的代码那样实现你的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];

}

【讨论】:

    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2015-07-27
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多