How to play a wav file placed in Documents directory in OSX using objective c - objective-c

I have been trying to play an audio file but unable to succeed.
SoundFilePath:
/Users/admin/Library/Containers/com.abc.myApp/Data/Documents/MyAudioSample1.wav
NSSound:
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:soundFilePath byReference:YES];
sound.volume = 1;
sound.delegate = self;
[sound play];
Only once out of 5 times my audio is played.
AVFoundation:
NSURL* file = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[audioPlayer prepareToPlay];

Add [audioPlayer play];
You are preparing to play but not playing it. After adding above line your code will be:
NSURL* file = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:file error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];
Hope it helps

Related

AVAudioPlayer Play/Pause/Stop local audio file on three different buttons

I want to play/pause/stop local audio file. Buttons for all functionalities are different. Check attached screenshot for reference.
Below is my click events,
-(IBAction)click_Play:(id)sender {
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Here, I am facing issue in pause functionality. After pausing the audio when I click on play button again. It start playing from starting and not at the point where I pause it. Any solution?
Thanks in advance!
Change your code to this:
-(IBAction)click_Play:(id)sender {
if (!player)
{
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
[player play];
}
else {
[player play];
}
}
Don't create instance of AVAudioSession and AVAudioPlayer again and again in play button action. So, keep it simple, create instance of AVAudioSession and AVAudioPlayer in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path;
NSURL *url;
//where you are about to add sound
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
path =[[NSBundle mainBundle] pathForResource:#"MySong" ofType:#"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player prepareToPlay];
}
-(IBAction)click_Play:(id)sender {
[player play];
}
-(IBAction)click_Pause:(id)sender {
[player pause];
}
-(IBAction)click_Stop:(id)sender {
[player stop];
}
Edit:
Call this method when you what to change track. I'll never recommend to create instance of object again and again
-(void)changeTrackWithPath:(NSString *)path{
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
[player replaceCurrentItemWithPlayerItem:playerItem];
[player play];
}

AVAudioPlayer on OS X not playing any sound

I'm trying to play a sound using AVAudioPlayer on os x (not iOS!). I'm using the following code:
AVAudioPlayer *player;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
soundFileURL error:&err];
if( err ){
NSLog(#"Failed with reason: %#", [err localizedDescription]);
}
else{
player.delegate = self;
player.numberOfLoops = -1;
player.currentTime = 0;
player.volume = 1.0;
[player play];
}
All is ok, and the player calls play function without the errors. However I can't hear any sound after that. I'm sure that my audio file is ok, because I was able to play it using NSSound class. The reason I want to use AVAudioPlayer is that when I'm using NSSound class and I play it in a loop there is short pause in between and that's why I wanted to try AVAudioPlayer.
Define the *player as a property so it gets not evaporated after your method finishes.

MP3 file not playing in Iphone/Ipad

I am palying MP3 files in my application using code
NSString *filePath = [[NSBundle mainBundle] pathForAuxiliaryExecutable:#"congratulation.MP3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[audioPlayer prepareToPlay];
audioPlayer.currentTime = 0;
[audioPlayer play];
I am playing 15 MP3 files
All playing well, but one file is not playing it is playing very well i itunes but not in my app
Any solution for this strange problem.
Amit Battan
I have to replace the MP3 files

Audio playback does not start

NSError *err;
// Initialize audio player
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
audioPlayer.delegate = self;
[audioPlayer play];
With the code above, I'm trying to initialize playback of a .mp3 file, however the playback does not start at all. There is no sound. What am I doing wrong? I have inspected 'err' and there is nothing there.
Edit: After adding AVAudioSession, I'm getting the following error from AVAudioPlayer
The operation couldn’t be completed. (OSStatus error -43.)
Apparently AVAudioPlayer does not support streaming via. HTTP as I was trying to do, so by using AVPlayer instead, I got it working.
Did you initialize a AVAudioSession?
The following works fine for me. Might give you a hint of whats not working for you.
Initializing:
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:nil];
[session setActive:TRUE error:nil];
Loading:
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer prepareToPlay];
audioPlayer.volume = 1.0;
Playing:
[audioPlayer play];
Update
To find the right NSRUL for fileUrl, I used this code:
NSURL* fileUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"MySound" ofType:#"wav"] isDirectory:NO];
And then I added MySound.wav to the project (bundle).

Cocoa Touch - Loading AVAudioPlayer

I have this code to play a sound but the first time you play it it takes a good 5 seconds to load...
How can I speed this up?
-(IBAction)playSound{ //play the cricket noise
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
You can try by putting the following section of your code in viewDidLoad so that the audioPlayer instance is ready to play before it is asked to actually play the audio:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;