玩AVAudioPlayer声音 [英] Playing a sound with AVAudioPlayer
本文介绍了玩AVAudioPlayer声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想播放声音与 AVAudioPlayer
,但它不会工作。
编辑1:还不行
编辑2 :这code ++工程。我的设备是静音模式。
进口的UIKit
进口AVFoundation一流的ViewController:UIViewController的{ VAR audioPlayer = AVAudioPlayer()覆盖FUNC viewDidLoad中(){
super.viewDidLoad() VAR alertSound = NSURL(fileURLWithPath:NSBundle.mainBundle()pathForResource(按钮-09,ofType:WAV))
的println(alertSound) VAR错误:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL:alertSound,错误:&放大器;错误)
audioPlayer。prepareToPlay()
audioPlayer.play()}
解决方案
修改制造到code:
进口的UIKit
进口AVFoundation一流的ViewController:UIViewController的{ VAR audioPlayer = AVAudioPlayer() 覆盖FUNC viewDidLoad中(){
super.viewDidLoad() VAR alertSound = NSURL(fileURLWithPath:NSBundle.mainBundle()pathForResource(按钮-09,ofType:WAV))
的println(alertSound) //删除了德precated使用AVAudioSessionDelegate协议
AVAudioSession.sharedInstance()setCategory(AVAudioSessionCategoryPlayback,错误:无)。
AVAudioSession.sharedInstance()SETACTIVE(真,错误:无)。 VAR错误:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL:alertSound,错误:&放大器;错误)
audioPlayer。prepareToPlay()
audioPlayer.play()
}}
I'm trying to play a sound with AVAudioPlayer
but it won't work.
Edit 1: Still doesn't work.
Edit 2: This code works. My device was in silent mode.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button-09", ofType: "wav"))
println(alertSound)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
解决方案
Made modification to your code:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button-09", ofType: "wav"))
println(alertSound)
// Removed deprecated use of AVAudioSessionDelegate protocol
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}
这篇关于玩AVAudioPlayer声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文