4

I'm using AVPlayer to play a MP3 located in the documents directory (file is confirmed to be in there) - I load the AVPlayerItem wait for the AVPlayerItemStatusReadyToPlay then instantiate the AVPlayer with the item and play.

The AVPlayerItemStatusReadyToPlay does get called, but no audio actually plays, anyone have an idea why?

- (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");
            }
        }
    }
}
Wasim
  • 4,953
  • 10
  • 52
  • 87

3 Answers3

3

Do this:

- (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
 }
}

Refer avplayer-and-local-files link

Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • Thanks, but I already check for file existence, i've edited my post to show that. – Wasim Aug 28 '12 at 08:27
  • 1
    Ahh right, so if you playing from local source then you have to observe the player, but if you're playing from remote source then you observe the player item? Am i right in thinking that? your answer worked perfectly, thanks. – Wasim Aug 28 '12 at 08:40
0

in swift

func setupAudio() {
 if NSFileManager.defaultManager().fileExistsAtPath(self.urlForEightBarAudioFile.absoluteString) {
   self.eightBarsPlayerItem = AVPlayerItem(asset: AVAsset(URL: self.urlForEightBarAudioFile))
   self.eightBarsPlayer = AVPlayer(playerItem: self.eightBarsPlayerItem)
 }
}
R00We
  • 1,931
  • 18
  • 21
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
});

}

Ping
  • 145
  • 2
  • 6