iOS按钮不在按钮点击上播放音频文件

时间:2013-04-22 04:15:43

标签: ios iphone objective-c xcode uibutton

这是一个新手问题。

我有一个非常简单的应用程序,当点击主视图上的按钮时,该应用程序只能播放音频文件。

我正在使用XCode版本4.6。

我在我的项目中添加了一个音频文件'audioclip.wav'。我添加了AVFoundation.framework

我的项目只有ViewController.hViewController.m

我双击并从故事板中的按钮拖动到.h文件,使用助理编辑器创建我的IBAction连接。

我的ViewController.h:

#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>

@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
}
- (IBAction)playAudio:(id)sender;

@end

我的ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)playAudio:(id)sender {
    AVAudioPlayer *audioPlayer;
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"audioclip" ofType:@"wav" ];
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    [audioPlayer play];
}

@end

无论出于何种原因(可能是我遗漏的东西)当我点击按钮时,audioclip.wav文件无法播放。我已经在iPhone 6.1模拟器和我的设备上进行了测试。

5 个答案:

答案 0 :(得分:6)

我认为wav文件存在一些问题。有些人似乎让它工作,但我尝试了几个不同的文件,但没有一个播放。在下面的代码中注释掉的mp3和m4a文件运行正常。没有必要对委托做任何事情,它是可选的。

    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>

    @interface ViewController ()
    @property (strong,nonatomic) AVAudioPlayer *audioPlayer;
    @end

    @implementation ViewController


    - (IBAction)playAudio:(id)sender {
        //NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"11 Prayer" ofType:@"mp3" ];
       //NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"Sunshine" ofType:@"m4a" ];
        NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"space" ofType:@"wav" ];
        NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
        NSError *error;
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
        [self.audioPlayer play];
    }

如果我记录错误,请使用wav文件:

Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn’t be completed. (OSStatus error 1685348671.)"

答案 1 :(得分:1)

请尝试使用这个..

- (IBAction)playAudio:(id)sender
{
     NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"audioclip" ofType:@"wav"]];
    NSData *data =[NSData dataWithContentsOfURL:url];
    audioPlayer  = [[AVAudioPlayer alloc] initWithData:data error:nil];
    audioPlayer.delegate = self;
    [audioPlayer setNumberOfLoops:0];

    [audioPlayer play];
}

我希望这会对你有所帮助

答案 2 :(得分:0)

尝试这样,

     NSString *audio=[[NSBundle mainBundle]pathForResource:@"audioclip" ofType:@"wav"];
    NSURL *url=[[NSURL alloc]initFileURLWithPath:audio];
    avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    avPlayer.delegate = self;
    [avPlayer prepareToPlay];
    [avPlayer play];

并检查IBOutlet是否已连接,并使用断点检查按钮操作方法。

答案 3 :(得分:0)

正如我所看到的,你已经使代码变得非常简单,所以它的制作方式要求它不是一个大的wav文件。

然后你需要将音频文件减少到10秒以下。

答案 4 :(得分:0)

当我遇到这个问题时,我正在使用.m4a。对我来说,没有实现AVAudioSession就是问题

var session = AVAudioSession.sharedInstance()

    do {
        session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try session.setActive(true)
    } catch {
        gf.displayAlert("problem encountered", userMessageTitle: "")
        self.navigationController?.popViewControllerAnimated(true)
    }
相关问题