简体   繁体   中英

iOS button not playing audio file on button tap

This is a newbie question.

I have a very simple app that is supposed to only play an audio file when a button on the main view is tapped.

I am using XCode version 4.6.

I added an audio file 'audioclip.wav' to my project. I have added the AVFoundation.framework .

My project only has ViewController.h and ViewController.m .

I double clicked and dragged from the button in the storyboard to the .h file using the assistant editor to create my IBAction connection.

My ViewController.h:

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

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

@end

My 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

For whatever reason (probably something silly I left out) The audioclip.wav file does not play when I click the button. I have tested on the iPhone 6.1 simulator and on my device.

I think there is some problem with wav files. Some people seem to get it to work, but I've tried several different files and none of them play. The mp3 and m4a files that are commented out in the code below worked fine. There's no need to do anything with a delegate, it's optional.

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

If I log the error, with the wav file I get:

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

Please try to use this one..

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

i hope this will help u

try like this ,

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

and once check IBOutlet connected or not and check the button action method with breakpoints.

As i can see you have made the code quite simple, so the way it's made does require that it is not a big wav file.

And by that you need to cut down the audio file to something like 10 seconds and under.

I was using the .m4a when I encountered this problem. For me, not implementing AVAudioSession was the issue

var session = AVAudioSession.sharedInstance()
    do {
        session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try session.setActive(true)
    } catch {
        gf.displayAlert("problem encountered", userMessageTitle: "")
        self.navigationController?.popViewControllerAnimated(true)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM