无法在iOS中播放与iPodMusicPlayer捆绑的歌曲

时间:2013-02-09 13:24:21

标签: ios cocoa-touch mpmusicplayercontroller mpmediaitemcollection

我正在尝试使用iPodMusicPlayer在我的应用中播放包(文档目录)中的歌曲。

我不想玩AVPlayer

以下是我的一些代码:

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
        NSFileManager *fm = [NSFileManager defaultManager];
        NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
        NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp3'"];
        NSArray *onlyMP3 = [dirContents filteredArrayUsingPredicate:fltr];
        self.app.arrayFromAppDelegate = [onlyMP3 mutableCopy];

        NSLog(@"%i",self.app.arrayFromAppDelegate.count);

        self.userMediaItemCollection = [[MPMediaItemCollection alloc] initWithItems:self.app.arrayFromAppDelegate];
        NSLog(@"Collection count is : %i",self.userMediaItemCollection.count);

        [self.player setQueueWithItemCollection:self.userMediaItemCollection];
        [self.player setNowPlayingItem:(MPMediaItem *)[self.app.arrayFromAppDelegate objectAtIndex:0]];

[self.player play];

首先,我从文档目录中检索所有mp3并将它们添加到NSMutableArray中。

然后我将它们添加到MPMediaItemCollection和setQueue中进行播放。 然后我玩。

但是当我玩的时候,它只显示MediaPlayer: Message nowPlayingItem timed out in iOS

当我使用上述代码播放iPod库中的歌曲时,我没事。

但我无法播放App Bundle中的歌曲。

NSLog方法还会显示文档目录中正确的歌曲数量。

但我不能玩。

我该怎么办?

1 个答案:

答案 0 :(得分:2)

使用MPMusicPlayerController,您无法使用文档目录中的mp3文件。 MPMusicPlayerController仅与设备iPod库中的项目兼容。