开发者

How to unduck AVAudioSession

kAudioSessionProperty_OtherMixableAudioShouldDuck开发者_StackOverflow中文版 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.


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


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)
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜