我試圖按一個按鈕,音頻文件播放一個接一個。試圖按一個按鈕,然後按順序播放音頻或隨機播放
這是我的代碼如下。我按播放鍵,兩個聲音同時播放。 我試圖按播放按鈕,聲音開始並停止,然後另一個聲音文件開始和停止。按下2個聲音文件按下的一個按鈕是我的目標。謝謝
我想在循環中,但它不工作。
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var sliderValue: UISlider!
var player:AVAudioPlayer = AVAudioPlayer()
var player1:AVAudioPlayer = AVAudioPlayer()
@IBAction func play(_ sender: AnyObject) {
player.play()
player1.play()
}
@IBAction func pause(_ sender: AnyObject) {
player.pause()
}
@IBAction func stop(_ sender: AnyObject) {
player.stop()
player.currentTime = 0
}
@IBAction func sliderChanged(_ sender: AnyObject) {
player.volume = sliderValue.value
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let audioPath = Bundle.main.path(forResource: "sound1", ofType: "mp3")!
let audioPath1 = Bundle.main.path(forResource: "sound2", ofType: "mp3")!
do {
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath))
try player1 = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath1))
} catch{
//Process Error here
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
完成後播放第二個聲音,您可以使用AVAudioPlayer的代理來播放聲音播放完成與否。 –
我忘了說。我是一個新手。謝謝rohit –
訪問此鏈接https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/index.html#//apple_ref/occ/cl/AVAudioPlayer關於它的詳細說明 –