crashtesttommy
crashtesttommy

Reputation: 439

iPhone Sound: Adjust speed of playback of audio file while playing

is there a way to adjust the speed of the playback of an audio while playing in Objective C for the iPhone/iPod touch?

Also would be interesting if playing a file backwards would be possible.

Thanks

Tom

Upvotes: 1

Views: 6983

Answers (3)

Eli Burke
Eli Burke

Reputation: 2789

As of iOS 6 (and I believe 5) you can adjust playback rate within AVAudioPlayer; no extra code necessary:

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:
              [NSURL fileURLWithPath:path] error:&err];
    player.enableRate=YES;
    player.rate = 0.5;
    [player play];

Rate can range between .5 and 2.0, and can be adjusted during playback as long as enableRate is set to YES before playback starts.

Upvotes: 5

invalidname
invalidname

Reputation: 3185

A cheesy way to do it is to tweak the sample rate when you send it to the playback engine (Audio Queue, Remote I/O Unit, OpenAL). For PCM -- and I'm not sure this would work for anything other than PCM (so you'd have to decompress an MP3 or AAC with Audio Converter Services first) -- you could speed up your audio by adjusting the AudioStreamBasicDescription like this:

audioStreamDesc.mSampleRate = audioStreamDesc.mSampleRate * 1.2;

Note that this also changes the pitch of your audio: not only is it faster, it's also higher pitched. The Mac has a system-supplied audio unit that allows you to change playback speed without changing pitch, but it seems to be absent on iPhone.

Upvotes: 5

Gordon Childs
Gordon Childs

Reputation: 36169

AVAudioPlayer doesn't give you speed control, but it does let you set the position, so you could do a poor man's speed up/reverse the same way QuickTime Player does: by jumping through the file and playing small snippets at normal speed.

Or you decompress the samples yourself with an offline AudioQueue and do whatever rate you want. That's what I do.

Upvotes: 6

Related Questions