Я использую mpmovieplayer
для воспроизведения аудиопотока.У меня проблемы с обработкой вторжений, например, при получении вызова.Мой игрок объявлен в viewcontroller
, и я считаю, что мне нужно что-то сделать в applicationdidresignactive
в моем appdelegate
правильно?Как мне это сделать, если мой appdelegate
не знает о моем moviePlayer
?Я новичок в разработке для iPhone, поэтому я учусь по ходу дела и наслаждаюсь им:)
Вот что я делаю в viewcontroller
-(IBAction)play1MButton:(id)sender{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerStatus:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
NSString *url = @"http://22.22.22.22:8000/listen.pls";
[self.activityIndicator startAnimating];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL URLWithString:url]];
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
-(void) moviePlayerStatus:(NSNotification*)notification {
//MPMoviePlayerController *moviePlayer = notification.object;
MPMoviePlaybackState playbackState = moviePlayer.playbackState;
if(playbackState == MPMoviePlaybackStateStopped) {
NSLog(@"MPMoviePlaybackStateStopped");
} else if(playbackState == MPMoviePlaybackStatePlaying) {
NSLog(@"MPMoviePlaybackStatePlaying");
} else if(playbackState == MPMoviePlaybackStatePaused) {
NSLog(@"MPMoviePlaybackStatePaused");
} else if(playbackState == MPMoviePlaybackStateInterrupted) {
NSLog(@"MPMoviePlaybackStateInterrupted");
} else if(playbackState == MPMoviePlaybackStateSeekingForward) {
NSLog(@"MPMoviePlaybackStateSeekingForward");
} else if(playbackState == MPMoviePlaybackStateSeekingBackward) {
NSLog(@"MPMoviePlaybackStateSeekingBackward");
}
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
if ([moviePlayer loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
[moviePlayer play];
[self.activityIndicator stopAnimating];
[moviePlayer setFullscreen:NO animated:NO];
}
}
и в appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) {
}
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) {
}
Я могу уловить ошибки, но как мне использовать плеер из appdelegate?