Я хочу, чтобы следующая песня воспроизводилась после окончания первой. Как я могу решить это? Я проверил документацию Apple. Но я не нашел ничего полезного для меня.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var arrayOfSounds = ["sting", "hotel"]
var audioPlayer : AVAudioPlayer?
var isPlaying = false
var timer:Timer!
func setupAudioPlayer(file: NSString, type: NSString){
let path = Bundle.main.path(forResource: file as String, ofType: type as String)
let url = NSURL.fileURL(withPath: path!)
do {
try audioPlayer = AVAudioPlayer(contentsOf: url)
} catch {
print("Player not available")
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet weak var Slider: UISlider!
@IBAction func changeSlider(_ sender: UISlider) {
audioPlayer?.stop()
audioPlayer?.currentTime = TimeInterval(Slider.value)
audioPlayer?.prepareToPlay()
audioPlayer?.play()
}
@IBAction func playSound(_ sender: UIButton) {
let range: UInt32 = UInt32(arrayOfSounds.count)
let number = Int(arc4random_uniform(range))
self.setupAudioPlayer(file: arrayOfSounds[number] as NSString, type: "mp3")
if isPlaying == true {
audioPlayer?.pause()
isPlaying = false
} else {
audioPlayer?.play()
isPlaying = true
}
}
@IBAction func nextSound(_ sender: UIButton) {
let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "sting", ofType: "mp3")!)
do{
audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)
}catch {
print("there was some error. The error was \(error)")
}
audioPlayer?.play()
}
@IBAction func lastSound(_ sender: UIButton) {
let soundURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "hotel", ofType: "mp3")!)
do{
audioPlayer = try AVAudioPlayer(contentsOf: soundURL as URL)
}catch {
print("there was some error. The error was \(error)")
}
audioPlayer?.play()
}
func audioPlayerDidFinishPlaying(audioPlayer: AVAudioPlayer, successfully: Bool) {
print("Good")
}
}