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

时间:2022-05-01 21:29:49

I have created a game (Swift 2.0, iOS9) in which a background music is played continuously and various other sounds are played based on user interaction.

我创建了一个游戏(Swift 2.0,iOS9),其中连续播放背景音乐,并根据用户交互播放各种其他声音。

But now, I would like to play a certain sound only in the right or left ear-phone, if the player is using head phones. Is this possible using AVFoundation?

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

2 个解决方案

#1


6  

This is how you would play a sound in left or right.

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

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

    }

}

#2


1  

You can check if audio is being played through headphones before you play that sound with this function

在使用此功能播放声音之前,您可以检查是否通过耳机播放音频

func headsetPluggedIn() -> Bool {
    let route = AVAudioSession.sharedInstance().currentRoute
    return (route.outputs ).filter({ $0.portType == AVAudioSessionPortHeadphones }).count > 0
}

And you can change the audio to left or right ear with AVAudioPlayer's pan property like this

您可以使用AVAudioPlayer的pan属性将音频更改为左耳或右耳

myAudioPlayer.pan = 1

set it to -1 for left ear or 1 for right ear

将其设置为-1(左耳或1为右耳)

#1


6  

This is how you would play a sound in left or right.

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

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

    }

}

#2


1  

You can check if audio is being played through headphones before you play that sound with this function

在使用此功能播放声音之前,您可以检查是否通过耳机播放音频

func headsetPluggedIn() -> Bool {
    let route = AVAudioSession.sharedInstance().currentRoute
    return (route.outputs ).filter({ $0.portType == AVAudioSessionPortHeadphones }).count > 0
}

And you can change the audio to left or right ear with AVAudioPlayer's pan property like this

您可以使用AVAudioPlayer的pan属性将音频更改为左耳或右耳

myAudioPlayer.pan = 1

set it to -1 for left ear or 1 for right ear

将其设置为-1(左耳或1为右耳)