0

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.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
brno792
  • 6,479
  • 17
  • 54
  • 71

5 Answers5

6

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.)"
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • @brno792, did you actually get it to work? If so, how? I'd like to know. – rdelmar Apr 22 '13 at 05:29
  • I didnt have @property so maybe that was it. Or I had error=nil instead of error=&error, so maybe that was also it. It works now so thanks. Im still using the .wave too. If I wanted to randomly select an audio file to play from 3 or 4 when I tap the button, what should I do? thanks. – brno792 Apr 22 '13 at 05:32
  • @brno792, yeah, I've seen some posts about the audio player being deallocated if you don't have a strong property, and you're using ARC. The error parameter wouldn't cause the problem. I do have the strong property, and mine still doesn't work. – rdelmar Apr 22 '13 at 05:34
1

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

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

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.

Balu
  • 8,470
  • 2
  • 24
  • 41
  • just for method invokation iboutlet is not compulsory instead proper event should trigger. – βhargavḯ Apr 22 '13 at 04:20
  • Thanks, so I tried your solution. I had to add AVAudioPlayer* avPlayer = .... I also checked IBOutlet, I have Received Actions: playAudio: Button Touch Up Inside. So I think its connected. Im still not getting any sound though. – brno792 Apr 22 '13 at 05:01
  • once check your audio file is there any music or not/? – Balu Apr 22 '13 at 05:03
  • once check this http://stackoverflow.com/questions/9963230/sound-not-playing-with-avaudioplayer – Balu Apr 22 '13 at 05:11
0

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.

0

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)
    }
Tristan
  • 3,058
  • 6
  • 40
  • 68
Fred
  • 332
  • 4
  • 5