Shanthanu
Shanthanu

Reputation: 421

Playing audio using url is not working on iOS7

In my application, I have an audio url. I want to play the audio using that url on my device. It does not seem to be working.

Here is the code I am using.

- (IBAction)play:(id)sender {

     NSURL *url = [NSURL URLWithString:@"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3"];

     NSData *data = [NSData dataWithContentsOfURL:url];

     AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];

     [audioPlayer play];
}

I have used the above code. It's not working. Please tell me where I'm going wrong in the above code.

Upvotes: 1

Views: 1944

Answers (2)

Sunny Shah
Sunny Shah

Reputation: 13020

You need to add percentage encoding %20 of space in URL

  NSString *strURl=@"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3";
        NSURL *url = [NSURL URLWithString:[strURl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

if still not working use delegate methods

audioPlayer = self;

USe delegate methods

 -(void)audioPlayerDecodeErrorDidOccur:
    (AVAudioPlayer *)player
                                    error:(NSError *)error
    {
        NSLog(@"%@",error);
    }

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107221

You need to add percentage escape in your query string, also you need to declare the AVAudioPlayer as a global variable.

So write the property in your class extension:

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

And modify your method like:

- (IBAction)play:(id)sender
{
     NSString *urlstr = @"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3";
     urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSURL *url = [NSURL URLWithString:urlstr];
     NSData *data = [NSData dataWithContentsOfURL:url];
     _audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
     [_audioPlayer play];
}

Upvotes: 2

Related Questions