AVAudioPlayer: How to Change the Playback Speed of Audio?

你离开我真会死。 提交于 2019-12-17 09:01:36

问题


I want to control the playback speed of audio in AVAudioplayer. Is this possible? If so, how would you do it?


回答1:


Now it is possible to change the playback speed.

Sample code:

player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&err];
player.volume = 0.4f;
player.enableRate = YES; //<--
[player prepareToPlay];
[player setNumberOfLoops:0];
player.rate = 2.0f; //<-- Playback Speed
[player play];

enableRate is set to YES and you can change it.

See more in the docs.




回答2:


Looks like it works for iOS5 - docs and look at rate.

thanks,




回答3:


Swift 2.0

You can increase the rate by doing this:

player.prepareToPlay()
player.enableRate = true
player.rate = 2.0
player.play()

If you want to loop, you can add this:

player.numberOfLoops = 3



回答4:


Try this -

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



回答5:


You can't. You can only change the volume not the speed of playback.

I think to do that, you will have to use the much lower level Audio Queue APIs and manipulate the audio stream manually to apply that effect.




回答6:


AVAudioPlayer does not support playback speed setting. Audio Queue Services are quite a pain to use, so that you might want to try OpenAL. See the sound engine from Cocos2D or Finch for examples of how to wrap OpenAL in Objective-C.




回答7:


set enableRate = YES and then you can adjust the rate between 0.1 and 2.0 for slowed down and sped up (1.0 is normal speed)



来源:https://stackoverflow.com/questions/2350657/avaudioplayer-how-to-change-the-playback-speed-of-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!