我正在使用mpmovieplayer
播放音频流。我在处理入侵时遇到了麻烦,例如接到电话时。我的玩家在viewcontroller
中声明,我认为我需要在appdelegate
中的applicationdidresignactive
中做一些事情,对吗?如果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中的播放器?
尝试导入MPMoviePlayerController所在的类。在AppDelegate.h"import "YourClassNameWithThePlayer.h"
"中使用此代码。也许这会有所帮助。我也是新手,希望你考虑一下。