Streaming mp3 audio with AVPlayer

后端 未结 5 1275
眼角桃花
眼角桃花 2020-11-28 22:09

I\'m hearing some conflicting reports about this. What I\'m trying to do is stream an mp3 file from a URL. I\'ve done hours of research, but I cannot find any good guides on

5条回答
  •  Happy的楠姐
    2020-11-28 22:49

    try this

     -(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];
    
    
    
        }
        - (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
    
        }
    

提交回复
热议问题