[英]xcode - stop audio files from playing over each other
因此,我一般對xcode和編程都是陌生的。 我正在創建一個簡單的音板,但遇到了一個問題。 我想做的是,當選擇其他音頻按鈕時,停止播放當前音頻。 然后,將停止的音頻本身復位,並准備好在再次按下時從頭開始播放。 我已經在代碼中添加了stop(),它將停止播放音頻,但是當我嘗試重新啟動它時,我發現音頻剛剛暫停,並從停止的地方繼續播放。
任何人都可以為我提供解決此問題所需的代碼集,如果沒有,那么任何人都可以指出我的指導,以指導我走正確的道路。 我看了看Apple Audio文檔,很不幸我迷路了。
我的代碼如下。
var SoundPlayer1:AVAudioPlayer = AVAudioPlayer()
var SoundPlayer2:AVAudioPlayer = AVAudioPlayer()
var SoundPlayer3:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let FileLocation1 = Bundle.main.path(forResource: "Audio1", ofType: "mp3")
let FileLocation2 = Bundle.main.path(forResource: "Audio2", ofType: "mp3")
let FileLocation3 = Bundle.main.path(forResource: "Audio3", ofType: "mp3")
do {
SoundPlayer1 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: FileLocation1!))
SoundPlayer2 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: FileLocation2!))
SoundPlayer3 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: FileLocation3!))
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
}
catch {
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func catButton(_ sender: AnyObject) {
SoundPlayer1.play()
}
@IBAction func pigButton(_ sender: AnyObject) {
SoundPlayer2.play()
}
@IBAction func pigButton(_ sender: AnyObject) {
SoundPlayer3.play()
}
這只是Swift邏輯中的一小部分,您可以執行檢查其他玩家是否在玩然后停止他們!
// Make sure all audio playback times are reset
func resetAllAudioTime(){
SoundPlayer1.currentTime = 0.0;
SoundPlayer2.currentTime = 0.0;
SoundPlayer3.currentTime = 0.0;
}
IBAction func catButton(_ sender: AnyObject) {
// Check SoundPlayer2 and stop if playing
if(SoundPlayer2.playing){
SoundPlayer2.pause();
}
// Check SoundPlayer3 and stop if playing
if(SoundPlayer3.playing){
SoundPlayer3.pause();
}
// Reset all playback times to start of file
resetAllAudioTime();
// Play audio file
SoundPlayer1.play()
}
@IBAction func pigButton(_ sender: AnyObject) {
// Check SoundPlayer1 and stop if playing
if(SoundPlayer1.playing){
SoundPlayer1.pause();
}
// Check SoundPlayer3 and stop if playing
if(SoundPlayer3.playing){
SoundPlayer3.pause();
}
// Reset all playback times to start of file
resetAllAudioTime();
// Play audio file
SoundPlayer2.play()
}
@IBAction func pigButton(_ sender: AnyObject) {
// Check SoundPlayer1 and stop if playing
if(SoundPlayer1.playing){
SoundPlayer1.pause();
}
// Check SoundPlayer2 and stop if playing
if(SoundPlayer2.playing){
SoundPlayer2.pause();
}
// Reset all playback times to start of file
resetAllAudioTime();
// Play audio file
SoundPlayer3.play()
}
注意: 此代碼未經測試,但應該可以使用。 有關更多幫助,請參見AVAudioPlayer文檔。
您將看到,我沒有使用AVAudioPlayer.stop()
方法,而是使用了AVAudioPlayer.pause()
,然后將播放時間重置為開始。 如果您閱讀該文檔,則說明將調用AVAudioPlayer.stop()
方法:
func stop()
停止播放並撤消播放所需的設置。
因此,調用暫停並重設時間並不能保持撤銷播放所需的設置(應該更有效!)。
我希望這有幫助! :-)
WoodyDev,感謝您提供的干凈且易於理解的信息。 我可以通過兩個功能解決問題。
func stopAll() {
SoundPlayer1.stop()
SoundPlayer2.stop()
}
func resetAudioTime() {
SoundPlayer1.currentTime = 0.0;
SoundPlayer2.currentTime = 0.0;
}
@IBAction func catButton(_ sender: AnyObject) {
stopAll();<br/>
resetAudioTime();<br/>
SoundPlayer1.play()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.