我創建了兩個UIButton,並且只有播放按鈕出現在UIToolbar上。在我想要的播放方式中,當播放按鈕被按下時,它顯示暫停按鈕,並且如果用戶按下暫停按鈕,則暫停音頻文件,然後顯示播放按鈕。播放/暫停之間切換UIButtons
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
playButton.frame = CGRectMake(0, 0, 50, 50);
UIImage *image = [UIImage imageNamed:@"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playButton];
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pauseButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
pauseButton.frame = CGRectMake(0, 0, 50, 50);
UIImage *imge = [UIImage imageNamed:@"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
-(void)play:(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];
}
我需要協助執行這項
在劇中方法
我做
[audioPlayer play];
UIImage *imge = [UIImage imageNamed:@"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
[audioPlayer pause];
UIImage *image = [UIImage imageNamed:@"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
[audioPlayer Play];
我覺得我在做錯誤的方式
請指點。
感謝您的幫助。
感謝
這是一個有點混亂,你有兩個按鈕。你是否試圖讓戲劇和停頓互相替換? ['>'更改爲'||']? – nycynik 2012-02-01 22:25:42
是的,當播放按鈕被擊中時,它應該播放音樂並用暫停按鈕替換播放按鈕,當暫停按鈕被擊中時,它應該暫停音樂,並且應該顯示播放按鈕 – user1120133 2012-02-01 22:34:13