gpt4 book ai didi

iphone - 在 UIBarButtonSystemItemPlay 和 UIBarButtonSystemItemPause 之间切换

转载 作者:行者123 更新时间:2023-12-03 19:32:01 26 4
gpt4 key购买 nike

有两个 UIBarButtonItems 想要将其作为一个 UIBarButtonItem 并在它们之间切换

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

}

有什么想法我可以做到这一点。

最佳答案

本质上,每次更新播放/暂停状态时,您都需要运行一个方法来更新工具栏。像这样的东西应该有效。您可以创建这样的方法:

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

关于iphone - 在 UIBarButtonSystemItemPlay 和 UIBarButtonSystemItemPause 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com