1

基本的に、アプリにはオーディオファイルの再生ボタンがあり、別の UIButton を追加したい 再生ボタンのほかに、それが押されるたびにオーディオファイルが何度か繰り返されるか、オーディオ再生の繰り返しを停止するためにもう一度押されるまで無限になる可能性がありますファイル。どうすればこれを達成できますか。

以下は、再生/一時停止トグルボタンのコードです

_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
_playButton.frame = CGRectMake(80, 40, 25, 25);
[_playButton setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateNormal];
[_playButton setImage:[UIImage imageNamed:@"audiopause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:_playButton];

// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Theme"ofType:@"mp3"];

// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

//Initialize the AVAudioPlayer.
player = [[AVAudioPlayer alloc]
          initWithContentsOfURL:fileURL error:nil];
player.delegate = self;

- (void)playAction:(id)sender
{
    if([player isPlaying])
    {
        [sender setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateSelected];
        [player pause];
        //[timer invalidate];
        //timer = nil;
        //[self pauseTimer];
    }else{
        [sender setImage:[UIImage imageNamed:@"audiopause.png"] forState:UIControlStateNormal];
        [player play];
        slidertimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateProgressBar:) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:slidertimer forMode:NSRunLoopCommonModes];
        timer = slidertimer;
    }
}
4

2 に答える 2

0

私はついにこのようにそれを解決しました

 -(void) RepeatAction:(id)sender{

if (player && player.playing) {

    [player stop];

    player = nil;

    [_playButton setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateNormal];

    return;
}

// Get the file path to the song to play.

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

// Convert the file path to a URL.

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

//Initialize the AVAudioPlayer.

player = [[AVAudioPlayer alloc]
          initWithContentsOfURL:fileURL error:nil];

player.delegate = self;

    player.numberOfLoops = -1;

   [player play];

}

今直面している唯一の問題は、再生/一時停止トグルボタンがこれで邪魔されることです。それを修正する方法。

于 2013-02-07T19:45:31.513 に答える
0

必要なのは、x 秒ごとにアクションを実行することだけのようです。UIButton が実行しているコードをメソッドに移動し、そのメソッドを n 回実行する NSTimer ループを作成し、完了したら NSTimer を無効にすることができます。

于 2013-02-07T16:18:25.280 に答える