开发者

iPhone - MPMoviePlayerController identify when stopped playing and when started

开发者 https://www.devze.com 2023-03-28 04:04 出处:网络
In my app, i\'m using MPMoviePlayerController to play a streaming video from my server. I\'m hiding 开发者_StackOverflow中文版the controls and have my own button to indicate whether the player is play

In my app, i'm using MPMoviePlayerController to play a streaming video from my server. I'm hiding 开发者_StackOverflow中文版the controls and have my own button to indicate whether the player is playing, paused or stopped. I want to enable/disable the buttons depending on the state of streaming.

I'm using following code to initiate the player:

self.avPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:audioURL]];
[self.avPlayer.moviePlayer setShouldAutoplay:YES];
[self.avPlayer.moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlayStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];       

When the play starts, i'm getting the notification. But if due to some reason, wifi is slow or not available, player might be streaming and I'm not getting any notification. Why am I not getting notification when the video is streaming?

Also when the video is streaming in middle, the player stops and throws "finished" notification. How to prevent it from stopping during streaming.....


Before anything else, I think you should specify the movieSourceType property:

self.avPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

Now, about the notifications,

  • First, why not check the playbackState property before and after to see why the notification was called, when and if it gets called.
  • Maybe even an more reliable indicator, since these appear to be connection problems, is to handle the MPMoviePlayerLoadStateDidChangeNotification and check the loadState property (like the notification for PlaybackStateChanged there is no userinfo dictionary so you have to read it through the loadState property).
  • Finally, for the MPMoviePlayerPlaybackDidFinishNotification getting thrown during streaming I'm guessing there was some error, since other options would be the movie reached the end or the user stopped the movie himself. Again, you can check to see why the notification was called by reading the MPMovieFinishReason constant that gets passed as a key in the userinfo dictionary parameter of the notification.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号