iphone dev mp3 file not playing using av audio player - iphone

i am trying to play an mp3 audio file, the file will be uploaded by user on web server and i will get the url of that file and then i have to play that file but i am not succeeding in it. here is my code
NSURL *url = [NSURL URLWithString:url1];
NSData *theData = [NSData dataWithContentsOfURL:url];
newPlayer = [[AVAudioPlayer alloc] initWithData:theData error:nil];
[newPlayer play];
the url is like this
http://mqm.designers99.com/Notification/mp3_files/1331551313_1919.mp3
Plz. guide me coz i am stuck here, thanx and regards

Try 'reading' the error:
NSURL *url = [NSURL URLWithString:url1];
NSData *theData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
newPlayer = [[AVAudioPlayer alloc] initWithData:theData error:&error];
if (error == nil) {
NSLog(#"AVAudioPlayer start playing");
[newPlayer play];
} else {
NSLog(#"%#", error);
}
Any "luck" ?

Include AVFoundation and AudioToolBox frameworks
Import header for that framework in .h file
#import<AVFoundation/AVFoundation.h>
#import<AudioToolbox/AudioToolbox.h>
Instantiate AVAudioPlayer,
AVAudioPlayer *player
Use this code for play audio.It's working fine for me
NSString *url1=#"http://mqm.designers99.com/Notification/mp3_files/1331551313_1919.mp3";
url1=[url1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//Encode your url
NSLog(#"after encoding========:%#",url1);
NSURL *url = [NSURL URLWithString:url1];
NSData *theData = [NSData dataWithContentsOfURL:url];
player = [[AVAudioPlayer alloc] initWithData:theData error:nil];
[player play];
Edit:Without encode the url you given also played well.

Related

Play audio without buffer

I am playing an audio file in my iphone app,I fetch the audio from a url.While playing it stops for a while to buffer.
How can I make playing audio without any buffer or play after loaded fully ?
-(void) playAudio2
{
NSURL *url = [NSURL URLWithString:#"http:domain/iphonetest/mp3/sol.mp3"];
NSData *data = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer setVolume:1];
[audioPlayer play];
}
Try using NSURLConnection or AFNetworkingKit to download the file (There is lots of resources show how to download a file. Just search for it).
Then in the -(void)connectionDidFinishLoading:(NSURLConnection *)connection delegate method for NSURLConnection or in the completionBlock for AFNetworkingKit, play the file like your code. But use NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file] for initializing url and use [audioPlayer prepareToPlay] before playing that.

iOS 5.0 AVAudioPlayer Error loading audio clip: The operation couldn’t be completed. (OSStatus error -50.)

So I'm trying to test out the audio player on the iPhone, and I went off Troy Brant's iOS book. I have the Core Audio, Core Foundation, AudioToolbox, and AVFoundation frameworks added to my project. The error message I get is in the subject field. I read like 20 pages of Google search results before resorting to asking here! /sigh. Thanks if you can help. Here's my code, pretty much verbatim out of his book:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Yonah" ofType:#"caf"];
NSLog(#"%#", soundFilePath);
NSURL *fileURL = [NSURL URLWithString:soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (!error)
{
audioPlayer.delegate = self;
//audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}
else
{
NSLog(#"Error loading audio clip: %#", [error localizedDescription]);
}
EDIT: Holy Shinto. I figured out what it was. I changed
NSURL *fileURL = [NSURL URLWithString:soundFilePath];
to
NSURL *fileURL = [NSURL fileURLWithPath:soundFilePath];
to the latter and I was getting a weird error, weirder than the one in the subject BUT I googled that and I changed my OS input device from my webcam to my internal microphone and guess what, it worked under the fileURLWithPath method. I'll be. Damned.
try this one- convert your url into NSdata
NSString* resourcePath = self.audioStr; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error = [[NSError alloc] init];
NSLog(#"url is %#",resourcePath);
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
}`
Try to read the audio file with something like this:
audioPlayer_ = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:[NSString stringWithString:soundFilePath_]]
error:&error];

Get an audio stream from URI and play it on iPhone

I would like to get an audio stream from URL and play it on iPhone. Unfortunately, there is only C# example of how to play that stream, but I need a realization on Objective C in iPhone frameworks. I have tried my self to use different audio frameworks but it was all unsuccessful. This is what in C#:
response = httpWebRequest.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (SoundPlayer player = new SoundPlayer(stream))
{
player.PlaySync();
}
}
What should I do in objective C to get a stream audio from that url? In that URL I don't have an mp3 file or something, I just get a stream. Probably I must somehow download that stream to an iPhone audio file and then play it to get rid of lags.
I tried this:
AVPlayer *player = [AVPlayer playerWithURL:url];
[player play];
And this:
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:nil];
[theAudio setVolume :0.50]; //setting the volume
[theAudio play];
Neither worked.
I have tested my url, it works well in browser.
This is the code that worked:
NSData *_objectData = [NSData dataWithContentsOfURL:url];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
NSString* resourcePath = url; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
app.audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
app.audioPlayer.numberOfLoops = 0;
app.audioPlayer.volume = 1.0f;
[app.audioPlayer prepareToPlay];
if (app.audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[app.audioPlayer play];
Hey I faced this problem of audio streaming for a radio application. I had searched for the answers and they did not work. AVAudioPlayer is not the class to be used here. Downloading the contents of URL using NSData object is not the solution either as it downloads the contents then the code further executes. It is similar to downloading a mp3 and then playing it locally on a player.
[NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]]; //Downloads the contents of the url and in case of radio the url's content is unlimited. The program gets frozen on executing this.
Instead use AVPlayer class.
This code worked in my radio application
AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];
This code does not download the contents at the url but it plays the stream as it is received.
Look into the AVPlayer class, part of the AVFoundation framework... this is the objective-c (iOS) class you use for streaming audio.
MPMoviePlayerController *player;
player = [[MPMoviePlayerController alloc] init] ;
player.view.frame = CGRectMake(0, 0, 0, 0);
player.movieSourceType = MPMovieSourceTypeStreaming;
player.view.hidden = YES;
[self.view addSubview: [wikiMusicPlayer view]];
NSString *address = Your URL;
address = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:address];
[wikiMusicPlayer setContentURL:url];
[wikiMusicPlayer prepareToPlay];
[wikiMusicPlayer setShouldAutoplay: NO];
[wikiMusicPlayer play];

I can not play mp3 file using AVAudioPlayer

I want to play mp3 file placed in resources as code is given below,
NSString *st=#"alarm_1.mp3";
NSLog(st);
NSURL* musicFile = [NSURL fileURLWithPath:st];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
//AVAudioPlayer *click1=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:#"alarm_1.mp3"]];
[click prepareToPlay];
[click play];
[click release];
but unfortunately i cant hear the sound
please help
I don't think your file name is right. Try this syntax:
NSURL *clickURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:clickURL error:nil];
I had a similar problem due to ARC. Instead of defining the AVAudioPlayer in the same method you are using it, you should should have an instance variable somewhere else, such as the UIViewController. This way ARC doesn't auto release this object and audio can play.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
This is answer

AVAudioPlayer with external URL to *.m4p

My Problem is the following. I got this code and i guess a corrupt NSURL since the AVAudioPlayer is nil after initializing:
NSString *dummyURLString = #"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p";
NSError *error;
NSURL *url = [NSURL URLWithString:dummyURLString];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[player play];
Any suggestions what is going wrong here?
The &error shows this:
Error Domain=NSOSStatusErrorDomain Code=-43 "Operation could not be completed. (OSStatus error -43.)"
AVAudioPlayer only works with local URL's. It must be a File URL (file://)
See Apple's Technical Q&A QA1634
I tried this first but got error 2003334207:
NSData *soundData = [NSData dataWithContentsOfURL:URL];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:soundData error:&error];
Seems that AVAudioPlayer really wants a file. So I put the data into a file first:
NSURL *url = [NSURL URLWithString:#"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:#"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:filePath] error:NULL];
NSLog(#"error %#", error);
Instead of using the AVAudioPlayer you can use the AVPlayer. The AVPlayer works as well with remote URLs