Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avplayer is not playing the URL

Tags:

ios

avplayer

I am using avplayer for play audio url, but it is not working, I don't know where i am wrong

NSString *radioURL = @"https://www.example.com";

radioPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:radioURL]] ;
// [radioPlayer seekToTime:kCMTimeZero];
NSLog(@"radio player %@",radioPlayer.currentItem);
[radioPlayer play];

Any help would be appreciated.

like image 795
Prachi Rajput Avatar asked Mar 06 '14 06:03

Prachi Rajput


2 Answers

I had the same issue and just realized that I wasn't retaining the player (using ARC)! So it gets deallocated and stop playing immediately after start.

You need to make sure that you have a strong property radioPlayer and use self.radioPlayer instead of radioPlayer.

like image 140
Borzh Avatar answered Nov 13 '22 10:11

Borzh


I strongly recommended the code below to play radio streaming: please take a look also AVPlayer_Class

 -(void)Play{
        NSString *radioURL = @"https://www.example.com"; //this url must valid 
        AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:radioURL]];
        self.songPlayer = player;    //self.songPlayer is a globle object of avplayer
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[songPlayer currentItem]];
        [self.songPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
}


- (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");
                [self.songPlayer play];


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

            }
        }
    }

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

     //  code here to play next sound file

    }

Ref link is - Streaming mp3 audio with AVPlayer

like image 11
Nitin Gohel Avatar answered Nov 13 '22 10:11

Nitin Gohel