AVAUdioPlayer - changing current URL

前端 未结 2 1316
无人共我
无人共我 2020-12-17 05:06

In my audio player app, if I hit the fast forward button - the player must find the next song in the playlist and play it. What I do is I get the URL from the next song, and

相关标签:
2条回答
  • 2020-12-17 05:30

    For playing more than one URL, or effectively changing the URL, use the subclass AVQueuePlayer.

    AVQueuePlayer is a subclass of AVPlayer you use to play a number of items in sequence.

    Example:

    NSString *fooVideoPath = [[NSBundle mainBundle] pathForResource:@"tommy" ofType:@"mov"];
    NSString *barVideoPath = [[NSBundle mainBundle] pathForResource:@"pamela" ofType:@"mp4"];
    
    AVPlayerItem *fooVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:fooVideoPath]];
    AVPlayerItem *barVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:barVideoPath]];
    
    self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:fooVideoItem, barVideoItem,nil]];
    [self.queuePlayer play];
    
    // things happening...
    
    [self.queuePlayer advanceToNextItem];
    
    0 讨论(0)
  • 2020-12-17 05:47

    Have checked 4 Apple samples, they are using AVAudioPlayer to play one song only. However, your result looks very interesting and impressive! Please let us know, are you stopping the playback before reinitializing object with the same address, are you starting the new audio session ?

    As for me, I'd not put the playback and app stability in a risk doing something not mentioned in the documentation but , but to be on the bright side would use the AVAudioPlayer class as it seems the most right, which gives us:

    • use the error variable to track the possible errors
    • stop the playing AVAudioPlayer instance, initialize a new instance of AVAudioPlayer and set it to the property letting an old-one to be deallocated automatically.

    And you probably know yourself, that

    self.background = [self.background initWithContentsOfURL::
    

    will remove the warning for you.

    0 讨论(0)
提交回复
热议问题