Play Audio iOS Objective-C

I'm trying to get an audio file playing in my iOS app. This is my current code

NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.m4a", [[NSBundle mainBundle] resourcePath]];

NSLog(@"%@",soundFilePath);
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[audioPlayer setVolume:1.0];

audioPlayer.delegate = self;

[audioPlayer stop];
[audioPlayer setCurrentTime:0];
[audioPlayer play];

I've being checking a bunch of other posts but they all generally do the same thing. I'm not getting an error, but I don't hear any audio playing on the simulator or device.

This is the path I get for the audio file on the simulator

/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/long-numbered-thing/BookAdventure.app/test.m4a

Any help is appreciated!


Solution 1:

Maybe you should try using the NSBundle method pathForResource:ofType: method to create the path to the file.

The following code should successfully play an audio file. I have only used it with an mp3, but I imagine it should also work with m4a. If this code does not work, you could try changing the format of the audio file. For this code, the audio file is located in the main project directory.

/* Use this code to play an audio file */
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test"  ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite

[player play];

EDIT

Ok, so try this:

NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.m4a",[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite

[player play];

Also, make sure you do the proper imports:

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

Solution 2:

You need to store a strong reference to 'AVAudioPlayer'

@property (strong) AVAudioPlayer *audioPlayer;

And if you are sure that your audio file is in following Bundle Resources, it should play.

Project>Build Phases>Copy

Solution 3:

To play file with extension .caf, .m4a, .mp4, .mp3,.wav, .aif


Download following two files from GitHub

SoundManager.h
SoundManager.m

Add these files to your project

Also add audio files to resource folder (sample files)

mysound.mp3, song.mp3

Import header file in desired file

#import "SoundManager.h"

And add following two lines in -(void)viewDidLoad

[SoundManager sharedManager].allowsBackgroundMusic = YES;
[[SoundManager sharedManager] prepareToPlay];

Use following line to play sound (mysound.mp3)

[[SoundManager sharedManager] playSound:@"mysound" looping:NO];

Use following line to stop the sound (mysound.mp3)

[[SoundManager sharedManager] stopSound:@"mysound"];

Also you can play music (song.mp3) as

[[SoundManager sharedManager] playMusic:@"song" looping:YES];

And can be stop music as

[[SoundManager sharedManager] stopMusic];

Complete Documentation is on GitHub

Solution 4:

First import this framework to your file

#import <AVFoundation/AVFoundation.h>

Then declare an instance of AVAudioPlayer.

AVAudioPlayer *audioPlayer;

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Name of your audio file" 
                                                          ofType:@"type of your audio file example: mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];