9

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.

Prachi Rajput
  • 502
  • 1
  • 8
  • 20

5 Answers5

11

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

Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
7

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.

Borzh
  • 5,069
  • 2
  • 48
  • 64
  • @ Borzh, I did same as you tolad. In my case I get response AVPlayerStatusReadyToPlay, but audio didn't play. Please reply ASAP. thanks – Ravi Dec 03 '16 at 05:10
  • @Monu singh, probably you have some other problem, I just told the problem I had myself. – Borzh Dec 06 '16 at 22:23
-1

You are doing good but your method need to correct on one place.Check using the below method:

NSURL *audioURL = [NSURL URLWithString:radioURL];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:nil];
self.audioPlayer.numberOfLoops = -1;
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
self.audioPlayer.volume=1.0;
[self.audioPlayer autorelease];
[self.audioPlayer Play];

and add proper delegate methods for AVAudioPlayer.

-1

Make Sure URL is AddingPercentEscapes.

NSURL *audioURL = // Your Url;
NSString *encodedString = [@"Your Url" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL = [[NSURL alloc] initWithString:encodedString]
AVPlayer *player = [AVPlayer playerWithURL:myURL];
[player prepareToPlay];
player play];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • weird ... where did that prepareToPlay comefrom? in AVPlayer? can't seem to find it on the .h source io8 is that removed? – eNeF Jul 02 '15 at 08:35
  • It's probably wrong since its an method on AVAudioPlayer, not AVPlayer – Jonas Nov 23 '15 at 11:37
-1

Did you forgot to set the attribute App Transport Security Settings -> Allow Arbitrary Loads (Yes)?

App Transport Security Settings

Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37