Search code examples
iosxcodeaudiostreamavaudioplayer

Need help streaming Audio from URL using AVAudioPlayer iOS


I am using twilio to create a phone app. I have a link to a voicemail and I am trying to play the voicemail from a control in a tableview. Since I have never streamed audio before I decided to simplify everything and just try to stream audio from any URL. The URL I am trying to stream audio from is "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8".

I have tried using examples from Apples Developer page but I guess I'm just not understanding the documentation.

This is what I am currently trying and I get an error.

NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

And I get This Error...

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

Am I going in the right direction or should I be doing something differently. I've found a number of different threads but none of what I see gets me to play audio. A Push in the right direction would be greatly appreciated.

I have also tried using AVPlayer...

NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.player = [[AVPlayer alloc] initWithURL:url];
[self.player play];

and nothing happens


Solution

  • Try this

    @property (nonatomic,strong) AVPlayer *audioStremarPlayer;
    
    NSURL *url = [NSURL URLWithString:@"your url"];
    
    // stremar player
    AVPlayer *player = [[AVPlayer alloc]initWithURL:url];
    self.audioStremarPlayer= player;
    [self.audioStremarPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
    {
    
        if (object == self.audioStremarPlayer && [keyPath isEqualToString:@"status"]) {
            if (self.audioStremarPlayer.status == AVPlayerStatusFailed)
            {
          //  //NSLog(@"AVPlayer Failed");
            } 
            else if (self.audioStremarPlayer.status == AVPlayerStatusReadyToPlay) 
            {
                [self.audioStremarPlayer play];
            } 
            else if (self.audioStremarPlayer.status == AVPlayerItemStatusUnknown) 
            {
          //  //NSLog(@"AVPlayer Unknown");
    
            }
        }
    }