Swift Avplayer - 当点击 'Play' 时停止所有其他 tableviewcells 播放
Swift Avplayer - Stop all other tableviewcells from playing, when 'Play' is tapped
我的主页是一个 table 视图,每个单元格都有一个播放按钮(单击时会创建一个 AVAudioPlayer
实例)。问题是我可以在多个单元格上单击播放,它们将同时播放。我想弄清楚如何在单击任何“播放”按钮时暂停其他单元格。
这是我的 TableViewCell
文件中的播放按钮代码:
@IBAction func playButtonTapped(_ sender: UIButton) {
if self.audioPlayer != nil {
if self.audioPlayer.isPlaying {
self.audioPlayer.pause()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePlay"), for: .normal)
}
else {
self.audioPlayer.play()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePause"), for: .normal)
}
return
}
do {
self.audioPlayer = try AVAudioPlayer(data: self.audioFile!)
self.audioPlayer.prepareToPlay()
self.audioPlayer.delegate = self as? AVAudioPlayerDelegate
self.audioPlayer.play()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePause"), for: .normal)
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
} catch {
print(#line, error.localizedDescription)
}
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(HomeTableViewCell.updateProgress)), userInfo: nil, repeats: true)
}
我一直在尝试协议,试图编辑 TableViewVC
和 TableViewCellVC
,但我不太明白。
您可以尝试在调用 self.audioPlayer.pause()
之后调用 self.audioPlayer.replaceCurrentItem(with: nil)
,这样您的代码将如下所示。
if self.audioPlayer != nil {
if self.audioPlayer.isPlaying {
self.audioPlayer.pause()
self.audioPlayer.replaceCurrentItem(with: nil)
self.audioPlayer.prepareToPlay()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePlay"), for: .normal)
}
else {
self.audioPlayer.play()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePause"), for: .normal)
}
...
}
由于您在一个单元格中有一个音频播放器,因此您需要一种方法来通知所有其他单元格已单击音频播放器。您可以通过在每个 UITableViewCell
中创建一个闭包并在 cellForRow
中分配它来实现这一点。每当单击播放按钮时,都会触发闭包,在您的 UIViewController
中,您将遍历所有可见单元格并暂停它们,然后在您单击的单元格中播放播放器。像这样:
class CustomTableViewCell : UITableViewCell{
var playButtonTapped : (()->())?
@IBAction func playButtonTapped(_ sender: UIButton) {
playButtonTapped?()
//rest of the code comes below
}
}
class TableViewController : UIViewController, UITableViewDataSource{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
....
cell.playButtonTapped = {
for tempCell in tableView.visibleCells{
if let ultraTempCell = tempCell as? CustomTableViewCell, ultraTempCell != cell /* or something like this */{
//pause the player here
}
}
}
}
}
或者,你可以做的是,不是在每个单元格中都有一个玩家,而是在 UIViewController
中创建一个玩家,并根据点击的单元格,换里面的歌就行了
我的主页是一个 table 视图,每个单元格都有一个播放按钮(单击时会创建一个 AVAudioPlayer
实例)。问题是我可以在多个单元格上单击播放,它们将同时播放。我想弄清楚如何在单击任何“播放”按钮时暂停其他单元格。
这是我的 TableViewCell
文件中的播放按钮代码:
@IBAction func playButtonTapped(_ sender: UIButton) {
if self.audioPlayer != nil {
if self.audioPlayer.isPlaying {
self.audioPlayer.pause()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePlay"), for: .normal)
}
else {
self.audioPlayer.play()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePause"), for: .normal)
}
return
}
do {
self.audioPlayer = try AVAudioPlayer(data: self.audioFile!)
self.audioPlayer.prepareToPlay()
self.audioPlayer.delegate = self as? AVAudioPlayerDelegate
self.audioPlayer.play()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePause"), for: .normal)
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
} catch {
print(#line, error.localizedDescription)
}
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(HomeTableViewCell.updateProgress)), userInfo: nil, repeats: true)
}
我一直在尝试协议,试图编辑 TableViewVC
和 TableViewCellVC
,但我不太明白。
您可以尝试在调用 self.audioPlayer.pause()
之后调用 self.audioPlayer.replaceCurrentItem(with: nil)
,这样您的代码将如下所示。
if self.audioPlayer != nil {
if self.audioPlayer.isPlaying {
self.audioPlayer.pause()
self.audioPlayer.replaceCurrentItem(with: nil)
self.audioPlayer.prepareToPlay()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePlay"), for: .normal)
}
else {
self.audioPlayer.play()
self.playbutton.setImage(#imageLiteral(resourceName: "bluePause"), for: .normal)
}
...
}
由于您在一个单元格中有一个音频播放器,因此您需要一种方法来通知所有其他单元格已单击音频播放器。您可以通过在每个 UITableViewCell
中创建一个闭包并在 cellForRow
中分配它来实现这一点。每当单击播放按钮时,都会触发闭包,在您的 UIViewController
中,您将遍历所有可见单元格并暂停它们,然后在您单击的单元格中播放播放器。像这样:
class CustomTableViewCell : UITableViewCell{
var playButtonTapped : (()->())?
@IBAction func playButtonTapped(_ sender: UIButton) {
playButtonTapped?()
//rest of the code comes below
}
}
class TableViewController : UIViewController, UITableViewDataSource{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
....
cell.playButtonTapped = {
for tempCell in tableView.visibleCells{
if let ultraTempCell = tempCell as? CustomTableViewCell, ultraTempCell != cell /* or something like this */{
//pause the player here
}
}
}
}
}
或者,你可以做的是,不是在每个单元格中都有一个玩家,而是在 UIViewController
中创建一个玩家,并根据点击的单元格,换里面的歌就行了