0

私はXcodeの初心者であり、xcodeをマスターした人からの経験が必要です。UIActionSheetで音楽を再生、一時停止、停止しようとしていますが、[一時停止]と[停止]ボタンをクリックしても停止しないか、[再生のみを一時停止]ボタンが機能します。誰かが助けてくれますか?これが私のコードです。

 #import <AVFoundation/AVFoundation.h>
 @interface MainViewController () <UIActionSheetDelegate,AVAudioPlayerDelegate>
 @property(nonatomic,retain)AVAudioPlayer *playAudio;
 @end

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle =[actionSheet buttonTitleAtIndex:buttonIndex];
    NSURL *url =[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/MusicName.mp3",[[NSBundle mainBundle] resourcePath]]];
    NSError *error;

    self.playAudio=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];

    if (buttonIndex==0) {

        [self.playAudio play];

    }
    if ([buttonTitle isEqualToString:@"Pause"]) {

        [self.playAudio pause];
    }
    if (buttonIndex==2) {
        [self.playAudio stop];
    }
}
- (IBAction)Music:(id)sender {

    UIActionSheet *musicSheet =[[UIActionSheet alloc]initWithTitle:@"Choose" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Play",@"Pause",@"Stop", nil];
    [musicSheet showInView:self.view];


}

。私はベトナム人なので、英語のスキルがあなたに私の言うことを理解するのを難しくすることができます。お時間をいただきありがとうございます。お返事をお待ちしております

4

1 に答える 1

0

まず第一に、ボタンのタイトルに頼るべきではありません。ボタン インデックスを使用して、どのボタンが押されているかを識別します。これがその目的です。

2 番目: ボタンのインデックスは正しいですか? switch ステートメントを使用してみてください。

そのような :

switch (buttonIndex) {
        case 0:
            [self.playAudio play];
            break;
        case 1:
            [self.playAudio pause];
            break;
        case 2:
            [self.playAudio stop];
            break;
        default:
            break;
}
于 2013-03-10T08:24:55.453 に答える