Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

iphone - toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause

Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemPlay 
                               target:self 
                               action:@selector(playaudio:)];

systemItem1.style = UIBarButtonItemStyleBordered;

UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] 
                               initWithBarButtonSystemItem:UIBarButtonSystemItemPause
                               target:self
                               action:@selector(pause:)];

systemItem2.style = UIBarButtonItemStyleBordered;


// flex item used to put space in between buttons

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] 
                             initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                             target:nil
                             action:nil];

//Add buttons to the array

NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, modalBarButtonItem, nil];

 [toolbar setItems:toolbarItems];

[settingsButton release];
[systemItem  release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[modalBarButtonItem release];
[flexItem release];

-(void) playaudio: (id) sender {

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme" 
                                                     ofType:@"mp3"];

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

audioPlayer = [[AVAudioPlayer alloc] 
                    initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[audioPlayer play];
[fileURL release];  

}

- (void)pause: (id)sender {

if

([audioPlayer isPlaying])

{[audioPlayer pause];} 

else 

{[audioPlayer play];}

}

Any ideas how i can do that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Essentially, every time the play/pause status is updated, you're going to want to run a method to update the toolbar. Something like this should work. You can create a method like this:

-(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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...