AVAudioPlayer iOS Error - ios

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?

Related

MP3 file from server is not playing with AVAudioPlayer in iOS7

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)

AVAudioPlayer dose not play sound, gives error

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.

Having trouble with audio timing

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

AVAudioPlayer initWithURL not working to stream radio

I'm trying to stream an radio in my app.
I'm using AVAudioPlayer, but I have an error I don't understand :
NSString *urlString = #"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:myURL error:&error];
if (self.audioPlayer == nil){
NSLog(#"Error loading clip: %#", [error localizedDescription]);
}
else {
self.audioPlayer.delegate = self;
self.audioPlayer.volume = 0.5;
[self.audioPlayer play];
}
And I have the following error :
Error loading clip: The operation couldn’t be completed. (OSStatus error -43.)
Does anyone see what's wrong ?
EDIT :
This is what the Apple docs say:
The AVAudioPlayer class does not provide support for streaming audio
based on HTTP URL's. The URL used with initWithContentsOfURL: must be
a File URL (file://). That is, a local path.
Now what ? :(
download file to document directoty then play file
NSString *urlString = #"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];
NSData *audioData = [NSData dataWithContentsOfURL:myURL];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#.mp3", docDirPath , fileName];
[audioData writeToFile:filePath atomically:YES];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (self.audioPlayer == nil) {
NSLog(#"AudioPlayer did not load properly: %#", [error description]);
} else {
self.audioPlayer.delegate = self;
self.audioPlayer.volume = 0.5;
[self.audioPlayer play];
}

how can i play remote .mp3 file in my ios application?

I'm trying to play remote .mp3 file
but its giving the following error.
The operation couldn’t be completed. (OSStatus error -43.)
here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
isPlaying = NO;
/*
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"Dar-e-Nabi-Per" ofType:#"mp3"]];
*/
NSURL *url = [NSURL
URLWithString:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
}
else
{
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
}
}
Can anyone please guide me where i'm doing mistake
For steaming audio from a remote server, use AVPlayer instead of AVAudioPLayer.
AVPlayer Documentation
Sample Code:
- (void)playSampleSong:(NSString *)iSongName {
NSString *aSongURL = [NSString stringWithFormat:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
// NSLog(#"Song URL : %#", aSongURL);
AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
[anAudioStreamer play];
// Access Current Time
NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);
// Access Duration
NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);
}
Use this to play song from URL
NSData *songData=[NSData dataWithContentsOfURL:url];
self.player = [[AVAudioPlayer alloc] initWithData:songData error:nil];
self.player.numberOfLoops=0;
_player.delegate=self;
[_player prepareToPlay];
[_player play]

Resources