Wednesday, June 5, 2013

AVAudioPlayer playing example in Objective C (iOS).


AVAudioPlayer playing

A Boolean value that indicates whether the audio player is playing (YES) or not (NO). (read-only)

@property(readonly, getter=isPlaying) BOOL playing

Discussion of [AVAudioPlayer playing]
To find out when playback has stopped, use the audioPlayerDidFinishPlaying:successfully: delegate method.

Important: Do not poll this property (that is, do not use it inside of a loop) in an attempt to discover when playback has stopped.

AVAudioPlayer playing example.
//Pause the player and restart it
if (player.playing) {
    NSLog(@"Reset sound: %@", selectedSound);
    [player pause];
}

player.currentTime = 0;
[player play];

Example of [AVAudioPlayer playing].
-(void)PlayStop{

if (playing==NO) {
 // Init audio with playback capability
    [play setBackgroundImage:[UIImage imageNamed:@"hmpause.png"] forState:UIControlStateNormal];

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:______ error:&err];
    [audioPlayer prepareToPlay];

    audioPlayer.delegate=self;
    [audioPlayer play];

    playing=YES;
}
else if(playing==YES){
    [play setBackgroundImage:[UIImage imageNamed:@"Audioplay.png"] forState:UIControlStateNormal];

    [audioPlayer pause];

    playing=NO;
}

}

AVAudioPlayer playing example.
- (IBAction)Beat {
    if (audioPlayer.playing == NO) {
        // Audio player is not playing.
        // Set the button title here to "stop"...
        [self startPlaybackForPlayer:audioPlayer];
    }else {
        // Audio player is playing.
        // Set the button title here to "play"...
        [self pausePlaybackForPlayer:audioPlayer];
    }

}

End of AVAudioPlayer playing example article.