我有一个带有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)
}
}
答案 0 :(得分:0)
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
来停止它。在再次使用它之前,请记得重新分配它!