1

I am trying to implement AVPlayer for play audio file from URL. It is working fine. But sometimes AVPlayer stops/pauses playing and do not resume again. If I click on play button then it works fine. (it should play without user interface). (I do not want use : [player addObserver:self forKeyPath:@"rate" options:0 context:nil];) I also take help from this URL : AVPlayer stops playing and doesn't resume again. But this code has some errors. For temporary I am using this code and looks fine from my side. But I am not sure whether it is fine or not in all cases.So I want to know what should I do which handles all cases. Thanks in advance...... My code :

-(void)updateTime:(NSTimer *)timer {

// temporary  code for play audio again, when player pause/stop automatically
if (player.rate==0) {
    [player play];
}

currentTime = CMTimeGetSeconds(playerItem.currentTime);
duration = CMTimeGetSeconds(playerItem.duration);
[self.audioSliderBar setValue:(currentTime/duration)];
float minutes = floor(currentTime/60);
seconds =currentTime - (minutes * 60);
float duration_minutes = floor(duration/60);
duration_seconds =
duration - (duration_minutes * 60);
NSString *timeInfoString = [[NSString alloc]
                            initWithFormat:@"%0.0f:%0.0f",
                            minutes, seconds ];
self.audioCurrentTimeLabel.text = timeInfoString;

}

Community
  • 1
  • 1
Ravi
  • 800
  • 2
  • 12
  • 28
  • The AVPlayer doesn't stop playing without a reason. Do you have any idea why it stops? Are you streaming the URL? Could it be a network problem? If you can figure out why it is stopping, you will be able to fix the underlying problem rather than throwing in code that covers up the real problem. –  Sep 01 '15 at 12:15
  • @ Michael L , I am playing audio from URL. I don't know exactly why it stops. How can I know what is exactly problem by which player stops. Sometimes it works fine on same network and sometimes does not work fine(stops automatically during playing). Thanks in advance !!!!!!! – Ravi Sep 01 '15 at 12:28
  • 1
    Try checking the status of the player periodically - if you ever see AVPlayerStatusFailed you may be able to see the reason. Otherwise what I would do is download the file first and then play it. I would not rely on the network - I am pretty sure that is your problem. –  Sep 01 '15 at 12:32
  • @ Michael L , I did as you said, it helped me little bit, but now facing another problem, that is when once a time player stopped/paused : 'if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"]) { if (playerItem.playbackBufferEmpty) { [player pause]; }' then it takes too much time to play : 'if (playerItem.playbackLikelyToKeepUp) { printf("\n\n\t****Ready to Play audio ****\n\n"); [player play]; }' Please let me solution for this issue. Thank in advance !!!! – Ravi Sep 01 '15 at 17:16
  • Sorry, too little information. If you are having new problems, consider asking a new question. –  Sep 01 '15 at 17:48
  • @ Michael L, Sure for complete in details have a look please http://stackoverflow.com/questions/32337699/objective-c-avplayer-taking-too-much-time-to-resume-play-audio-from-url – Ravi Sep 01 '15 at 17:57
  • 1
    Possible duplicate of [AVPlayer stops playing and dont resume again](http://stackoverflow.com/questions/19291636/avplayer-stops-playing-and-dont-resume-again) – schmittsfn May 19 '16 at 07:54

0 Answers0