2012-12-17 33 views
1

如何使用同一段代码制作play/Pause按钮。使用相同的按钮播放/暂停

- (IBAction)min:(id)sender 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1min" ofType:@"mp3"]; 
    AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
        theAudio.delegate = self; 
        theAudio.numberOfLoops = -1; 
        [theAudio play]; 
        [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; 
} 

我该如何恢复相同的按钮?

回答

7

使用此识别按钮的状态:

在.h文件中作出theAudio声明:

AVAudioPlayer *theAudio; 

在你的方法:

UIButton *button = (UIButton *)sender; 

button.selected = !button.selected; 

if(button.selected) 
{ 
    // Play 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1min" ofType:@"mp3"]; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
    [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; 
} 
else 
{ 
    // Pause 
    [theAudio pause]; 
} 
+0

能请写//播放更多的细节和//暂停部分? –

+0

@JangSejin试试我的更新回答.. –

+0

非常感谢你 –

0

创建一个布尔值,如buttonIsPlayButton。如果按钮是播放按钮,则在开始时将其设置为true。然后当按下按钮时,将其设置为false。每次按下按钮时,您都应该根据布尔值更改图像。

相关问题