根据阵列中每个I时间的持续时间播放快速声音 [英] Play swift sound based on each duration of I time in array

查看:39
本文介绍了根据阵列中每个I时间的持续时间播放快速声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的快速代码调用playSound并根据数组playamount中每个项目的持续时间播放声音.因此,我希望用户第一次播放声音10秒钟,然后从头开始播放声音20秒钟,然后播放同一声音30秒钟.因此,声音总是在每次被调用时从头开始.

I want my swift code to call the playSound and play the sound based for the duration of each item in array playamount. So I want the user to play the sound for the first time for 10 seconds then play the sound starting at the beginning for 20 and then the same thing for 30 seconds. So the sound always starts at the beginning each time it is called.

import UIKit;  import AVFoundation

class ViewController: UIViewController {
    
  

    var player: AVAudioPlayer?

    func playSound() {
        let url = Bundle.mainBundle().URLForResource("rock", withExtension: "mp3")!

        do {
            player = try AVAudioPlayer(contentsOfURL: url)
            guard let player = player else { return }

            player.prepareToPlay()
            player.play()

        } catch let error as NSError {
            print(error.description)
        }
    }
    var playAmount : [Int] = [10,20,30]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

推荐答案

class ViewController: UIViewController {

    var player: AVAudioPlayer?
    var currentIndex: Int = 0

    func runMusicBox() {
        guard !playAmount.isEmpty else { return }
        currentIndex = 0
        newTimerForIndex(index: 0)
    }
    
    func newTimerForIndex(index: Int) {
        player?.prepareToPlay()
        player?.play()
        Timer.scheduledTimer(withTimeInterval: Double(playAmount[index]), repeats: false) { timer in
            self.player?.stop()
            if self.currentIndex + 1 < self.playAmount.count {
                self.currentIndex += 1
                self.newTimerForIndex(index: self.currentIndex)
            } else {
                self.player?.stop()
            }
        }
    }
    
    func playSound() {
        let url = Bundle.mainBundle().URLForResource("rock", withExtension: "mp3")!

        do {
            player = try AVAudioPlayer(contentsOfURL: url)
            guard let player = player else { return }
            runMusicBox()

        } catch let error as NSError {
            print(error.description)
        }
    }
    var playAmount : [Int] = [10,20,30]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

您好,您可以尝试使用此代码吗?在这里,我有一个计时器,当一个计时器停止时,我们检查数组中是否还有另一个元素,并将运行一个新的计时器.计时器正在工作,但我没有检查播放器.如果它能按预期工作-应该可以为您工作

Hi, can you try this code. Here I have a timer, and when one stops, then we check if we have another element in the array and will run a new timer. The Timer is working, but i didn;t check the player. if it works as expected - it should work for you

这篇关于根据阵列中每个I时间的持续时间播放快速声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆