Ios 在swift中制作播放列表(开始下一首歌曲)

Ios 在swift中制作播放列表(开始下一首歌曲),ios,swift,avfoundation,avaudioplayer,playlist,Ios,Swift,Avfoundation,Avaudioplayer,Playlist,我已经创建了一个声音播放器在斯威夫特与AVFoundation。当播放歌曲结束时,我正在尝试开始阵列中的下一首歌曲。我试图实现这个代码 if (audioPlayer.currentTime >= audioPlayer.duration){ var recentSong = songPlaylist[selectedSongNumber + 1] audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPat

我已经创建了一个声音播放器在斯威夫特与AVFoundation。当播放歌曲结束时,我正在尝试开始阵列中的下一首歌曲。我试图实现这个代码

if (audioPlayer.currentTime >= audioPlayer.duration){
    var recentSong = songPlaylist[selectedSongNumber + 1]
    audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:
            NSBundle.mainBundle().pathForResource(recentSong, ofType: "mp3")!), error: nil)
    audioPlayer.play()
}
但是我不能实现这个代码(我不知道在哪里实现它)

import UIKit
import AVFoundation
import AVKit

public var audioPlayer = AVPlayer()
public var selectedSongNumber = Int()
public var songPlaylist:[String] = ["song1", "song2"]
public var recentSong = "song1"
let playImage = UIImage(named: "Play.png") as UIImage!
let pauseImage = UIImage(named: "Pause.png") as UIImage!

class FirstViewController: UIViewController {

@IBOutlet weak var musicSlider: UISlider!

@IBOutlet weak var PlayPause: UIButton!

var audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:
    NSBundle.mainBundle().pathForResource(recentSong, ofType: "mp3")!), error: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    musicSlider.maximumValue = Float(audioPlayer.duration)

    var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("updateMusicSlider"), userInfo: nil, repeats: true)


    if (audioPlayer.currentTime >= audioPlayer.duration){

        var recentSong = songPlaylist[selectedSongNumber + 1]

        audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:
            NSBundle.mainBundle().pathForResource(recentSong, ofType: "mp3")!), error: nil)

        audioPlayer.play()

        }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
@IBAction func PlayPauseButton(sender: AnyObject) {

    if (audioPlayer.playing == false){
        audioPlayer.play()
        PlayPause.setImage(pauseImage, forState: .Normal)
    }else{
        audioPlayer.pause()
        PlayPause.setImage(playImage, forState: .Normal)
    }
}

@IBAction func StopButton(sender: AnyObject) {
    audioPlayer.stop()
    audioPlayer.currentTime = 0
    PlayPause.setImage(playImage, forState: .Normal)
}

@IBAction func musicSliderAction(sender: UISlider) {
    audioPlayer.stop()
    audioPlayer.currentTime = NSTimeInterval(musicSlider.value)
    audioPlayer.play()

}

func updateMusicSlider(){

    musicSlider.value = Float(audioPlayer.currentTime)

}

}

您需要实现
AVAudioPlayerDelegate
协议的方法:

optional func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer!, successfully flag: Bool)

在此处播放下一个音乐项目


但是我不推荐,因为
AVAudioPlayer
一次只能播放一个项目。完成后,您需要使用另一个音乐项再次实例化。我建议您使用
AVQueuePlayer
。给出了详细的答案。希望有帮助

我正在用不同的东西更新代码:

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate {

    var counter = 0
    var song = ["1","2","3"]
    var player = AVAudioPlayer()

    @IBOutlet weak var musicSlider: UISlider!

    override func viewDidLoad() {
        super.viewDidLoad()
        musicSlider.value = 0.0
    }

    func updateMusicSlider(){

        musicSlider.value = Float(player.currentTime)
    }

    @IBAction func playSong(sender: AnyObject) {

        music()
    }
    @IBAction func sliderAction(sender: AnyObject) {

        player.stop()
        player.currentTime = NSTimeInterval(musicSlider.value)
        player.play()
    }

    func music(){

        var audioPath = NSBundle.mainBundle().pathForResource("\(song[counter])", ofType: "mp3")!
        var error : NSError? = nil
        player = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath), error: &error)
        musicSlider.maximumValue = Float(player.duration)
        var timer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("updateMusicSlider"), userInfo: nil, repeats: true)
        player.delegate = self
        if error == nil {
            player.delegate = self
            player.prepareToPlay()
            player.play()
        }
    }

    func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool)
    {
        println("Called")
        if flag {
            counter++
        }

        if ((counter + 1) == song.count) {
            counter = 0
        }

        music()
    }

}
你可以这样做


希望它能有所帮助,并且是获取更多信息的示例项目。

我用AVFoundation在swift中创建了一个声音播放器。我使用progressView对歌曲计时,当它达到0.98743时,它将自动更新到下一首歌曲这是Github链接:


实际上,我知道我在做更多的测试,我刚刚意识到下一首歌在第一首歌结束后只开始一次,但当第二首歌结束时,第三首歌没有开始播放,即使我从第二首歌回到第一首歌,第二首歌也没有开始播放。我想问题可能是
播放器:AVAudioPlayer
没有更新,有什么建议吗?
func updateProgressView() {
    if (progressView.progress > a.advanced(by: 0.98743)) {
        audioPlayerDidFinishPlayeing()
    }

    if audioPlayer.isPlaying {
        let progress = Float(audioPlayer.currentTime/audioPlayer.duration)
        progressView.setProgress(progress, animated: true)
    }
}