How to get audio to play from an online url on iPhone

27,626

Solution 1

USe AVPlayer to play mp3 file from url

-(void)playselectedsong{

    AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
    self.songPlayer = player;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[songPlayer currentItem]];
    [self.songPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];

    [self.songPlayer play];

}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if (object == songPlayer && [keyPath isEqualToString:@"status"]) {
        if (songPlayer.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayer Failed");

        } else if (songPlayer.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayerStatusReadyToPlay");


        } else if (songPlayer.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");

        }
    }
}

- (void)playerItemDidReachEnd:(NSNotification *)notification {

 //  code here to play next sound file

}

Solution 2

its very simple Use AVAudioPlayer,take object as AVAudioPlayer *audioPlayer,

  NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
  NSData *soundData = [NSData dataWithContentsOfURL:url];
  audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData  error:NULL];
  audioPlayer.delegate = self;
  [audioPlayer play];

write below delegate Methods:

 -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
    [audioPlayer stop];
     NSLog(@"Finished Playing");
  }

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

Solution 3

You may find a live test stream at here

To play a file use :

[player play];

And here is the code to play a live stream from URL:

NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
    /* [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext]; (can use it) */
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];
Share:
27,626
falky
Author by

falky

Updated on April 15, 2020

Comments

  • falky
    falky about 4 years

    I know it's been asked before by plenty of people, but I just have no idea why this isn't working in my application in x-code and objective c!

    Basically, I want to play audio in an application from a url. Just using the testing audio from the Apple site, my code for it is:

    NSString *playString = @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";
    
    //Converts songURL into a playable NSURL
    NSURL *playURL = [NSURL URLWithString:playString];
    
    AVAudioPlayer *backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error];
    
    if (backgroundMusicPlayer == nil) {
        NSLog(@"%@", [error description]);
    }
    else {
        [backgroundMusicPlayer prepareToPlay];
        [backgroundMusicPlayer play];
    }
    

    It comes up with the following error:

    Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)

    And I can't find anything when I google it. Any ideas? Should I try another audio stream? Or does anyone have an example that works? Is it because I am using the iPhone simulator not the actual iPhone?

    EDIT:

    Please note - When I say URL I mean from an actual website, like "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8" I don't want to have to download the song first, I want to be able to play it as it is downloading! Thanks

  • iSpark
    iSpark about 11 years
    bcoz here url must be of type local,if we use initWithContentsOfURL.so converted it into NSData
  • falky
    falky about 11 years
    Thanks very much for your help. I still don't understand though why this doesn't work though. What do i need to change? I haven't put in any of the void delegate methods as above...: NSString *playString = @"devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"; NSURL *playURL = [NSURL URLWithString:playString]; NSData *soundData = [NSData dataWithContentsOfURL:playURL]; AVAudioPlayer *backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:&error]; [backgroundMusicPlayer prepareToPlay]; [backgroundMusicPlayer play];
  • Carl Veazey
    Carl Veazey about 11 years
    Downloading a playlist file to the local file system is not a viable workaround to AVAudioPlayer's lack of streaming support.
  • falky
    falky about 11 years
    @CarlVeazey What do you suggest then mate?
  • Iulian Onofrei
    Iulian Onofrei about 8 years
    Why is self.player being initialized twice?!
  • Moxarth
    Moxarth over 6 years
    i have tried this , but its not playing any songs from given URL . do know why ?
  • Moxarth
    Moxarth over 6 years
    i am playing song from mp3 url . these code did not work for me . do you have any other solutions or other way to do it ?
  • Moxarth
    Moxarth over 6 years
    i have followed your code but it just does not play song . and there is no error also . do you know what i have missed in it ?
  • Sumit Mundra
    Sumit Mundra over 6 years
    @Moxarth can you share url here
  • Moxarth
    Moxarth over 6 years
    i solved the issues . thanks . your method helped me a lot .
  • ekashking
    ekashking almost 3 years
    Make sure you complete your answer in a proper way. For instance, what is self.songPlayer ??????