开发者

iPhone SDK: Change playback speed using core audio AVAudioPlayer

开发者 https://www.devze.com 2022-12-22 12:26 出处:网络
I\'d like to be able to play back a开发者_JS百科udio I\'ve recorded using AVAudioRecorder @ 1.5x or 2.0x speed. I don\'t see anything in AVAudioPlayer that will support that. I\'d appreciate some sugg

I'd like to be able to play back a开发者_JS百科udio I've recorded using AVAudioRecorder @ 1.5x or 2.0x speed. I don't see anything in AVAudioPlayer that will support that. I'd appreciate some suggestions, with code if possible, on how to accomplish this with the iPhone 3.x SDK. I'm not overly concerned with lowering the pitch to compensate for increased playback speed, but being able to do so would be optimal.


Changing the sample rate should work just like a tape recorder would if you played it faster. Everything becomes higher in pitch. To keep the pitch the same, you would need to set up an Audio Unit graph that includes the AUPitch effect, and lower the pitch by the same ratio that you increase the sample rate by.


See this question. In other words, you'll have to use a different API like Audio Queue Services. If you want to try a simple hack, you can try doubling the sample rate property of the audio file. It probably won't work though. Also, be warned that it is fairly CPU demanding to adjust the playback speed while keeping the same pitch. You'll need to use a one of the available techniques called "time stretching". Also, in case you haven't realized it by now, doing complicated things with audio on the iPhone is a major pain because Apple's documentation is generally either very bad or nonexistent.


If you want to play audio faster or slower, there is a build-in method in avAudioPlayer. Have you tried AudioPlayer Rate property ? in Swift it will be like this -

    audioP = try! AVAudioPlayer(contentsOf: URL(fileURLWithPath: selectedPath), fileTypeHint: "caf")
    audioP.enableRate = true
    audioP.prepareToPlay()
    audioP.rate = 1.5
    audioP.play()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号