AVPlayer无法从音乐库播放 [英] AVPlayer not playing from music library

查看:203
本文介绍了AVPlayer无法从音乐库播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AVPlayer播放我的iPhone音乐库中的一首歌。一切似乎都准备好了,但玩家根本不会发出任何声音。我一直在努力解决这个问题,任何帮助都将不胜感激!

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!

注意:我意识到我可以使用AVAudioPlayer,但我想直接从我的音乐库中读取文件,据我所知,AVAudioPlayer不支持(我必须先输出这首歌,占用更多时间)。我无法使用MPMusicPlayerController,因为最终目标是将歌曲转换为NSData并在另一台设备上播放。

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.

最重要的是,我想知道为什么这段代码不是' t play:

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");
}];

输出为Ready to play,AVPlayer的rate属性为1.0我称之为play方法。 MPMediaItem存在,我可以使用valueForProperty方法获取正确的标题,艺术家等。任何想法为什么没有声音来自播放器?

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. 我把AVPlayer变成了一个属性(感谢小费meggar!)

  2. 在使用initWithAsset方法之前,我确保AVPlayer为零。

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

}];


希望这可以帮助别人!

这篇关于AVPlayer无法从音乐库播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆