-3
我已經在表格視圖中顯示iTunes音樂庫的所有歌曲。現在,我想在用戶點擊它時立即在表格視圖中播放選定的歌曲。播放iOS中的表格視圖中顯示的歌曲
這裏是我的代碼:
import UIKit
import MediaPlayer
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var table: UITableView!
//Create an array with some elements for the table view rows
var myMusicPlayer = MPMusicPlayerController()
var allSongsArray: [MPMediaItem]!
let songsQuery = MPMediaQuery.songsQuery()
var abcArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "#"]
//Define the amount of sections in table view
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return abcArray.count
}
//Assign the amount of elements in the array to the amount of rows in one section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allSongsArray.count
}
//Set up each element in abcArray as title for section
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.abcArray[section] as String
}
//Set up the Index Search
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return abcArray
}
//Assign each element in the array a row in table view
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell")
var items = allSongsArray[indexPath.row]
cell?.textLabel?.text = items.title
cell?.detailTextLabel?.text = items.artist
var imageSize = CGSizeMake(100, 100)
cell?.imageView?.image = items.artwork?.imageWithSize(imageSize)
return cell!
}
override func viewDidLoad() {
super.viewDidLoad()
self.allSongsArray = songsQuery.items! as [MPMediaItem]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
什麼是你的問題?你有什麼問題?您是否嘗試過任何代碼來播放歌曲或處理行選擇? – rmaddy
其實我不知道,哪一段代碼適合播放歌曲......? – Ruwen