首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVQueuePlayer不会停止演奏

AVQueuePlayer不会停止演奏
EN

Stack Overflow用户
提问于 2020-06-17 03:59:52
回答 1查看 182关注 0票数 0

如果一个人按下这个按钮10次,他们就会听到10种不同的歌曲列表被连续播放。我想要的是,如果一个人按了10次,他们只会听一个歌曲列表。我基本上是在尝试创建一个重置按钮。

代码语言:javascript
复制
var myQueuePlayer: AVQueuePlayer?
var avItems: [AVPlayerItem] = []


func audio () {

    var items: [String] = ["one", "two", "three", "four", "five"]
    items.shuffle()

    for clip in items {
        guard let url = Bundle.main.url(forResource: clip, withExtension: ".mp3") else {
            // mp3 file not found in bundle - so crash!
            fatalError("Could not load \(clip).mp3")
        }
        avItems.append(AVPlayerItem(url: url))
        //button.isHidden = true
    }    
}


@IBAction func didTapButton() {

    audio()
    if myQueuePlayer == nil {   
        // instantiate the AVQueuePlayer with all avItems
        myQueuePlayer = AVQueuePlayer(items: avItems)

    } else {
        // stop the player and remove all avItems
        myQueuePlayer?.removeAllItems()
        // add all avItems back to the player

        avItems.forEach {
                myQueuePlayer?.insert($0, after: nil)
            }
    }
    // seek to .zero (in case we added items back in)
    myQueuePlayer?.seek(to: .zero)
    // start playing
    myQueuePlayer?.play()    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-17 17:55:25

下面是一个非常简单的例子。

这假设包中有5个.mp3文件,还有4个按钮:

Restart

  • Pause

  • Resume

  • Shuffle /
  • Play /

连接到@IBAction功能:

代码语言:javascript
复制
class TestAVQueuViewController: UIViewController {

    var myQueuePlayer: AVQueuePlayer?

    var avItemsArray: [AVPlayerItem] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // assuming I have 5 .mp3 files in the bundle, named:
        let mySongs: [String] = [
            "clip1", "clip2", "clip3", "clip4", "clip5",
        ]

        // build the array of URLs for the song files
        for clip in mySongs {
            if let url = Bundle.main.url(forResource: clip, withExtension: ".mp3") {
                avItemsArray.append(AVPlayerItem(url: url))
            } else {
                print("Could not get URL for \(clip).mp3")
            }
        }
        if avItemsArray.count == 0 {
            fatalError("Failed to get URL for ANY songs!")
        }

    }

    func playQueue() -> Void {

        // if first time
        if myQueuePlayer == nil {
            // instantiate the AVQueuePlayer
            myQueuePlayer = AVQueuePlayer()
        }

        guard let player = myQueuePlayer else {
            // I suppose it's possible that AVQueuePlayer() failed to instantiate
            //  so print a message to debug console and return
            print("AVQueuePlayer failed to instantiate!")
            return
        }

        // this will make sure the player is stopped and remove any remaining avItems
        player.removeAllItems()

        // every time this is called,
        //  loop through and reset the time on each avItem
        for item in avItemsArray {
            item.seek(to: .zero, completionHandler: nil)
        }

        // add avItems to the player
        avItemsArray.forEach {
            player.insert($0, after: nil)
        }

        // start playing
        player.play()
    }

    @IBAction func playRestartTapped(_ sender: Any) {
        playQueue()
    }

    @IBAction func pauseTapped(_ sender: Any) {
        guard let player = myQueuePlayer, player.items().count > 0 else {
            return
        }
        player.pause()
    }

    @IBAction func resumeTapped(_ sender: Any) {
        guard let player = myQueuePlayer, player.items().count > 0 else {
            return
        }
        player.play()
    }

    @IBAction func restartShuffledTapped(_ sender: Any) {
        // shuffle the items
        avItemsArray.shuffle()
        playQueue()
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62421276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档