允许用户在swift 2.0中播放背景音乐 [英] Allow users background music in swift 2.0

查看:236
本文介绍了允许用户在swift 2.0中播放背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些代码,允许用户在使用我的应用程序时通过手机播放音乐。以前在swift 2.0之前,我会把它放在app委托中,它会完美运行:

I am looking for some code to allow the user to play music from their phone while still using my app. Previously before swift 2.0 i would put this in the app delegate and it would work perfectly:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)

有没有人知道如何在swift 2.0中实现它?

Does anyone know how to implement this in swift 2.0?

推荐答案

以下是Swift 2调用的语法 setCategory setActive on AVSession

The following would be the syntax for Swift 2 calling setCategory and setActive on AVSession:

do
{
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    try AVAudioSession.sharedInstance().setActive(true)
}
catch let error as NSError
{
    print(error)
}

OR

do
{
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    try AVAudioSession.sharedInstance().setActive(true)
}
catch let error as NSError
{
    print(error)
}

这篇关于允许用户在swift 2.0中播放背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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