Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-当按下不同UITableView单元格中的另一个播放按钮时,如何自动停止当前播放音频?_Ios_Iphone_Swift_Avplayer - Fatal编程技术网

Ios Swift-当按下不同UITableView单元格中的另一个播放按钮时,如何自动停止当前播放音频?

Ios Swift-当按下不同UITableView单元格中的另一个播放按钮时,如何自动停止当前播放音频?,ios,iphone,swift,avplayer,Ios,Iphone,Swift,Avplayer,我是一个敏捷的初学者,目前正在开发我的第一个iOS应用程序 我有一个TableViewController,它有几个单元格,每个单元格包含一个播放按钮。当按下几个播放按钮时,会同时播放几个不同的音频,但我想在按下另一个播放按钮时停止当前播放的音频 下面是我创建的示例代码 请给我一个建议。非常感谢你 ViewController.swift import UIKit class ViewController: UIViewController, UITableViewDelegate, UITa

我是一个敏捷的初学者,目前正在开发我的第一个iOS应用程序

我有一个
TableViewController
,它有几个单元格,每个单元格包含一个播放按钮。当按下几个播放按钮时,会同时播放几个不同的音频,但我想在按下另一个播放按钮时停止当前播放的音频

下面是我创建的示例代码

请给我一个建议。非常感谢你

ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "cell")

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 88
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    return cell
}


}
import UIKit
import AVFoundation

class TableViewCell: UITableViewCell, AVAudioPlayerDelegate {

var audioPlayer: AVAudioPlayer = AVAudioPlayer()
var counter = 0

@IBAction func buttonTapped(_ sender: Any) {

    // Audio player setting
    do {

        audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "sample", ofType: "mp3")!))
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()

    } catch {
        print(error)
    }

    if counter == 0 {
        audioPlayer.play()
        counter = 1
    } else {
        audioPlayer.stop()
        counter = 0
    }
}

}
TableViewCell.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "cell")

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 88
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    return cell
}


}
import UIKit
import AVFoundation

class TableViewCell: UITableViewCell, AVAudioPlayerDelegate {

var audioPlayer: AVAudioPlayer = AVAudioPlayer()
var counter = 0

@IBAction func buttonTapped(_ sender: Any) {

    // Audio player setting
    do {

        audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "sample", ofType: "mp3")!))
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()

    } catch {
        print(error)
    }

    if counter == 0 {
        audioPlayer.play()
        counter = 1
    } else {
        audioPlayer.stop()
        counter = 0
    }
}

}

一种可能的解决方案是创建_currentActivePlayer对象变量。当您开始播放音频时,请为其指定播放播放器。当其他单元格想要播放音频时,请检查该变量,停止播放机并分配新的播放机。

一种可能的解决方案是创建_currentActivePlayer对象变量。当您开始播放音频时,请为其指定播放播放器。当其他单元格想要播放音频时,请检查该变量,停止播放机并分配新的播放机。

谢谢您的回复!我试图创建一个像你在ViewController.swift中建议的那样的对象,但我在检测表视图单元格中按下了哪个播放按钮时遇到了问题。我不确定我是否走上了正确的道路。你能给我更多的线索吗?提前谢谢。关于如何在按钮处理程序中获取相应的单元格,有很多问题。我不想重复答案,只要找到答案就行了。非常感谢。谢谢你的回复!我试图创建一个像你在ViewController.swift中建议的那样的对象,但我在检测表视图单元格中按下了哪个播放按钮时遇到了问题。我不确定我是否走上了正确的道路。你能给我更多的线索吗?提前谢谢。关于如何在按钮处理程序中获取相应的单元格,有很多问题。我不想重复答案,只要找到答案就行了。非常感谢。