Wednesday, June 5, 2013

AVAudioPlayer rate example in Objective C (iOS).


AVAudioPlayer rate

The audio player’s playback rate.

@property float rate

Discussion of [AVAudioPlayer rate]
This property’s default value of 1.0 provides normal playback rate. The available range is from 0.5 for half-speed playback through 2.0 for double-speed playback.

To set an audio player’s playback rate, you must first enable rate adjustment as described in the enableRate property description.

AVAudioPlayer rate example.
_noticeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Rooster-mono" ofType:@"wav"]]
                                                      error:nil];;
if ([_noticeAudio respondsToSelector:@selector(setEnableRate:)])
    _noticeAudio.enableRate = YES;
if ([_noticeAudio respondsToSelector:@selector(setRate:)])
    _noticeAudio.rate = 2.0;

Example of [AVAudioPlayer rate].
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;
        [player play];

AVAudioPlayer rate example.
- (void)viewWillAppear:(BOOL)animated {
        MyAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
     if (appDelegate.myAudioPlayer.rate == 1.0){

     UIImage *img = [UIImage imageNamed:@"Pause_BT.png"];
     [control setImage:img forState:UIControlStateNormal];

     } else if (appDelegate.myAudioPlayer.rate == 0.0) {

     UIImage *img = [UIImage imageNamed:@"Play_BT.png"];
     [control setImage:img forState:UIControlStateNormal];
     }

End of AVAudioPlayer rate example article.