AVPlayer not playing from music library

删除回忆录丶 提交于 2020-01-01 05:29:11

问题


I am trying to play a song from my iPhone music library using AVPlayer. Everything seems ready to play, but the player simply won't make any sound. I've been struggling with this for a while, any help would be greatly appreciated!

Note: I realize I could use AVAudioPlayer, but I would like to read the file right from my music library and, to my understanding, AVAudioPlayer doesn't support that (I would have to export the song first, taking up more time). I cannot use MPMusicPlayerController because the end goal is to turn the song into NSData and play it on another device.

Above all, I would like to know WHY this code isn't playing:

NSArray *itemsFromQuery = [[MPMediaQuery songsQuery] items];
MPMediaItem *song = [itemsFromQuery objectAtIndex:29];
NSURL *songURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL:songURL options:nil];

NSArray *keyArray = [[NSArray alloc] initWithObjects:@"tracks", nil];

[urlAsset loadValuesAsynchronouslyForKeys:keyArray completionHandler:^{

    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:urlAsset];

    AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

    while (true) {
        if (player.status == AVPlayerStatusReadyToPlay && playerItem.status == AVPlayerItemStatusReadyToPlay) {
            break;
        }
    }

    if (player.status == AVPlayerStatusReadyToPlay && playerItem.status == AVPlayerItemStatusReadyToPlay) {
        NSLog(@"Ready to play");
        [player play];
    }
    else 
        NSLog(@"Not ready to play");
}];

The output is "Ready to play", and the "rate" property of the AVPlayer is 1.0 after I call the play method. The MPMediaItem exists, and I can use the valueForProperty method to obtain the correct title, artist, etc. Any ideas why no sound is coming from the player?


回答1:


Found something that worked:

  1. I made the AVPlayer a property (thanks for the tip meggar!)
  2. I made sure the AVPlayer was nil before using the initWithAsset method.

    NSArray *itemsFromQuery = [[MPMediaQuery songsQuery] items];
    MPMediaItem *song = [itemsFromQuery objectAtIndex:0];
    NSURL *songURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL:songURL options:nil];
    
    NSArray *keyArray = [[NSArray alloc] initWithObjects:@"tracks", nil];
    
    [urlAsset loadValuesAsynchronouslyForKeys:keyArray completionHandler:^{
    
        AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:urlAsset];
    
        player = nil;
        player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    
        while (true) {
            if (player.status == AVPlayerStatusReadyToPlay && playerItem.status == AVPlayerItemStatusReadyToPlay)
                break;
        }
    
        [player play];
    
    }];
    

Hope this helps someone out!




回答2:


Another reason is configuring AVAudioSession. Worked for me.

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  [[AVAudioSession sharedInstance] setActive: YES error: nil];


来源:https://stackoverflow.com/questions/11103490/avplayer-not-playing-from-music-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!