Thursday, June 6, 2013

AVAudioPlayer play example in Objective C (iOS).


AVAudioPlayer play

Plays a sound asynchronously.

- (BOOL)play

Return Value
Returns YES on success, or NO on failure.

Discussion of [AVAudioPlayer play]
Calling this method implicitly calls the prepareToPlay method if the audio player is not already prepared to play.

AVAudioPlayer play example.
-(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;
}

}

Example of [AVAudioPlayer play].
- (void) playaudio: (id) sender
{
   if(self.audioPlayer == nil) {
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Theme"
                                                 ofType:@"mp3"];  
      NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];  

      self.audioPlayer = [[AVAudioPlayer alloc]
                initWithContentsOfURL:fileURL error:nil];

      self.audioPlayer.currentTime = 0; //this could be outside the if if you want it to start over when they hit play
   }
   [self.audioPlayer play];
}

- (void)pause: (id)sender

{
   if([audioPlayer isPlaying]){
       [audioPlayer pause];
   } else {
       [audioPlayer play];
   }

}

- (void)stop: (id)sender

{

  [audioPlayer stop];

 }

AVAudioPlayer play example.
- (IBAction)play:(id)sender {
  //songIsCurrentlyPaused = NO;
  if(songIsCurrentlyPaused==YES){
    [self.background play];
  } else {
    playQueue = dispatch_queue_create("volume_change", NULL);
    dispatch_async(playQueue, ^{ NSString *filePath =
      [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
      NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
      self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
      self.background.delegate = self;
      [self.background setNumberOfLoops:1];
      [self.background setVolume:0.5];
      [self.background play]; });

      [trackNameLabel setText:@"Currently playing :\n some_song"];
      self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
    }
}

- (IBAction)pause:(id)sender {
  songIsCurrentlyPaused = YES;
  [self.background pause];
  [trackNameLabel setText:@"Currently playing : some_song (paused)"];
  [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
}

End of AVAudioPlayer play example article.