【问题标题】:How to move sound from left to right of earphones in swift?如何快速将声音从耳机的左移到右?
【发布时间】:2016-05-10 15:35:03
【问题描述】:

我正在用 swift 为盲人制作一个应用程序,我只能在右侧耳机上播放声音,或者只能在左侧使用 pan 属性播放声音,如下所示:

@IBAction func right(sender: UIButton) {
    player.pan = 1.0 //right headphone
}


@IBAction func left(sender: UIButton) {
    player.pan = -1.0 //left headphone

}

但是我需要从右边开始播放声音,然后将它移动到左边的耳机,我该怎么做?

【问题讨论】:

  • 我建议您查看AVFoundation 中有关如何使用AVAudioPlayer 的众多教程之一,请参阅e.g. this tutorial。此外,您可能可以使用 UISlider 而不是两个按钮,并让滑块的值控制播放器的 .pan 属性的值(地图滑块跨度 -> [-1, 1])。跨度>
  • 滑块的想法不错,但我需要自动移动声音
  • 假设您尝试创建的声音是简单的音调(不是平移音乐),那么您可以查看 OpenAL 而不是使用 AVAudioPlayer - 它更专注于游戏,但看起来像可能更适合您的用例。 developer.apple.com/library/ios/documentation/AudioVideo/…

标签: ios swift audio swift2 avfoundation


【解决方案1】:

我不知道平移应该基于什么,但如果您只是希望它随时间移动,您可以使用计时器来更新它。

var timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "update", userInfo: nil, repeats: true)

func update() {
    if player.pan == -1.0 {
        timer.invalidate()
        return
    }

    player.pan -= 0.1
}

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多