gpt4 book ai didi

ios - AVPlayer 暂停后不会继续播放

转载 作者:行者123 更新时间:2023-11-28 16:07:25 25 4
gpt4 key购买 nike

我刚刚从 AVAudioPlayer 切换到 AVPlayer,我正在检查我的旧功能并进行适当的调整。 AVPlayer 用于使用 URL 播放远程音频文件。当我选择要播放和暂停的文件时,一切正常。但是,当我想继续播放暂停的文件时,即使我看到正在调用播放功能,播放器也不会播放。我设置了一个观察者来知道玩家何时完成播放,以便播放/暂停按钮可以切换,我注意到观察者在我按下暂停后被调用。这不应该发生,因为玩家还没有玩完对吗?不管怎样,我设置了断点,一切都被正确调用了。知道为什么 AVPlayer 在暂停后不会继续播放吗?

var playerItem: AVPlayerItem?
var newPlayer: AVPlayer?
var trackIDplaying: Int?

func playPausePressed(_ sender:UIButton)
{

if let selectedTrackID = trackIDplaying
{

do
{

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)

if selectedTrackID == track.trackId
{
//if playing, then pause. If not playing, then play
if (self.newPlayer!.rate != 0)
{
self.newPlayer!.pause()
self.isPaused = true
}
else
{
if self.newPlayer!.currentItem != nil
{
self.newPlayer!.play()
print(self.newPlayer!.currentItem)
}
}
}
else
{
//If song is playing, switch to new song
let trackURL = URL(string: track.preSignedURL!)
trackIDplaying = track.trackId
self.playerItem = AVPlayerItem(url: trackURL!)
NotificationCenter.default.addObserver(self, selector: #selector((HomeController.playerDidFinishPlaying)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerItem)
self.newPlayer = AVPlayer(playerItem: self.playerItem!)
self.newPlayer!.play()
}
}
catch let error1 as NSError
{
error = error1
self.newPlayer = nil
}
}
else
{
do
{
//play selected song if no other songs are playing
let trackURL = URL(string: track.preSignedURL!)
print(trackURL!)
self.playerItem = AVPlayerItem(url: trackURL!)
NotificationCenter.default.addObserver(self, selector: #selector((HomeController.playerDidFinishPlaying)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerItem)

self.newPlayer = AVPlayer(playerItem: self.playerItem!)
self.newPlayer!.play()
self.trackIDplaying = track.trackId
}
}
if let err = error
{
print("audio player error \(err.localizedDescription)", terminator: "")
}
}

func playerDidFinishPlaying(note: NSNotification) {

guard let indexPath = self.playingAudioIndexPath, let cell = self.audioTable.cellForRow(at: indexPath) as? AudioCell else {

return;
}

cell.playButton.isSelected = false
self.playingAudioIndexPath = nil

}

最佳答案

尝试仅在 viewDidLoad()viewWillAppear() 中添加观察者 对于这类问题,添加观察者应该一次性完成。如果您还有任何问题,请随时问我。

关于ios - AVPlayer 暂停后不会继续播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40077975/

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