【问题标题】:play , pause , stop music with UIActionSheet使用 UIActionSheet 播放、暂停、停止音乐
【发布时间】:2013-03-10 08:15:22
【问题描述】:

我是 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];


}

。我是越南人,所以我的英语技能会让你很难理解我说的话。感谢您的时间 。期待看到你的回复

【问题讨论】:

    标签: uiactionsheet audio-player


    【解决方案1】:

    首先:你不应该依赖按钮标题。使用按钮索引来识别按下的是哪个按钮,这就是它的用途。

    第二: 你确定你的按钮索引是正确的吗?您可以尝试使用 switch 语句。

    如:

    switch (buttonIndex) {
            case 0:
                [self.playAudio play];
                break;
            case 1:
                [self.playAudio pause];
                break;
            case 2:
                [self.playAudio stop];
                break;
            default:
                break;
    }
    

    【讨论】:

    • 感谢您的回答,我会尝试,然后我会告诉你
    • ...即使我使用 switch 语句仍然无法正常工作。我必须为每个 buttonIndex 设置委托吗?我试图在 switch 语句之前设置委托,但它仍然不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多