MPMoviePlayerViewController stops after a few seconds
I have an app in which I am streaming a live TV channel in one of tabs. I am using MPMoviePlayerViewController
. I did declare my MPMoviePlayerViewController
in my header file and synthesize it in my implementation file.
Here's my viewDidAppear:
- (void)viewDidAppear:(BOOL)animated
{
NSURL *movieURL = [[NSURL alloc]initWithString:@"http://mysuperURL"];
moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self checkIt];
}
And my checkIt function
- (void) checkIt {
if ([[moviePlayerController moviePlayer] loadState] == MPMovieLoadStateUnknown) { // before you wreck yourself
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkIt) userInfo:nil repeats:NO];
} else {
[moviePlayerController setModalTransitionStyle:UIModalTransi开发者_JAVA百科tionStyleCrossDissolve];
[self presentModalViewController:moviePlayerController animated:YES];
}
}
However the video freezes after two seconds and the app stops responding.
You should use the MPMoviePlayerNotifications instead of manually polling the current state.
For example - somewhere in you initializing code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(MPMoviePlayerLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
Now implement a notification handler:
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
{
NSLog(@"loadstate change: %Xh", movieController_.loadState);
}
And somewhere within your deinitializing code:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
Also note that MPMoviePlayerController.loadState is a bitmap -> you need to mask out the value you want to check for.
For Example:
if ((movieController_.loadState & MPMovieLoadStatePlayable) == MPMovieLoadStatePlayable)
{
NSLog(@"yay, it became playable");
}
Asfar as my knowledge concern usage of timer it is freezing and it takes time streaming also.
精彩评论