创建要播放的音乐文件队列
Posted
技术标签:
【中文标题】创建要播放的音乐文件队列【英文标题】:Create Queue Of Music Files To Play 【发布时间】:2015-10-16 04:58:35 【问题描述】:我有 10 个音乐文件的 URL。我想创建音乐队列,让所有歌曲一首播放,我也可以更改特定文件的索引。
我正在使用 swift 2.0
例如: iPhone 的默认播放列表正在播放。
【问题讨论】:
AVAudioPlayer - playing multiple audio files, in sequence的可能重复 谢谢@Badal,但我需要 swift 2.0 版本。 【参考方案1】:您可以使用AVAudioPlayerDelegate
,如下示例代码所示:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate
var counter = 0
var song = ["1","2","3"] //Song names array
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()
let audioPath = NSBundle.mainBundle().pathForResource("\(song[counter])", ofType: "mp3")!
var error : NSError? = nil
do
player = try AVAudioPlayer(contentsOfURL: NSURL(string: audioPath)!)
catch let error1 as NSError
error = error1
musicSlider.maximumValue = Float(player.duration)
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()
// Delegate method for AVAudioPlayerDelegate which is called every time when song finished.
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool)
if flag
counter++
if ((counter + 1) == song.count)
counter = 0
music()
Sample 了解更多信息。
希望它会有所帮助。
【讨论】:
以上是关于创建要播放的音乐文件队列的主要内容,如果未能解决你的问题,请参考以下文章