如果在后台模式下播放时手机响铃

发布于 2025-01-08 10:05:21 字数 1848 浏览 2 评论 0原文

我正在制作一个 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];

I'm making an iphone app(Live radio app).

My app is supported background mode.

But, if the phone rings when radio app is playing in background mode, my app stop with errors .

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

So, I modified my code, but useless.

I'll add my code. Please tell me my faults. Thanks.

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2025-01-15 10:05:21

你需要添加这个:
[[UIApplication共享应用程序]开始接收远程控制事件];

you need to add this:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文