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

同じボタンで再開するにはどうすればよいですか?

4

2 に答える 2

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];
}
于 2012-12-17T08:30:42.533 に答える
0

などのブール値を作成しますbuttonIsPlayButton。ボタンが再生ボタンの場合は、最初にtrueに設定します。次に、ボタンが押されたら、falseに設定します。ブール値に基づいて、ボタンを押すたびに画像を変更する必要があります。

于 2012-12-17T08:30:10.277 に答える