MPMoviePlayerController play error on iPad - iphone

I'm writing an app that will be playing videos alot using the MPMoviePlayerController on the iPad. The problem is the app was working fine and playing the videos when I stopped working about 15 hours ago but now the videos do not play. The MPMoviePlayerController will show the first frame from the video, and in full screen view I can scrub through the movie fine but when I hit play it just pauses right away. I have the code below, when debugging i noticed that when I call play it sends an MPMoviePlayerPlaybackStateDidChangeNotification with the playbackState being MPMoviePlaybackStatePlaying and then right away it sends another MPMoviePlayerPlaybackStateDidChangeNotification notification with playbackState being MPMoviePlaybackStatePaused. Not sure if that helps but please let me know if you see anything wrong in my code or have an ideas, thanks.
- (void)handleNotification:(NSNotification *)notification {
if ([[notification name] isEqualToString:MPMoviePlayerPlaybackStateDidChangeNotification]) {
if (_videoPlayer.playbackState == MPMoviePlaybackStatePlaying) {
_playButtonLarge.hidden = YES;
_scrubber.maximumValue = _videoPlayer.duration;
[_playPauseButton setBackgroundImage:[UIImage imageNamed:#"video_controls_pause.png"] forState:UIControlStateNormal];
if (_updateScrubberTimer == nil) {
_updateScrubberTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateScrubber) userInfo:nil repeats:YES];
}
} else if (_videoPlayer.playbackState == MPMoviePlaybackStatePaused || _videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
[_playPauseButton setBackgroundImage:[UIImage imageNamed:#"video_controls_play.png"] forState:UIControlStateNormal];
_playButtonLarge.hidden = NO;
if (_updateScrubberTimer != nil) {
[_updateScrubberTimer invalidate];
_updateScrubberTimer = nil;
}
if (_videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
_scrubber.value = 0.0f;
_timePlayedLabel.text = #"0:00";
_timeRemainingLabel.text = #"-0:00";
_videoPlayerBG.hidden = NO;
}
}
} else if ([[notification name] isEqualToString:MPMoviePlayerPlaybackDidFinishNotification]) {
NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([reason intValue] == MPMovieFinishReasonPlaybackEnded) {
_videoPlayerBG.hidden = NO;
}
_scrubber.value = _scrubber.maximumValue;
}
}
- (void)playPause {
if ([_videos count] > 0) {
if (_videoPlayer.playbackState == MPMoviePlaybackStatePaused || _videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
_playButtonLarge.hidden = YES;
_videoPlayerBG.hidden = YES;
if ([_videoPlayer contentURL] == nil) {
Video *video = [_videos objectAtIndex:0];
[_videoPlayer setContentURL:video.videoURL];
}
if (![_videoPlayer isPreparedToPlay]) {
[_videoPlayer prepareToPlay];
}
[_videoPlayer play];
} else if (_videoPlayer.playbackState == MPMoviePlaybackStatePlaying) {
_playButtonLarge.hidden = NO;
[_videoPlayer pause];
}
}
}

I think I figured it out, I put the following code before every call to play
if (![_videoPlayer isPreparedToPlay]) {
[_videoPlayer prepareToPlay];
}
works now, if anyone else has any input let me know

Related

video playing from application background to foreground takes 2 seconds lagging from pause to play in iOS 6.0

I have VideoController which plays video. Now VideoController is pushed in navigationController.
VideoController *ObjVideoController = [[VideoController alloc] init];
ObjVideoController.strVideoURL = [AnimationArr objectAtIndex:SequenceCount];
[self.navigationController pushViewController:ObjVideoController animated:NO];
[ObjVideoController play:[AnimationArr objectAtIndex:SequenceCount]];
Play method in VideoController is like this:
- (void)play:(NSString*)videoFile {
playbaktime = 0.0;
NSBundle *main = [NSBundle mainBundle];
NSURL *url = [NSURL fileURLWithPath:[[main resourcePath] stringByAppendingPathComponent:videoFile]];
if (!self.ctl)
{
self.ctl = nil;
self.ctl = [[MPMoviePlayerController alloc]init];
[self.view addSubview:self.ctl.view];
}
[self.ctl prepareToPlay];
self.ctl.contentURL = url;
self.ctl.controlStyle = 0;
//self.ctl.useApplicationAudioSession = NO;
[self.ctl.view setUserInteractionEnabled:NO];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[self.ctl.view setFrame:CGRectMake(0,0,480,320)];
else
[self.ctl.view setFrame:CGRectMake(0,0,1024,768)];
self.ctl.backgroundView.backgroundColor = [UIColor whiteColor];
[self.ctl play];
}
Now observer has been add for UIApplicationWillResignActive, UIApplicationDidBecomeActive. Its selector are given below:
-(void)Pause_Video:(NSNotification*)notification {
Pausevideo = 1;
playbaktime = self.ctl.currentPlaybackTime;
[self.ctl pause];
}
-(void)Play_Video:(NSNotification*)notification {
if(self.ctl.loadState == MPMovieLoadStateUnknown)
{
[self play:self.strVideoURL];
Pausevideo = 0;
}
else{
if (self.ctl.playbackState == MPMoviePlaybackStatePaused) {
[self.ctl play];
Pausevideo = 0;
}
else
{
[self.ctl setInitialPlaybackTime:playbaktime];
[self.ctl play];
Pausevideo = 0;
}
}
}
Hope u understood question and help will be appreciated.
You seem to be calling prepareToPlay every time before playing, this is not necessary, just call it once when you load the file. There is no need to call it after you pause.
Additionally, you are incurring a lag because notifications do not fire instantly. Specifically, UIApplicationDidBecomeActive only fires well after the app has entered the foreground. What you want instead is to observe the UIApplicationWillEnterForegroundNotification, which will fire as the app is entering the foreground, and allow you to decrease the lag significantly.

How to check live streaming url response iphone

In my app I am doing live streaming with the following code :
-(void)playurl:(id)sender
{
self.mpPlayer=[[[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:#"http://alwatan.lsops.net/live/alwradio_ar_audio.sdp/playlist.m3u8"]] autorelease];
[self.mpPlayer prepareToPlay];
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.mpPlayer.view.frame = CGRectMake(0, 50, 768, 400);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.mpPlayer.view.frame = CGRectMake(0, 0, 1024, 500);
}
[self.mpPlayer play];
self.mpPlayer.movieSourceType = MPMovieSourceTypeStreaming;
//self.mpPlayer.movieSourceType = MPMovieSourceTypeFile;
self.mpPlayer.shouldAutoplay = TRUE;
self.mpPlayer.controlStyle = MPMovieControlStyleDefault;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mpPlayer];
[scrollView addSubview:self.mpPlayer.view];
if(isVideoPlay)
{
isVideoPlay = FALSE;
}
else
{
isVideoPlay = TRUE;
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
NSError *error = [[notification userInfo] objectForKey:#"error"];
if (error) {
}
else
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
}
}
Everything is fine but my problem is that is url is not runing then application will be crashed so how can I check the live streaming url response so I can mange the condition and give alert for url not respond.
Thanks,
Use the following notification
MPMoviePlayerPlaybackStateDidChangeNotification
and check for
 if(moviePlayer.state == MPMoviePlaybackStateStopped)
{
// Alert
}
else  if(moviePlayer.state == MPMoviePlaybackStatePlaying)
{
// Alert
}
else  if(moviePlayer.state == MPMoviePlaybackStatePaused)
{
// Alert
}
else  if(moviePlayer.state == MPMoviePlaybackStateInterrupted)
{
// Alert
}
else  if(moviePlayer.state == MPMoviePlaybackStateSeekingForward)
{
// Alert
}
else  if(moviePlayer.state == MPMoviePlaybackStateSeekingBackward)
{
// Alert
}
My experience with MPMoviePlayerControler has been that it's not very good with error handling. One possibility would be to request the URL first, then only load the .m3u8 file when your stream is running.
You could use NSURLConnection and friends to try and load the stream, and, if necessary, read the file looking for something specific that indicates the stream is running (perhaps the presence of a line containing #EXTINF:.

iPhone: first song not pause before start next song

In My app Song play through buffering,
I have two music player in my app,
But it plays both musicplayer songs together. It does not pause the first song even when I try to pause.
i refer this tutorial:
http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html
for that i try to call
[streamer pause];
- (void)pause
{
#synchronized(self)
{
if (state == AS_PLAYING)
{
err = AudioQueuePause(audioQueue);
if (err)
{
[self failWithErrorCode:AS_AUDIO_QUEUE_PAUSE_FAILED];
return;
}
self.state = AS_PAUSED;
}
else if (state == AS_PAUSED)
{
err = AudioQueueStart(audioQueue, NULL);
if (err)
{
[self failWithErrorCode:AS_AUDIO_QUEUE_START_FAILED];
return;
}
self.state = AS_PLAYING;
}
}
}
streamer calls the pause method of buffering.
How can i do that?
if (audioPlayer.playing==YES) {
[audioPlayer pause];
}
try this.

i have a problem in UIImageView's animating

-(IBAction) change {
self.imageView.animationImages = myImages;
self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}
hello, i'm studying iOS programming.
but i have a question.
i have a button and when i click the button, then this method will be called.
first i click the button, then this code will start the if statement. that's what i want.
i click the button again, i think that will execute the else statement.
but it always execute the if statement only.
why is that?
i really don't know why is that. please help me
I think setting the properties like animationImages or animationDuration will stop the animation, so that by clicking, you every time stop and then just after (re)start it in the if part. Try setting these two properties outside the action method you wrote, and just let the if/else sequence.
-(IBAction) change {
// set these two anywhere else
//self.imageView.animationImages = myImages;
//self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}

switch.hidden = YES not working, outlets all correctly set up

I am having a problem that is totally confounding me. Please look at the code below, it is from the book "Beginning iPhone 4 Development" chapter 4. I'm new to this :)
- (IBAction)toggleControls:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
NSLog(#"Show switches");
[self.leftSwitch setHidden:NO];
[self.rightSwitch setHidden:NO];
[self.doSomethingButton setHidden:YES];
}
else
{
NSLog(#"Hide switches");
[self.leftSwitch setHidden:YES];
[self.rightSwitch setHidden:YES];
[self.doSomethingButton setHidden:NO];
}
}
The strange thing is that it logs this correctly but the ui controls aren't hiding/showing.
I also tried this (original in book):
- (IBAction)toggleControls:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
NSLog(#"Show switches");
leftSwitch.hidden = NO;
rightSwitch.hidden = NO;
doSomethingButton.hidden = YES;
}
else
{
NSLog(#"Hide switches");
leftSwitch.hidden = YES;
rightSwitch.hidden = YES;
doSomethingButton.hidden = NO;
}
}
It sounds like you may have forgotten to wire up your outlets in Interface Builder. Check the values of leftSwitch and rightSwitch when this method is called by using a break point or an NSLog.