Question

I'm struggling to follow Apple's documentation regarding playing back a small .wav file using the AVAudioPlayer class. I'm also not sure what toolboxes I need for basic audio playback. So far I have imported:

AVFoundation.framework
CoreAudio.framework
AudioToolbox.framework

Here is my code .h and .m:

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

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

- (IBAction)playAudio:(id)sender;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (IBAction)playAudio:(id)sender {

    NSLog(@"Button was Pressed");

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"XF_Loop_028" ofType:@"wav"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

    // create new audio player
    AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
    [myPlayer play];

}
@end

I don't seem to have any errors. However, I am not getting any audio.

Was it helpful?

Solution

You're having an ARC issue here. myPlayer is being cleaned up when it's out of scope. Create a strong property, assign the AVAudioPlayer and you're probably all set!

@property(nonatomic, strong) AVAudioPlayer *myPlayer;

...

// create new audio player
self.myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
[self.myPlayer play];

OTHER TIPS

Is the filePath coming back with a valid value? Is the fileURL coming back with a valid value? Also, you should use the error parameter of AVAudioPlayer initWithContentsOfURL. If you use it it will likely tell you EXACTLY what the problem is.

Make sure you check for errors and non-valid values in your code. Checking for nil filepath and fileURL is the first step. Checking the error parameter is next.

Hope this helps.

What I do is create an entire miniature class just for this purpose. That way I have an object that I can retain and which itself retains the audio player.

- (void) play: (NSString*) path {
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
    NSError* err = nil;
    AVAudioPlayer *newPlayer =
        [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: &err];
    // error-checking omitted
    self.player = newPlayer; // retain policy
    [self.player prepareToPlay];
    [self.player setDelegate: self];
    [self.player play];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top