ios - 创建一个随机音频声音发生器

标签 ios swift audio random

感谢您的回复。

我正在尝试制作一个程序,当我按下一个按钮时,会播放两个随机声音。如果我按下按钮,我可以播放随机声音,但我正在寻找如果我按下按钮,随机声音每次都会播放不同的声音。

我可以将声音粘贴在一起以按照我想要的顺序听到它们,但我希望快速生成声音。

想到用AVqueplayer做成播放列表。我在想这可以比喻成一对骰子。例如,如果我掷骰子,就会出现随机声音。

我还是个新手,我试着自己解决这个问题,因为它看起来很简单,但我现在别无选择。

这是我到目前为止得到的。当我每次按下按钮时,这将播放随机声音。

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVAudioPlayer = AVAudioPlayer()

    var sounds = ["sound1", "sound2", "sound3"]

    override func viewDidLoad() {
        super.viewDidLoad()  

    }

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        if event!.subtype == UIEventSubtype.motionShake {


            let randomNumber = Int(arc4random_uniform(UInt32(sounds.count)))
            let fileLocation = Bundle.main.path(forResource: sounds[randomNumber], ofType: "mp3")
            var error: NSError? = nil
            do { try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation!))
            player.play()
            } catch {}           
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

最佳答案

使用相同的代码,并添加第二个具有匹配文件位置的随机数将允许声音连续播放,两者都是随机的:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVAudioPlayer = AVAudioPlayer()

    var sounds = ["sound1", "sound2", "sound3"]

    override func viewDidLoad() {
        super.viewDidLoad()  

    }

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        if event!.subtype == UIEventSubtype.motionShake {


            let randomNumber1 = Int(arc4random_uniform(UInt32(sounds.count)))
            let randomNumber2 = Int(arc4random_uniform(UInt32(sounds.count)))
            let fileLocation1 = Bundle.main.path(forResource: sounds[randomNumber1], ofType: "mp3")
            let fileLocation2 = Bundle.main.path(forResource: sounds[randomNumber2], ofType: "mp3")
            //var error: NSError? = nil
            do {
                try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation1!))
                player.play()
                try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation2!))
                player.play()
            } catch {}           
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

关于ios - 创建一个随机音频声音发生器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38886889/

相关文章:

ios - TableView 复制图像?

iOS - UISearchBar 动态搜索结果在快速输入时不起作用(快速)

swift - 如何使用选择器: with function that throws exception in Swfit

windows - Windows 7 CoreAudio Media Foundation-IID_IAudioStreamVolume的用法

在 Mobile Safari 上缓存声音

ios - 水平旋转(翻转)一个按钮

ios - 如何将 NSMutableDictionary setObject 的值赋值成为 indexPath.section 的值?

swift - SpriteKit : How to create infinite game background like 'Binary Rush' or 'Stay in the line' ?

swift - 转换为 Swift 3 错误,可选类型的值未展开

iphone - 如何访问 MusicSequence 中的 MusicTracks?