HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 使用AVFoundation有没有办法在swift 2.0中只用耳机的一只耳朵播放声音?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个游戏( Swift 2.0,iOS9),其中连续播放背景音乐,并根据用户交互播放各种其他声音.

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

解决方法

这就是你如何向左或向右播放声音.

import UIKit
import AVFoundation

class 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中只用耳机的一只耳朵播放声音?所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。