I am trying to play an MP3 file, however I am having problems with my code. This is what I have:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (!audioPlayer) {
NSLog(#"localizedDescription : %#", [error localizedDescription]);
} else {
[audioPlayer play];
}
and this is the error I am reciving
localizedDescription : The operation couldn’t be completed. (OSStatus error -43.)
How do I stop this from happening? I want to play my sound once then stop it, but it can't even start.
The issue is probably caused by the path being invalid.
Try building it as follows
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"audiofile" ofType:#"mp3"];
NSURL *soundURL = [NSURL URLWithString:soundPath];
instead.
Also make sure that audiofile.mp3 is included in the current target.
Related
Here is my code, In this first I am calling mp3 url for downloading the audio data. then I am saving that data in local directory, After that I am playing audio from local file path but its not working AVAudioPlayer throwing error. I have tried some other ways also but no one work for me.
On simulator same code is working fine.
And the audio url is playing like a charm in browser.
NSURL *urlll=[NSURL URLWithString:#"www.xyz.audio.mp3"];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:urlll] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responce,NSData* data,NSError * error) {
if (error == nil)
{
indicator.hidden=YES;
[indicator stopAnimating];
if (data)
{
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#.mp3", docDirPath , #"Audio"];
[data writeToFile:filePath atomically:YES];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
AudioPlayer.delegate=self;
[AudioPlayer prepareToPlay];
if (AudioPlayer == nil) {
NSLog(#"AudioPlayer did not load properly: %#", [error description]);
} else {
[AudioPlayer play];
}
}
}
}];
But I am getting these kind of errors : Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn't be completed. (OSStatus error 1685348671.)
Please Help me :( .
In your code you save the audio to NSString *filePath but you initiate with fileURL. Could it be that you actually wanted to do this:
AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:&error];
If not then you should know that the error is reporting to you a kAudioFileInvalidFileError. Which means the file is malformed. You could try other formats like .caf or .aifc or aiff.
--edit--
Make sure the AVAudioPlayer is a property in .m or .h file, so the memory allocation lasts after leaving the method:
#property (nonatomic,strong) AVAudioPlayer *AudioPlayer;
(side note: properties should start with lowercase -> audioPlayer)
I have a simple code to a play sound through AVAudioPlayer
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"beep"
ofType:#"mp3"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.player.volume = 1;
[self.player prepareToPlay];
My exception breakpoint will stop at prepareToPlay and console log wrote:
ERROR: AVAudioSessionUtilities.h:111: GetProperty:
AudioSessionGetProperty ('prp?') failed with error: 'pty?
After 2x tapping to pass through this breakpoint, application will not crash. This happened only in the simulator.
Any suggestions?
Following code might be helpful.
NSURL *url;
url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/beep.mp3", [[NSBundle mainBundle] resourcePath]]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
self.audioPlayer .delegate = self;
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
I think its a Simulator specific error. This error doesnt come up on the device. There is a discussion here if you want to know more.
AVSpeechSynthesizer error AudioSession
I have the following code in my viewcontroller. When I run, the audio doesn't play in the xcode simulator. Any advice on what I should do?
Also, I've saved anomaly.mp3 in the same directory (no sub-folders) as ViewController.m.
#import <AVFoundation/AVFoundation.h> //before viewDidLoad
NSError * error = nil;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"anomaly"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error){
NSLog(#"error: %#", [error localizedDescription]);
}
NSLog(#"test");
[audioPlayer play];
Try setting the audio player's volume. You can also check the results of the "play" function, to see if things worked correctly. The play function implicitly calls prepareToPlay. This function can fail out for a number of reasons, irritatingly without any error object or message...
NSError * error = nil;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"anomaly"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error){
NSLog(#"error: %#", [error localizedDescription]);
} else {
audioPlayer.volume = 50;
if([audioPlayer play]) NSLog(#"Should be playing");
else NSLog(#"Something weird happened"); //usually related to unavailable hardware
}
Keep a strong reference to the audioPlayer by declaring it as:
#property (nonatomic) AVAudioPlayer *audioPlayer;
and then use self.audioPlayer wherever you have used audioPlayer.
The problem with your code is that because your audioPlayer is just a local variable to the method, it gets deallocated by ARC as soon as your method is completed. Keeping a strong reference to it will keep the audioPlayer object and memory and your sound will keep playing as long as you don't call
[self.audioPlayer stop];
I am having trouble with my audio.
here is the code:
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle]pathForResource:#"Cheering" ofType:#"mp3"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
It will not play like this. The code steps through just fine but no audio. If I put a break on the last line and then step through it plays properly. I have tried everything I can to get this to work but have no clue where to go from here.
It is acting like it needs a delay before the play command.
Try this Sure It will Works
NSURL* url = [[NSBundle mainBundle] URLForResource:#"Cheering" withExtension:#"mp3"];
NSAssert(url, #"URL is valid.");
NSError* error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
if(!audioPlayer)
{
NSLog(#"Error creating player: %#", error);
} else
{
[audioPlayer play];
}
Did you try
[audioPlayer prepareToPlay]
before you actually try to play it?
https://developer.apple.com/library/ios/DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html#//apple_ref/occ/instm/AVAudioPlayer/prepareToPlay
I get an error trying to play a clip using AVAudioPlayer. The error gives me:
'The operation couldn’t be completed. (OSStatus error -50.)'
I have no idea. The frameworks are all there etc...
Is it something to do with the URL?
Here's my code:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"grunt" ofType:#"aiff"];
NSURL *fileURL = [NSURL URLWithString:soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if(!error){
audioPlayer.delegate = self;
[audioPlayer play];
}else{
NSLog(#"Error loading clip: %#", [error localizedDescription]);
}
Thanks in advance!
have you tried NSURL +fileURLWithPath?