我制作一个简单的媒体播放器。我有一个UITableViewController
来自文档目录的mp3文件和UIViewController
播放mp3文件。我将NSURL
的{{1}}从UITableViewController
传递给UIViewController
,我可以播放它。我想制作按钮,这些按钮将转向下一个或上一个。我该怎么做?
代码在特定文件上传递NSURL。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
var indexPath = tableView.indexPathForSelectedRow()
var nameOfObjectForPass = listOfMP3Files![indexPath!.row] // default it's name and
var fileManager = NSFileManager.defaultManager()
var wayToFile = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
var passMusicFileURL: NSURL? // for pass mp3
if let documentPath: NSURL = wayToFile.first as? NSURL {
let musicFile = documentPath.URLByAppendingPathComponent(nameOfObjectForPass)
println(musicFile)
passMusicFileURL = musicFile
}
if segue.identifier == "listenMusic" {
playerVC.nameMusicFile = nameOfObjectForPass // name
playerVC.mp3URL = passMusicFileURL
// test
playerVC.allUrlsVC = allURLS
}
代码正在播放mp3文件
func playMusic() {
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) // == true
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
// audioPlayer.prepareToPlay()
if currentPause == nil {
} else {
audioPlayer.currentTime = currentPause
}
//
audioPlayer.volume = 0.5
audioPlayer.play()
}
更新
我做了以下
var arrayMP3url: Array<AnyObject>!
func playNextSound() {
var queue = AVQueuePlayer(URL: mp3URL)
var current = queue.currentItem
var arrayForSearch = arrayMP3url as! [NSURL]
var arrNS = arrayForSearch as NSArray
var index = arrNS.indexOfObject(mp3URL!)
println("index \(index)")
println("array for search \(arrayForSearch)")
println("current song \(current)")
}
但我总是从NSArray
答案 0 :(得分:1)
使用您的mp3文件数组,并使用current playing index
确定indexPath
,增加或减少按钮点击current playing index
为Next, or, Prev
,并更改mp3文件指数。
重播音乐。
简单。
Ping,如果您需要更多帮助。
<强>更新强>
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
var indexPath = tableView.indexPathForSelectedRow();
//We only need to pass current index, and array of mp3 urls
playerVC.curIndex=indexPath.row;
playerVC.mp3s= listOfMP3Files;
}
如上所述,只需在playerVC
中声明两个属性,即
curIndex
为int,mp3s
为NSArray。
在功能
中func playMusic() {
var nameOfObjectForPass = mp3s![curIndex]; // default it's name and
var fileManager = NSFileManager.defaultManager()
var wayToFile = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
var passMusicFileURL: NSURL? // for pass mp3
if let documentPath: NSURL = wayToFile.first as? NSURL {
let musicFile = documentPath.URLByAppendingPathComponent(nameOfObjectForPass)
println(musicFile)
passMusicFileURL = musicFile
}
self.nameMusicFile = nameOfObjectForPass // name
self.mp3URL = passMusicFileURL
// test
self.allUrlsVC = allURLS
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) // == true
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
// audioPlayer.prepareToPlay()
if currentPause == nil {
} else {
audioPlayer.currentTime = currentPause
}
//
audioPlayer.volume = 0.5
audioPlayer.play()
}
然后简单地定义您的下一个和上一个索引计算,如
func playNextSound(){
self.curIndex++;
var maxCount=self.mp3s.count-1;
if(self.curIndex>maxCount){
self.curIndex=maxCount;
}
playMusic();
}
func playPrevSound(){
self.curIndex--;
if(self.curIndex<0){
self.curIndex=0;
}
playMusic();
}
希望它有所帮助。欢呼声。