avplayer ne lit pas l'URL

J'utilise avplayer pour lire l'URL audio, mais ça ne marche pas, je ne sais pas où je me trompe

NSSsortingng *radioURL = @"https://www.example.com"; radioPlayer = [AVPlayer playerWithURL:[NSURL URLWithSsortingng:radioURL]] ; // [radioPlayer seekToTime:kCMTimeZero]; NSLog(@"radio player %@",radioPlayer.currentItem); [radioPlayer play]; 

Toute aide serait appréciée.

Je recommand fortement d'utiliser ce code pour jouer en streaming radio avec ce code ci-dessous: s'il vous plaît jeter un oeil aussi AVPlayer_Class

  -(void)Play{ NSSsortingng *radioURL = @"https://www.example.com"; //this url must valid AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithSsortingng: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:(NSSsortingng *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == songPlayer && [keyPath isEqualToSsortingng:@"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 est – Streaming audio mp3 avec AVPlayer

J'ai eu le même problème et je me suis juste rendu count que je ne retenais pas le joueur (en utilisant ARC)! Il est donc désalloué et arrête de jouer immédiatement après le début.

Vous devez vous assurer que vous possédez la propriété radioPlayer et utilisez self.radioPlayer au lieu de radioPlayer.

Vous faites bien, mais votre méthode doit corriger sur un seul endroit. Vérifiez en utilisant la méthode ci-dessous:

 NSURL *audioURL = [NSURL URLWithSsortingng: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]; 

et ajoutez des methods de délégation appropriées pour AVAudioPlayer.

Assurez-vous que l'URL est AddingPercentEscapes .

 NSURL *audioURL = // Your Url; NSSsortingng *encodedSsortingng = [@"Your Url" ssortingngByAddingPercentEscapesUsingEncoding:NSUTF8SsortingngEncoding]; NSURL *myURL = [[NSURL alloc] initWithSsortingng:encodedSsortingng] AVPlayer *player = [AVPlayer playerWithURL:myURL]; [player prepareToPlay]; player play];