我们如何处理使iOS设备在运行时/关闭背景音乐的能力? [英] How can we handle enable/disable background audio abilities at runtime on iOS devices?

查看:576
本文介绍了我们如何处理使iOS设备在运行时/关闭背景音乐的能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何设置我的iOS应用程序,使其在播放背景音乐使用的plist(=当其他应用程序使用的iOS设备上)。

I know how to set up my iOS application to make it playing audio in background (= when another application is used on the iOS device) using the plist.

某些应用程序,如:
- 布卢姆
- 一些无线电球员

Some applications like: - BLOOM - some radio players

提供了一个基本UISwitch来启用或禁用此行为。

provides a basic UISwitch to enable or disable this behavior.

有关如何做到这一点任何想法?

Any ideas about how to do that ?

推荐答案

有在处理音频播放code主类类型UIBackgroundTaskIdentifier的伊娃,与beginBackgroundTaskWithExpirationHandler ....启动音频播放器的方法之前,这个初始化并使用endBackgroundTask音频播放器何时完成。

Have an iVar of type UIBackgroundTaskIdentifier in the main class that handles audio playing code, initialize this with beginBackgroundTaskWithExpirationHandler.... method before starting the audio player and use endBackgroundTask when audio player completes.

code:

@inerface AudioPlayerController : NSObject
{
    UIBackgroundTaskIdentifier bgTaskID;
}
@end

@implementation AudioPlayerController

- (void) startPlayer
{

    bgTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

   // Write code to start the audio player
}

// Call this when your program receives message of audio playing complete

- (void) audioPlayComplete
{

    // Do the audio playing finish

    if (bgTaskID != UIBackgroundTaskInvalid)
      [[UIApplication sharedApplication] endBackgroundTask:bgTaskID];
} 

@end

这篇关于我们如何处理使iOS设备在运行时/关闭背景音乐的能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆