1
我有一個包含歌曲列表的tableviewcontroller,只要用戶點擊一首歌曲,它就會打開另一個視圖控制器並播放它。每當用戶按下下一個按鈕或前一個按鈕時,我都需要幫助,以便播放上一首或下一首歌曲。這裏是我的tableviewcontroller:播放下一首歌AVAudioPlayer
import UIKit
class LibraryTableViewController: UITableViewController {
var titleText = ["Heroes", "Skyline", "In The Woods"]
var authorText = ["Kedam", "Kovan & Electro Light", "Max Pros"]
@IBOutlet var songList: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return titleText.count
}
//tableview delegate
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell : LibrarySongTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Library Cell") as! LibrarySongTableViewCell
if(cell == nil)
{
cell = Bundle.main.loadNibNamed("Library Cell", owner: self, options: nil)?[0] as! LibrarySongTableViewCell;
}
let titleTitle = titleText[indexPath.row] as String //NOT NSString
let authorTitle = authorText[indexPath.row] as String
cell.titleLabel.text=titleTitle
cell.authorLabel.text=authorTitle
cell.imageView?.image = UIImage(named: titleTitle)
cell.imageView?.layer.cornerRadius = 5
return cell as LibrarySongTableViewCell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "toLibraryDetail"
{
let detailViewController = ((segue.destination) as! LibraryDetailViewController)
let indexPath = self.songList!.indexPathForSelectedRow!
let topicLabelText = titleText[indexPath.row]
let detailLabelText = authorText[indexPath.row]
detailViewController.titleLabelText = topicLabelText
detailViewController.authorLabelText = detailLabelText
}
}
}
這裏是該小區去播放該歌曲的視圖控制器:
import UIKit
import AVFoundation
import MediaPlayer
// audioPlayer Outlet
var libraryPlayer = AVAudioPlayer()
class LibraryDetailViewController: UITableViewController, AVAudioPlayerDelegate {
// Title and Author
var titleLabelText: String!
var authorLabelText: String!
@IBOutlet weak var albumArt: UIImageView!
@IBOutlet weak var titleText: UILabel!
// Image Outlets
@IBOutlet weak var authorText: UILabel!
@IBOutlet weak var blurredAlbumArt: UIImageView!
// Toolbar Outlets
@IBOutlet weak var toolbar: UIToolbar!
var playButton: UIBarButtonItem!
var pauseButton: UIBarButtonItem!
var flexibleSpace: UIBarButtonItem!
var nextButton: UIBarButtonItem!
var previousButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Customize Album Art
albumArt.image = UIImage(named: titleLabelText)
blurredAlbumArt.image = UIImage(named: titleLabelText)
albumArt.layer.cornerRadius = 10
// Set label text
titleText.text = titleLabelText
authorText.text = authorLabelText
// Create the audioPlayer
do {
libraryPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: titleLabelText, ofType: "mp3")!))
libraryPlayer.delegate = self
libraryPlayer.prepareToPlay()
var audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}
}
catch {
print(error)
}
// Create ToolBar Buttons
playButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.play, target: self, action: "playButtonTapped")
pauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.pause, target: self, action: "pauseButtonTapped")
flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
previousButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.rewind, target: self, action: "previousButtonTapped")
nextButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fastForward, target: self, action: "nextButtonTapped")
// Customize ToolBar Buttons
previousButton.tintColor = UIColor.lightGray
nextButton.tintColor = UIColor.lightGray
toolbar.items = [flexibleSpace, previousButton, flexibleSpace, pauseButton, flexibleSpace, nextButton, flexibleSpace]
// Set the delegate
// Play
libraryPlayer.play()
// Do any additional setup after loading the view.
}
// Play
func playButtonTapped() {
toolbar.items = [flexibleSpace, previousButton, flexibleSpace, pauseButton, flexibleSpace, nextButton, flexibleSpace];
libraryPlayer.play()
}
// Pause
func pauseButtonTapped() {
toolbar.items = [flexibleSpace, previousButton, flexibleSpace, playButton, flexibleSpace, nextButton, flexibleSpace];
libraryPlayer.pause()
}
// Previous
func previousButtonTapped() {
// NEED HELP HERE
}
// Next
func nextButtonTapped() {
// NEED HELP HERE
}
override func viewDidDisappear(_ animated: Bool) {
libraryPlayer.pause()
}
override func viewDidAppear(_ animated: Bool) {
libraryPlayer.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func dismiss(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
感謝這麼多的幫助!我使用的是斯威夫特3和Xcode的8
看到這篇文章https://stackoverflow.com/a/14704753/4970453 –
@VarinderSingh我還沒有推出Objective C你有什麼東西在Swift中?謝謝! – iFunnyVlogger