Question

I have a music player application for iOS6 and above. My music player works fine, it can play music online (streaming) and offline (local mp3 files).

The problem comes when I connect to my device (iPad or iPhone) a bluetooth headset, if the device is unlocked it works fine but when the device is locked the music continues until the song finishes and then when it tries to play next song it is not played. After some debugging I realize that the problem is just with local files and it seems that the file is not "playable", but it really is because with the device unlocked it works fine.

My code:

    NSURL *URL = [[NSURL alloc] initFileURLWithPath:path];
    AVAsset *asset = [AVURLAsset URLAssetWithURL:URL options:nil];
    if(asset.isPlayable) {
        AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
        _player = [AVPlayer playerWithPlayerItem:anItem];
        if(![self isPlaying]) {
            [self play];
        }
    }

The code inside the "if(asset.isPlayable) {" is never executed.

UPDATE:

I have changed my code with @MDB983 advice but it doesn't work anyway.

        NSURL *URL = [[NSURL alloc] initFileURLWithPath:path];
        AVPlayerItem *anItem = [AVPlayerItem playerItemWithURL:URL];
        if(_player == nil) {
            _player = [AVPlayer playerWithPlayerItem:anItem];
        } else {
            [_player replaceCurrentItemWithPlayerItem:anItem];
        }
        if(![self isPlaying]) {
            [self play];
        }

Any ideas?

Was it helpful?

Solution 2

I have finally solved my problem.

The point wasn't with the AVPlayer, it was the file storage. Before storing my files locally I encrypt the file and then for playing it I decrypt it.

The problem was that I was saving the files like this:

BOOL success = [encryptedResult writeToFile:path options:NSDataWritingFileProtectionComplete error:&error];

So I couldn't read the files properly when the device is locked, as it says in Apple's documentation.

I've changed everywhere to:

BOOL success = [encryptedResult writeToFile:path options:NSDataWritingFileProtectionNone error:&error];

And it works like a charm.

OTHER TIPS

Are you setting 'MPNowPlayingInfoCenter's nowPlayingInfo' ?

I found the same problem when playing a streaming file via AVPlayer. As soon as the file finished, the next file never played while the lock screen was being displayed. The solution was to populate the MPNowPlayingInfoCenter's nowPlayingInfo; try adding something like this to set it before playing the new track;

   UIImage *artworkImage = [UIImage imageWithName:@"artworkImage.jpg"];
   MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc]initWithImage:artworkImage];
   [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"My Song Title", MPMediaItemPropertyTitle,
                                                            @"Artist Name", MPMediaItemPropertyArtist,
                                                            artwork, MPMediaItemPropertyArtwork, 
                                                            1.0f, MPNowPlayingInfoPropertyPlaybackRate, nil];

One other issue I encountered, The AVPlayer must be instantiated prior to the lock screen being displayed, so you should modify your approach and create the AVPlayer as a property and set a new AVPlayerItem on each song change as follows;

     AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
     [self.player replaceCurrentItemWithPlayerItem:anItem];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top