Friday, May 31, 2013

UIBarButtonItem UIBarButtonSystemItemPause example in Objective C (iOS).


UIBarButtonItem UIBarButtonSystemItemPause


UIBarButtonSystemItem
Defines system-supplied images for bar button items.

typedef enum {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo, // iOS 3.0 and later
UIBarButtonSystemItemRedo, // iOS 3.0 and later
UIBarButtonSystemItemPageCurl, // iOS 4.0 and later
} UIBarButtonSystemItem;

UIBarButtonItem UIBarButtonSystemItemPause example.
-(void)playPause{
      if(audioPlayer == nil){
          NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"mp3"];
          NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
          audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
          audioPlayer.currentTime = 0;
          [fileURL release];
      }
      UIBarButtonSystemItem buttontype = UIBarButtonSystemItemPlay;
      if([audioPlayer isPlaying]){
          [audioPlayer pause];
      }
      else {
          [audioPlayer play];
          buttontype = UIBarButtonSystemItemPause;
      }
      UIBarButtonSystemItem *item = [[[UIBarButtonItem alloc]
                           initWithBarButtonSystemItem:buttontype
                           target:self
                           action:@selector(playPause)] autorelease];
      self.toolbar.items = [NSArray arrayWithObject:item];
}

Example of [UIBarButtonItem UIBarButtonSystemItemPause].
- (void) setStartStopButton:(BOOL)startorstop
{
    UIBarButtonItem *startStopButton = nil;
    if (startorstop == YES) {
        startStopButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(startStopAction:)];
    }
    else
    {
        startStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(startStopAction:)];
    }
    self.navigationItem.rightBarButtonItem = startStopButton;
    [startStopButton release];
}

UIBarButtonItem UIBarButtonSystemItemPause example.
            buttonToRemove = self.playButton;
            self.playButton = nil;
            self.pauseButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause
                                                                             target:self
                                                                             action:@selector(pauseAudio:)];
            buttonToAdd = self.pauseButton;

End of UIBarButtonItem UIBarButtonSystemItemPause example article.