ioscocoa-touchmpmusicplayercontrollermpmediaitemcollection

Can't play songs from bundle with iPodMusicPlayer in iOS


I am trying to play songs from bundle(document directory) in my app with iPodMusicPlayer.

I don't want to play with AVPlayer.

Here is some of my code:

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];

First I retrieve all mp3s from the documents directory and add them into an NSMutableArray.

And then I add them into an MPMediaItemCollection and setQueue to play. Then I play.

But when I play, it's only showing MediaPlayer: Message nowPlayingItem timed out in iOS.

I am okay when I play songs from the iPod Library with the above code.

But I can't play songs from the App Bundle.

NSLog method also shows the correct count of songs from the documents directory.

But I can't play.

How can I?


Solution

  • With MPMusicPlayerController, you can't use mp3 files from the documents directory. MPMusicPlayerController is only compatible with items from the device iPod library.