【问题标题】:How do I call mpmovieplayer from applicationwillresignactive in ppdelegate?如何从 appdelegate 中的 applicationwillresignactive 调用 mpmovieplayer?
【发布时间】:2012-04-01 05:26:00
【问题描述】:

我正在使用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 的播放器?

【问题讨论】:

  • 有人吗?我还是想不通!! :(

标签: audio-streaming mpmovieplayer audiosession


【解决方案1】:

尝试导入 MPMoviePlayerController 所在的类。在 AppDelegate.h "import "YourClassNameWithThePlayer.h"" 中使用此代码。 也许这会有所帮助。我也是这方面的新手,希望你考虑一下。

【讨论】:

  • 谢谢你,我做了更多关于这个的阅读,我想我已经弄清楚了我需要做什么......我仍在探索 ios xcode......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 2014-11-20
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多