Search code examples
iosobjective-caudioavqueueplayertempo

AVQueuePlayer set Tempo of audio


I am using AVQueuePlayer for playing multiple audio which are saved locally.

For reference please check below code,

for (int j = 0; j < arrPlayData.count; j++) {
                  
    path =[[NSBundle mainBundle] pathForResource:[[arrPlayData objectAtIndex:j] valueForKey:AUDIO_NAME] ofType:@"wav"];
    item = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
    if (_queuePlayer == nil) {
        _queuePlayer = [[AVQueuePlayer alloc] initWithPlayerItem:item];
     } else{
        [_queuePlayer insertItem:item afterItem:nil];
     }
}
[_queuePlayer setVolume:1.0];
[_queuePlayer play];

This is working fine,

Now I want to apply tempo for generated audio between 0 to 240 as shown in below image.

enter image description here

I am aware about the setRate: property and applied it as below,

[_queuePlayer setRate:sldValue/240.0];

Here, sldValue is selected value from above slider. Here if I select 240 from slider then rate becomes 1(Which plays audio in normal form - Original one). And as I select down value from slider then it will slow down the audio speed.

My query is how to increase audio speed then normal play. Or how this tempo exactly working?

Any help is really appreciated. Thanks in advance!


Solution

  • [_queuePlayer setRate:sldValue/245.0];
    

    Here, sldValue is selected value from above slider. Here if I select 240 from slider then rate becomes 1(Which plays audio in normal form - Original one). And as I select down value from slider then it will slow down the audio speed.

    That's because that's exactly what you're asking for. Think about it. If the slider is at 240, then sldValue/245.0 is approximately 1, which means normal, just as you say. And if you slide down, let's say to 120, then sldValue/245.0 is approximately 1/2, which is half speed.

    The program is doing exactly what you are telling it to do. If that isn't what you want, then don't say that!

    Also, you will need to set the audioTimePitchAlgorithm of your player items appropriately, or you won't be able to achieve a full range of rates. See: AVPlayer rate property does not work?