왼쪽 또는 오른쪽으로 사운드를 재생하는 방법입니다.
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()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var audioPlayer = AVAudioPlayer()
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("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(contentsOfURL: alertSound) //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")
}
}
}
예를 위해 왼쪽 귀 또는 1 -1로,이 일했다. 고맙습니다. .. – Suhas
나는 이걸 두 번 upvote 할 수 있으면 좋겠어. 모든 코멘트는 매우 유용했고 코드를 읽기 쉽도록 만들었습니다. 아주 좋은 대답, 다시 한번 감사드립니다. 또한 성명서의 필요성을 표시해주십시오. audioPlayer.prepareToPlay() – Suhas