在iOS上开始在后台播放声音? [英] Start playing sound in background on iOS?

查看:1459
本文介绍了在iOS上开始在后台播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行与 Tile.app 类似的操作。当它显示通知时,它会播放声音。这似乎很简单 - 使用 UILocalNotification 并包含声音文件名。但是本地通知将声音限制在不超过30秒,并且Tile的声音持续播放的时间比这长得多。我希望它会循环播放,直到我再也无法忍受噪音了。

I'm trying to do something similar to Tile.app. When it shows a notification, it plays a sound. That seems simple enough-- use UILocalNotification and include the sound file name. But local notifications limit sounds to no more than 30 seconds, and Tile's sound keeps playing for a whole lot longer than that. I expect it's looping, and it continues until I can't stand the noise any more.

即使手机的静音开关打开,声音也会播放,发生本地通知。由于这些原因, UILocalNotification 似乎已经出局。

The sound also plays even if the phone's mute switch is on, which doesn't happen with local notifications. For these reasons, UILocalNotification appears to be out.

我想也许我可以发布纯文字的本地通知让我的应用播放声音。但是使用 AVAudioPlayer play 方法返回 NO 并且声音无法播放。

I thought maybe I could post a text-only local notification and have my app play the sound. But using AVAudioPlayer, the play method returns NO and the sound doesn't play.

其他问题表明这是设计的,应用程序不应该能够从后台播放声音 - 只能继续播放已经播放的声音。我接受这个推理,除了Tile这样做,所以它显然是可能的。一个建议的解决方法违反了Apple的指南在某种程度上,我很确定Apple现在会检查。

Other questions have suggested that this is by design, that apps aren't supposed to be able to play sounds from the background-- only continue sound that's already playing. I'd accept that reasoning except that Tile does it, so it's obviously possible. One suggested workaround violates Apple's guidelines in a way that I'm pretty sure Apple checks for now.

可能相关的一些细节:


  • 我在Info.plist中有音频后台模式

  • 我使用音频会话上的AVAudioSessionCategoryPlayback ,会话应该是活动的(至少 setActive:错误:声称成功)。

  • 此应用使用蓝牙,但目前不使用蓝牙中央后台模式。

  • I have the audio background mode in Info.plist
  • I use AVAudioSessionCategoryPlayback on the audio session, and the session should be active (at least, setActive:error: claims to succeed).
  • This app uses Bluetooth but does not currently use the bluetooth-central background mode.

推荐答案

实际上你可以使用 AudioServices ... 函数之一

Actually you can use one of AudioServices... functions


  • AudioServicesPlaySystemSound

  • AudioServicesPlayAlertSound

  • AudioServicesPlaySystemSoundWithCompletion

  • AudioServicesPlaySystemSound
  • AudioServicesPlayAlertSound
  • AudioServicesPlaySystemSoundWithCompletion

开始在后台播放声音。

to start playing sound in background.

我不确定可以提交给 AudioServicesCreateSystemSoundID 的声音长度,因为我只检查了响铃的短循环声音,但此API与通知无关,因此可能超过30秒限制。

I'm not sure about length of sound that can be submitted to AudioServicesCreateSystemSoundID because I'm checked only with short looped sound for ringing but this API not tied to notification so possible can overpass 30 second limit.

编辑:
App仍然在 UIBackgroundModes 中需要 audio 才能在后台播放

App still requires audio in UIBackgroundModes to play in background

剪切来自我的测试项目appDelegate,iOS 9.3.1

Cut from my test project appDelegate, iOS 9.3.1

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // test sound in foreground.
    registerAndPlaySound()

    return true
}

func applicationDidEnterBackground(application: UIApplication) {
    var task =  UIBackgroundTaskInvalid
    task = application.beginBackgroundTaskWithName("time for music") {
        application.endBackgroundTask(task)
    }
    // dispatch not required, it's just to simulate "deeper" background  
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC)))

    dispatch_after(delayTime, dispatch_get_main_queue()) {
        self.registerAndPlaySound()
    }
}

func registerAndPlaySound() {
    var soundId: SystemSoundID = 0

    let bundle = NSBundle.mainBundle()
    guard let soundUrl = bundle.URLForResource("InboundCallRing", withExtension: "wav") else {
        return
    }
    AudioServicesCreateSystemSoundID(soundUrl, &soundId)

    AudioServicesPlayAlertSound(soundId)
}

更新

在那里我使用 beginBackgroundTask 来启动声音,这只是在另一个项目中执行代码的最简单方法,类似代码 registerAndPlaySound 是从没有后台任务的 PushKit 回调中调用的。

There I used beginBackgroundTask to start sound only as simplest way to execute code in background in another project similar code to registerAndPlaySound was called from PushKit callback without background task.

这篇关于在iOS上开始在后台播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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