Итак, я сейчас создаю приложение для воспроизведения музыкального плеера. Мое первое представление содержит ячейки табличного представления и песни внутри них. Во втором представлении находится музыкальный проигрыватель (воспроизведение, пауза и т. Д.). Я также хотел бы добавить индикатор прогресса в представление. Однако, когда воспроизводится аудиофайл, на панели нет прогресса, и когда нажата кнопка воспроизведения, я получаю сообщение об ошибке в разделе viewdidload «Произошла ошибка при попытке извлечь аудиофайл». Любая помощь приветствуется.
class SecondViewController: UIViewController {
@IBOutlet weak var progressBar: UIProgressView!
var audioPlayer:AVAudioPlayer!
@IBAction func playbtn(_ sender: Any) {
if audioStuffed == true && audioPlayer.isPlaying == false
{
audioPlayer.play()
}
}
func playThis (thisOne:String) {
do
{
let audioPath = Bundle.main.path(forResource: thisOne, ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
audioPlayer.play()
}
catch
{
print ("ERROR")
}
}
@objc func setProgressBar()
{
if audioPlayer.isPlaying
{
// Update progress
progressBar.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration),
animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let path: String! = Bundle.main.resourcePath?.appending("/(thisSong).mp3")
let mp3URL = NSURL(fileURLWithPath: path)
do
{
// 2
audioPlayer = try AVAudioPlayer(contentsOf: mp3URL as URL)
audioPlayer.play()
// 3
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:
#selector(setProgressBar), userInfo: nil, repeats: true
progressBar.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration),
animated: false)
}
catch
{
print("An error occurred while trying to extract audio file")
}
}
}