0
votes

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

2
P.s. You don't need the -initWithData:error: instance method for this, you could just use -initWithContentsOfURL:error:basvk
What iOS version are you deploying on?basvk
no luck rather it is displaying this error the operation couldn't be completed (OSStatus -43)Saad Umar
ios version 5 is of my device its ipod 4.Saad Umar

2 Answers

1
votes

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" ?

0
votes

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.