gpt4 book ai didi

ios - AVQueuePlayer 和 AVAudioPlayer 同时调用

转载 作者:行者123 更新时间:2023-11-30 12:39:33 26 4
gpt4 key购买 nike

我的屏幕有 3 个不同的按钮。当点击 Button1 时,将调用 AVQueuePlayer。当点击 Button2 或 Button3 时,将调用 AVAudioPlayer。

当点击 Button1(启动 AVQueuePlayer),然后点击 Button2 或 Button3(启动 AVAudioPlayer)时,就会出现问题。结果是AVQueuePlayer当前正在播放的音频文件和AVAudioPlayer正在播放的音频文件同时播放。

我正在寻找一种方法来防止在 AVQueuePlayer 处于事件状态时点击 Button2 或 Button3,或者在用户点击 Button2 或 Button3 时让 AVQueuePlayer 停止播放。

我发现只使用一个播放器可以解决问题,而不是使用两个播放器,但我不知道如何解决。

        var queue = AVQueuePlayer()
var items = [AVPlayerItem]()

override func viewDidLoad() {
super.viewDidLoad()

let asset1 = AVPlayerItem(url: url1)
let asset2 = AVPlayerItem(url: url2)
let asset3 = AVPlayerItem(url: url3)


items = [asset1, asset2, asset3, asset4]

queue = AVQueuePlayer(items: items)

for item in queue.items() {
NotificationCenter.default.addObserver(self, selector:#selector(playerItemDidReachEnd(notification:)),
name: .AVPlayerItemDidPlayToEndTime, object: item)
}
}

@IBAction func pushButton1(_ sender: UIButton) {
sender.isSelected = true
queue.play()
}

@IBAction func pushButton2(sender: UIButton) {
if self.lonelyPlayer == nil {

}else {
if (self.lonelyPlayer?.isPlaying)! {
self.lonelyPlayer?.stop()
}
}
audioPlayer(url: url1, buttonId: 1) }

@IBAction func pushButton3(sender: UIButton) {
if self.lonelyPlayer == nil {

}else {
if (self.lonelyPlayer?.isPlaying)! {
self.lonelyPlayer?.stop()
}
}
audioPlayer(url: url2, buttonId: 2)
}

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if (self.currentPlayer == 1) {
yourButton2.isSelected = false
} else if (self.currentPlayer == 2) {
yourButton3.isSelected = false
}
}

func playerItemDidReachEnd(notification: NSNotification) {

if notification.object as? AVPlayerItem == items[0] {
yourButton1.isSelected = false
yourButton2.isSelected = true
}

if notification.object as? AVPlayerItem == items[1] {
yourButton2.isSelected = false
yourButton3.isSelected = true
}

func audioPlayer(url: URL, buttonId: Int) {
do {
try lonelyPlayer = AVAudioPlayer(contentsOf:url)
lonelyPlayer!.play()
lonelyPlayer!.delegate = self
yourButton2.isSelected = (buttonId == 1)
yourButton3.isSelected = (buttonId == 2)
currentPlayer = buttonId
} catch {
print(error)
}
}

最佳答案

var queue = AVQueuePlayer()
var items = [AVPlayerItem]()
var currentlyPlaying = 0
var isPlaying = false {
didSet{
checkButtons()
}
}

@IBOutlet weak var button1 : UIButton? //connect to button 1
@IBOutlet weak var button2 : UIButton? //connect to button 2
@IBOutlet weak var button3 : UIButton? //connect to button 3

override func viewDidLoad() {
super.viewDidLoad()

let asset1 = AVPlayerItem(url: url1)
let asset2 = AVPlayerItem(url: url2)
let asset3 = AVPlayerItem(url: url3)


items = [asset1, asset2, asset3, asset4]

queue = AVQueuePlayer(items: items)

for item in queue.items() {
NotificationCenter.default.addObserver(self, selector:#selector(playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object: item)
}
}

@IBAction func pushButton1(_ sender: UIButton) {
self.currentlyPlaying = 1
self.isPlaying = true
sender.isSelected = true
queue.play()
}

@IBAction func pushButton2(sender: UIButton) {
self.currentlyPlaying = 2
self.isPlaying = true
if self.lonelyPlayer == nil {

}else {
if (self.lonelyPlayer?.isPlaying)! {
self.lonelyPlayer?.stop()
}
}
audioPlayer(url: url1, buttonId: 1)
}

@IBAction func pushButton3(sender: UIButton) {
self.currentlyPlaying = 3
self.isPlaying = true
if self.lonelyPlayer == nil {

}else {
if (self.lonelyPlayer?.isPlaying)! {
self.lonelyPlayer?.stop()
}
}
audioPlayer(url: url2, buttonId: 2)
}

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if (self.currentPlayer == 1) {
yourButton2.isSelected = false
} else if (self.currentPlayer == 2) {
yourButton3.isSelected = false
}
self.isPlaying = false
}

func playerItemDidReachEnd(notification: NSNotification) {

if notification.object as? AVPlayerItem == items[0] {
yourButton1.isSelected = false
yourButton2.isSelected = true
}

if notification.object as? AVPlayerItem == items[1] {
yourButton2.isSelected = false
yourButton3.isSelected = true
}
self.isPlaying = false
)

func audioPlayer(url: URL, buttonId: Int) {
do {
try lonelyPlayer = AVAudioPlayer(contentsOf:url)
lonelyPlayer!.play()
lonelyPlayer!.delegate = self
yourButton2.isSelected = (buttonId == 1)
yourButton3.isSelected = (buttonId == 2)
currentPlayer = buttonId
} catch {
print(error)
}
}

func checkButtons() {
if isPlaying {
switch currentlyPlaying {
case 1:
button2.isEnabled = false
button3.isEnabled = false
case 2:
button1.isEnabled = false
button3.isEnabled = false
case 3:
button1.isEnabled = false
button2.isEnabled = false
default:
print("Something went wrong!")
}
} else {
button1.isEnabled = true
button2.isEnabled = true
button3.isEnabled = true
}
}

播放时禁用按钮。

注意:此代码尚未经过测试,因为我是在总线上编写的

编辑:您可以将queue设置为可选,然后使用queue.pause,然后使用queue = nil来停止它。再次使用之前记得重新分配它!

关于ios - AVQueuePlayer 和 AVAudioPlayer 同时调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42391938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com