如何取消 AVAudioSession

发布于 2024-11-17 13:16:16 字数 192 浏览 6 评论 0原文

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

提前致谢, 钱德拉。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2024-11-24 13:16:16

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

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

例如,在斯威夫特:

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

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)
    }
}
好听的两个字的网名 2024-11-24 13:16:16

我也有同样的问题,并且很难让它持续工作。我花了很多时间研究和调试这个问题,最后才给苹果打电话。他们告诉我查看面包屑示例代码。我按照这个例子进行操作,一切正常。

这里的问题是,会话属性和计时等有许多不同的选项。例如,您是否设置属性然后取消设置或保留它然后启动和停止会话?

这是一个类似的问题:

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

这是苹果的示例代码:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文