ios4 - 如何取消 AVAudioSession

标签 ios4 avaudioplayer avaudiosession

kAudioSessionProperty_OtherMixableAudioShouldDuck 用于允许将 iPod 音频与应用程序音频混合的 Audio Session 类别,指定当应用程序产生声音时是否应降低 iPod 音频的级别。默认情况下,此属性的值为 FALSE (0)。将其设置为非零值以打开闪避。 我们怎样才能解决这个问题呢?有属性(property)吗?

提前致谢, 钱德拉。

最佳答案

查看@zaphodtx 发布的示例代码,一种可能的解决方案是激活和停用当前 Audio Session 。

示例中的具体文件为: https://developer.apple.com/library/ios/samplecode/Breadcrumb/Listings/Breadcrumb_BreadcrumbViewController_m.html#//apple_ref/doc/uid/DTS40010048-Breadcrumb_BreadcrumbViewController_m-DontLinkElementID_6

例如,在 Swift 中:

import UIKit
import AVFoundation


class MyController: UIViewController, AVAudioPlayerDelegate{

    var audioPlayer: AVAudioPlayer?

    func viewDidLoad(){
        super.viewDidLoad()
        configureAudioSession()
    }

    func configureAudioSession(){
        let session = AVAudioSession.sharedInstance()
        do{
            try session.setCategory(AVAudioSessionCategoryPlayback, withOptions: [.DuckOthers])
        } catch {
            print(
                "Unable to configure audio session, Attempting " +
                "to activate or deactivate the audio session will "
                "likely not meet your expectations."
            )
            return
        }
        print("Audio Session Configured")
    }

    func activateAudioSession(value: Bool){
        let session = AVAudioSession.sharedInstance()
        try? session.setActive(value)
    }

    func playAudioAtPath(path:String){
        audioPlayer?.stop()
        let url = NSURL(fileURLWithPath: path)

        if let player = try? AVAudioPlayer(contentsOfURL: url){
            activateAudioSession(true)
            print("Playing AVAudioPlayer Sound from path: '\(path)'")
            player.volume = volume
            player.delegate = self
            player.play()
            audioPlayer = player
        } else {
            print("Failed to play AVAudioPlayer Sound from path: '\(path)'")
            audioPlayer = nil
        }
    }

    func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
        activateAudioSession(false)
    }
}

关于ios4 - 如何取消 AVAudioSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6489630/

相关文章:

iOS 更改辅助功能焦点。

javascript - 摇动ios设备时加载随机图像

objective-c - 推送通知角标(Badge)计数未更新

ios - 使用 MPMoviePlayer 从广播流获取元数据? (iOS)

swift - 从 swift 3 迁移到 swift 4.2 时,AVAudioSession.mode 导致 "Editor placeholder in source file"错误

iOS - 开始录音时,蓝牙设备认为我正在打电话

iphone - 如何通过Get方法从Web服务中获取数据并想保存到iPhone SDK中的Mutable Dictionary中

ios - 使用类作为静态 AVAudioPlayer 的 AVAudioPlayerDelegate

ios - 使用 AmbientSound session 的 AVAudioPlayer 在应用程序处于后台时不播放

audio - 使用xcode 5(iOS7sdk)构建非iOS7 UI