Vishal
Vishal

Reputation: 159

Play Audio from URL using AVAudioPlayer

I am Playing Audio from URL given below

"http://sumeetmusiclocal.wsisites.net/Songs/Kashyala Lavato (Lavani).mp3"

By Using Code given below

NSString *urlString = @"http://sumeetmusiclocal.wsisites.net/Songs/Kashyala Lavato (Lavani).mp3";
NSURL *audioFileLocationURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
[audioPlayer setNumberOfLoops:-1];

if (error) {
    NSLog(@"%@", [error localizedDescription]);

    [[self volumeControl] setEnabled:NO];
    [[self playPauseButton] setEnabled:NO];

    [[self alertLabel] setText:@"Unable to load file"];
    [[self alertLabel] setHidden:NO];
} else {
    [[self alertLabel] setText:[NSString stringWithFormat:@"%@ has loaded", @"HeadspinLong.caf"]];
    [[self alertLabel] setHidden:NO];

    //Make sure the system follows our playback status
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    //Load the audio into memory
    [audioPlayer prepareToPlay];
}

But I getting Error saying

Printing description of error:
Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"
2016-03-08 12:43:45.835 SumeetApp[1839:78581] The operation couldn’t be completed. (OSStatus error 2003334207.)

Can anyone please help me to solve this error or suggest me some other way for playing audio from url.

Thank you

Upvotes: 3

Views: 4863

Answers (4)

iVarun
iVarun

Reputation: 6631

AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:@"http://sumeetmusiclocal.wsisites.net/Songs/Kashyala Lavato (Lavani).mp3"]];  
[player play];

Try this code this is working for me.

Upvotes: 1

Ashish Thummar
Ashish Thummar

Reputation: 431

Try This

AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/VfE_html5.mp4"]];

AVPlayerItem *playerItem1 = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *player1 = [AVPlayer playerWithPlayerItem:playerItem1];
AVPlayerLayer *playerLayer1 = [AVPlayerLayer playerLayerWithPlayer:player1];
playerLayer1.videoGravity = AVLayerVideoGravityResizeAspectFill;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    playerLayer1.frame = self.view.frame;
});

[self.view.layer insertSublayer:playerLayer1 atIndex:1];
[player1 play];

Hope this work for you.

Upvotes: 1

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5436

For Error:

you trying to access a file that didn't exist.

so, initWithContentsOfURL:audioFileLocationURL will return nil.

To play Audio:

NSURL *url = [NSURL URLWithString:@"<URL>];


    self.playerItem = [AVPlayerItem playerItemWithURL:url];

    self.player = [AVPlayer playerWithPlayerItem:playerItem];

    self.player = [AVPlayer playerWithURL:<URL>];

   [player play];

Upvotes: 0

Kamlesh Shingarakhiya
Kamlesh Shingarakhiya

Reputation: 2777

Try This

NSURL *url = [NSURL URLWithString:@"YOUR_MP3URL"];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:@"YOUR_MP3URL"];

for play

[self.player play];

Hope this helps.

Upvotes: 0

Related Questions