Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用AVAudioEngine更改重复声音的音调_Ios_Swift_Repeat - Fatal编程技术网

Ios 使用AVAudioEngine更改重复声音的音调

Ios 使用AVAudioEngine更改重复声音的音调,ios,swift,repeat,Ios,Swift,Repeat,所以我一直在努力让AVAudioEngine为我重复一个声音。我需要它反复播放26秒左右的曲目。它需要使用AVAudioEngine,而不是AVAudioPlayer,因为我需要能够改变它的音调。我发现了一些改变音调、强迫声音重复的例子,但仅限于正弦波,链接如下 下面是我在尝试实现该问题之前的旧代码。按下按钮时,它将播放一次声音,但不会再次播放 import UIKit import AVFoundation class aboutViewController: UIViewController

所以我一直在努力让AVAudioEngine为我重复一个声音。我需要它反复播放26秒左右的曲目。它需要使用AVAudioEngine,而不是AVAudioPlayer,因为我需要能够改变它的音调。我发现了一些改变音调、强迫声音重复的例子,但仅限于正弦波,链接如下

下面是我在尝试实现该问题之前的旧代码。按下按钮时,它将播放一次声音,但不会再次播放

import UIKit
import AVFoundation
class aboutViewController: UIViewController {

    var audioUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!)
    var audioEngine = AVAudioEngine()
    var myPlayer = AVAudioPlayerNode()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        audioEngine.attachNode(myPlayer)
        var audioFile = AVAudioFile(forReading: audioUrl, error: nil)
        var audioError: NSError?
        audioEngine.connect(myPlayer, to: audioEngine.mainMixerNode, format: audioFile.processingFormat)
        myPlayer.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
        audioEngine.startAndReturnError(&audioError)


    }

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


    @IBAction func toggleSideMenu(sender: AnyObject) {
    toggleSideMenuView()
    }

    @IBAction func testSound(sender: AnyObject) {
    myPlayer.play()
    }
}
下面是我尝试实现重复的代码,它没有任何作用,也不会崩溃

import UIKit
import AVFoundation
class aboutViewController: UIViewController {

    var audioUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!)
    var audioEngine = AVAudioEngine()
    var myPlayer = AVAudioPlayerNode()
    var audioError: NSError?

    override func viewDidLoad() {
        super.viewDidLoad()

    // Do any additional setup after loading the view.

        audioEngine.attachNode(myPlayer)

        var buffer = AVAudioPCMBuffer(PCMFormat: myPlayer.outputFormatForBus(0),frameCapacity:100)
        buffer.frameLength = 100
        var mixer = audioEngine.mainMixerNode
        var audioFile = AVAudioFile(forReading: audioUrl, error: nil)

        audioEngine.connect(myPlayer, to: audioEngine.mainMixerNode, format: audioFile.processingFormat)

        audioEngine.startAndReturnError(&audioError)
        myPlayer.play()
        myPlayer.scheduleBuffer(buffer, atTime: nil, options:.Loops , completionHandler: nil)
    }

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


    @IBAction func testSound(sender: AnyObject) {

    }
}

所以我要问的是:你能不能把一个例子放在一起,反复播放一个音频文件,在顶部有一个变量,比如:let tone:Int=100,它设置音频文件重复播放的音调

谢谢你的帮助

链接:

重复正弦波的示例:


音调变化示例:

您的音频是否已重复?当你尝试将AVAudioUnitTimePitch连接到你的音频引擎时会发生什么?不,目前什么都没有发生,它不会重复。我想我知道如何改变音高,是重复给我带来了麻烦@IanMacDonaldIt看起来您从未将音频文件加载到缓冲区。尝试使用audioFile.readIntoBuffer…正确,行audioFile.readIntoBuffer,error:audioError我得到错误:call@ianmacdonald中参数“frameCount”缺少参数如果将frameCount:100创建缓冲区的容量添加到调用中会发生什么?实际上,看起来您可能只是错过了一个&before音频错误。