【问题标题】:Swift Sound on repeat快速重复的声音
【发布时间】:2015-02-05 03:42:46
【问题描述】:

我有这个功能,我在第一个场景中就实现了:

    func ThemeSound() {
        if let path = NSBundle.mainBundle().pathForResource("InfinityThemeMain", ofType: "wav") {
            let enemy1sound = NSURL(fileURLWithPath:path)
            println(enemy1sound)

            var error:NSError?
            ThemePlayer = AVAudioPlayer(contentsOfURL: enemy1sound, error: &error)
            ThemePlayer.prepareToPlay()
            ThemePlayer.play()
        }
    }

但问题是,它只播放一次主题。我需要它重复播放。我以为我知道如何做到这一点。

runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(ThemePlayer), SKAction.waitForDuration(2.55)]))

但是我不能粘贴到 ViewController 中,因为 runAction 需要一个孩子来附加它。 还有什么办法可以吗?

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    我使用Timer() 让它重复 这是我的代码:

    import UIKit
    import AVFoundation
    
    class ViewController: UIViewController {
    
        var player = AVAudioPlayer()
    
        var timer = Timer()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    
        @IBAction func play(_ sender: UIButton) {
    
           // var player = AVAudioPlayer()
    
    
            let soundURL = Bundle.main.url(forResource: "beeb", withExtension: "wav")
    
            do {
                player = try AVAudioPlayer(contentsOf: soundURL!)
            } catch {
                print("No sound found by URL")
            }
    
    
    
            alertation(title: "click stop to stop", msg: "")
    
    
        }
    
    
        func alertation(title:String,msg:String)
        {
    
            player.play()
            self.timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { (timer) in
                self.player.play()
            })
    
    
    
            var alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
            var stop = alert.addAction(UIAlertAction(title: "STOP", style: .default, handler: { (action) in
    
                 self.player.stop()
                self.timer.invalidate()
    
            }))
    
    
            self.present(alert, animated: true, completion: nil)
    
        }
    
    
    
    }
    

    【讨论】:

      【解决方案2】:

      只需将 numberOfLoops 属性设置为负数。

      ThemePlayer.numberOfLoops = -1
      

      来自the documentation

      numberOfLoops 声音在到达结尾时返回开头重复播放的次数。

      ...

      var numberOfLoops: Int

      ...

      默认值 0 表示播放声音一次。设置一个正整数值来指定返回开始并再次播放的次数。例如,将值指定为 1 会导致总共播放两次声音。 设置任何负整数值以无限期循环声音,直到您调用 stop 方法

      您可能还需要make your AVAudioPlayer a property, rather than a local variable。就目前而言,通过自动引用计数,我认为您的本地 ThemePlayer 实例可能会被 ARC 释放。比我更了解 AVAudioPlayer 的人或许可以对此发表评论。

      (顺便说一句,您可能希望遵循 Apple 命名对象实例的约定,以小写字母开头,例如 themePlayer 而不是 ThemePlayer。这将使您的代码更容易被其他人阅读。)

      【讨论】:

      • 确保使用 m4a 格式,因为这样可以避免重复之间出现可听见的间隙。 OSX 原生支持转换,如下所述:osxdaily.com/2011/07/30/convert-audio-to-m4a-mac-os-x-lion
      • @nontomatic 当我将其转换为 m4a 时,将 numberOfLoops 设置为 -1 不会使其重复。请帮忙!我明天有一个学校项目要交;(
      • Alex,你的代码一定有问题,它应该可以工作。我建议发布您的代码,也许会提出一个新问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多