ios – 使用AVFoundation有没有办法在swift 2.0中只用耳机的一只耳朵播放声音?

ios – 使用AVFoundation有没有办法在swift 2.0中只用耳机的一只耳朵播放声音?,第1张

概述我创建了一个游戏( Swift 2.0,iOS9),其中连续播放背景音乐,并根据用户交互播放各种其他声音. 但是现在,如果播放器使用的是头戴式耳机,我只想在右耳或左耳手机中播放一定的声音. 这可能使用AVFoundation吗? 这就是你如何向左或向右播放声音. import UIKitimport AVFoundationclass ViewController: UIViewContro 我创建了一个游戏( Swift 2.0,iOS9),其中连续播放背景音乐,并根据用户交互播放各种其他声音.

但是现在,如果播放器使用的是头戴式耳机,我只想在右耳或左耳手机中播放一定的声音.
这可能使用AVFoundation吗?

解决方法 这就是你如何向左或向右播放声音.

import UIKitimport AVFoundationclass VIEwController: UIVIEwController {    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // Do any additional setup after loading the vIEw,typically from a nib.        testSound()    }    var audioPlayer = AVAudioPlayer()    let alertSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "CheckoutScannerBeep",ofType: "mp3")!) // If sound not in an assest     //let alertSound = NSDataAsset(name: "CheckoutScannerBeep") // If sound is in an Asset    func testSound(){        do {            //        audioPlayer = try AVAudioPlayer(data: (alertSound!.data),fileTypeHint: AVfileTypeMPEGLayer3) //If in asset            audioPlayer = try AVAudioPlayer(contentsOf: alertSound as URL) //If not in asset            audioPlayer.pan = -1.0 //left headphone            //audioPlayer.pan = 1.0 // right headphone            audioPlayer.preparetoPlay() // make sure to add this line so audio will play            audioPlayer.play()        } catch  {            print("error")        }    }}
总结

以上是内存溢出为你收集整理的ios – 使用AVFoundation有没有办法在swift 2.0中只用耳机的一只耳朵播放声音?全部内容,希望文章能够帮你解决ios – 使用AVFoundation有没有办法在swift 2.0中只用耳机的一只耳朵播放声音?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1075289.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存