arrays - AVQueuePlayer不会停止播放

标签 arrays swift audio avqueueplayer

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

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()    
}

最佳答案

这是一个非常简单的例子。

假设您的 bundle 包中有5个.mp3文件,并有4个按钮:

  • 播放/重新启动
  • 暂停
  • 恢复
  • 随机播放和播放

  • 连接到@IBAction函数:
    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()
        }
    
    }
    

    关于arrays - AVQueuePlayer不会停止播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62421276/

    相关文章:

    php - 在 UPDATE/SET 语句中取消嵌套,PostgreSQL/PHP

    android - MediaCodec 在 Android 上解码 AAC 音频 - 解码但听不到任何输出

    android - Delphi中的跨平台音频支持

    java - 如何计算java中音频信号的电平/幅度/分贝?

    javascript - 对包含 3 个元素的二维数组进行排序

    arrays - 如何在时间复杂度 < O(n^2) 和空间复杂度 O(n) 中使用 swift 首先按值排序整数数组,然后按重复次数排序整数数组

    ios - Swift 随机数组 - 访问前一个索引

    ios - 具有依赖性的异步任务执行

    ios - 为什么我的代码只能在 DispatchQueue.main.async block 内运行?

    ios - Firebase .indexOn 动态键