【问题标题】:If the phone rings while playing in background mode如果在后台播放时手机响铃
【发布时间】:2012-03-13 22:07:41
【问题描述】:

我正在制作一个 iphone 应用(直播应用)。

我的应用支持后台模式。

但是,如果收音机应用程序在后台模式下播放时手机响起,我的应用程序会因错误而停止。

MP AVAudioSessionDelegateMediaPlayerOnly end interruption. Interruptor <Phone> category <completed> resumable <0>,  _state = 6
MP endInterruptionFromInterruptor :: resuming playback 

所以,我修改了我的代码,但没用。

我将添加我的代码。请告诉我我的过错。谢谢。

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioSessionDelegate>

@property (assign, nonatomic) UIBackgroundTaskIdentifier bgTask;  
... ... 

@end

AppDelegate.m

- (void)applicationDidEnterBackground:(UIApplication *)application
{
      bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you.
        // stopped or ending the task outright.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task, preferably in chunks.

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
} 

- (void)applicationDidBecomeActive:(UIApplication *)application
{ 
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{        
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid; 
    }];     
}

viewController.m

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];  
moviePlayer = [[MPMoviePlayerController alloc] init];
[moviePlayer setContentURL:... m3u8];
[moviePlayer play];

【问题讨论】:

    标签: ios ios5 background mpmovieplayercontroller multitasking


    【解决方案1】:

    你需要添加这个:
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2015-02-09
      相关资源
      最近更新 更多