How to unduck AVAudioSession

最后都变了- 提交于 2019-12-08 09:48:23

问题


kAudioSessionProperty_OtherMixableAudioShouldDuck is used For audio session categories that allow mixing of iPod audio with application audio, specifies whether iPod audio should be reduced in level when your application produces sound. This property has a value of FALSE (0) by default. Set it to a nonzero value to turn on ducking. How can we unduck this? Is there any property?

Thanks in advance, Chandra.


回答1:


I had this same question and had trouble getting it to work consistently. I spent a lot of time researching and debugging this and finally just called Apple. They told me to look at the breadcrumb sample code. I followed that example and everything worked fine.

The issue here was that there are many different options for session properties and timing, etc. E.g. do you set the property and then unset it or leave it and then start and stop the session?

Here is a similar question:

Audio Session "Ducking" Broken in iOS 4...?

Here is Apple's sample code:

http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html




回答2:


Looking at the example code posted by @zaphodtx, one possible solution is to activate and deactivate the current audio session.

The specific file in the example is: https://developer.apple.com/library/ios/samplecode/Breadcrumb/Listings/Breadcrumb_BreadcrumbViewController_m.html#//apple_ref/doc/uid/DTS40010048-Breadcrumb_BreadcrumbViewController_m-DontLinkElementID_6

For example, in 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)
    }
}


来源:https://stackoverflow.com/questions/6489630/how-to-unduck-avaudiosession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!