3

我正在使用 AVPlayer 播放位于文档目录中的 MP3(文件已确认在其中) - 我加载AVPlayerItem等待AVPlayerItemStatusReadyToPlay然后用该项目实例化AVPlayer并播放。

确实被调用了AVPlayerItemStatusReadyToPlay,但实际上没有播放音频,有人知道为什么吗?

- (void)checkFileExists {
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.urlForEightBarAudioFile path]]) {
        [self beginEightBarDownload];
    } else {
        // set up audio
        [self setupAudio];

        //NSLog(@"file %@ already exists", [self.urlForEightBarAudioFile path]);
    }
}


- (void)setupAudio
{
    AVAsset *eightBarAsset = [AVAsset assetWithURL:self.urlForEightBarAudioFile];
    self.eightBarsPlayerItem = [[AVPlayerItem alloc] initWithAsset:eightBarAsset];
    [self.eightBarsPlayerItem addObserver:self forKeyPath:@"status" options:0 context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([object isKindOfClass:[AVPlayerItem class]])
    {
        AVPlayerItem *item = (AVPlayerItem *)object;

        if ([keyPath isEqualToString:@"status"])
        {
            switch(item.status)
            {
                case AVPlayerItemStatusFailed:
                    break;
                case AVPlayerItemStatusReadyToPlay:
                    self.player = [[AVPlayer alloc] initWithPlayerItem:self.eightBarsPlayerItem];
                    [self.player play];
                    NSLog(@"player item status is ready to play");
                    break;
                case AVPlayerItemStatusUnknown:
                    NSLog(@"player item status is unknown");
                    break;
            }
        }
        else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
        {
            if (item.playbackBufferEmpty)
            {
                NSLog(@"player item playback buffer is empty");
            }
        }
    }
}
4

3 回答 3

3

做这个:

- (void)setupAudio
{
 if([NSFileManager defaultManager] fileExistsAtPath:[self.urlForEightBarAudioFile absoluteString]])
 {
   AVAsset *eightBarAsset = [AVAsset assetWithURL:self.urlForEightBarAudioFile];
   self.eightBarsPlayerItem = [[AVPlayerItem alloc] initWithAsset:eightBarAsset];
   self.eightBarsPlayer = [AVPlayer playerWithPlayerItem:self.eightBarsPlayerItem]; //forgot this line
   [self.eightBarsPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; // add observer for player not item
 }
}

参考avplayer-and-local-files链接

于 2012-08-28T08:24:09.227 回答
0

迅速

func setupAudio() {
 if NSFileManager.defaultManager().fileExistsAtPath(self.urlForEightBarAudioFile.absoluteString) {
   self.eightBarsPlayerItem = AVPlayerItem(asset: AVAsset(URL: self.urlForEightBarAudioFile))
   self.eightBarsPlayer = AVPlayer(playerItem: self.eightBarsPlayerItem)
 }
}
于 2015-11-24T10:03:29.173 回答
0
- (void)setupAudio
{
  dispatch_async(dispatch_get_main_queue(), ^{ 
  AVAsset *eightBarAsset = [AVAsset   assetWithURL:self.urlForEightBarAudioFile];
  self.eightBarsPlayerItem = [[AVPlayerItem alloc] initWithAsset:eightBarAsset];
  self.eightBarsPlayer = [AVPlayer  playerWithPlayerItem:self.eightBarsPlayerItem]; //forgot this line
  [self.eightBarsPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; // add observer for player not item
});

}

于 2016-11-13T15:13:13.317 回答