Saad Umar
Saad Umar

Reputation: 604

iphone dev mp3 file not playing using av audio player

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

Upvotes: 0

Views: 655

Answers (2)

iphonecool
iphonecool

Reputation: 97

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.

Upvotes: 0

basvk
basvk

Reputation: 4546

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

Upvotes: 1

Related Questions