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)
Related
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 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.
I’m trying to play a sound using the AVAudioPlayer without success. I’ve tried it both in the iOS Simulator and on my iPhone. I don’t get any errors, but I also don’t hear a sound.
Here’s my code:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: #"RiggerSound_Click" ofType: #"wav"];
NSLog( #"Path is %#", soundFilePath );
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error;
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: &error];
[fileURL release];
if (newPlayer != nil)
{
if( ![newPlayer play] )
NSLog( #"Error playing" );
[newPlayer release];
}
else
NSLog(#"Error creating audioPlayer: %#", [error localizedDescription]);
The soundFilePath is “/Users/myname/Library/Application Support/iPhone Simulator/6.1/Applications/E50059DD-B328-45C7-A5D5-C650F415B183/appname.app/RiggerSound_Click.wav”.
The “newPlayer” variable is not null and [newPlayer play] does not fail.
The file “RiggerSound_Click.wav” is shown in the Resources folder of my project. I can select it in the project, click the Play button, and hear the sound.
I’m stumped.
Thanks for your help.
Set the AVAudioPlayer's delegate before playing. For example,
newPlayer.delegate = self;
Also, make sure your delegate's class conforms to the AVAudioPlayerDelegate protocol:
MyClass : Superclass <AVAudioPlayerDelegate>
Just define the AVAudio Player as static
static AVAudioPlayer *newPlayer = Nil;
if(newPlayer == Nil)
{
newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: &error];
}
That's it
Hi I have a setup that records audio (with AVAudioSessionCategoryPlayAndRecord category), now I want to playback and MP3 file as usual with AVAudioPlayer instance, but no any sound can be hear.
AVAudioPlayer do not throw any error, nor in its delegate callbacks.
Has anybody similar experience? How to overcome this?
Please use this -
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/%#.mp3", docDirPath , #"Welcome"];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (avPlayer == nil)
{
NSLog(#"AudioPlayer did not load properly: %#", [error description]);
}
else
{
[avPlayer play];
avPlayer.delegate=self;
}
And tell me if it is working or not.
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?