And*_*Dev 11 audio objective-c ios avplayer avplayeritem
我有一个AVPlayer类,所有设置都流式传输音频文件.它有点长,所以我不能在这里发布整个事情.我所坚持的是如何允许用户在完成一次听完之后重放音频文件.当它第一次完成时,我正确收到通知AVPlayerItemDidPlayToEndTimeNotification
.当我重播它时,我立即收到相同的通知,阻止我重播它.
我怎么能重置这个,以至于AVPlayerItem
不认为它已经播放了音频文件?我可以释放所有内容并重新设置它,但我相信这将迫使用户再次下载音频文件,这是毫无意义和缓慢的.
以下是我认为相关的课程的一些部分.尝试重播文件时得到的输出如下所示.前两行正是我所期望的,但第三行是一个惊喜.
正在播放
没有定时器的
音频播放器已播放音频
- (id) initWithURL : (NSString *) urlString
{
self = [super init];
if (self) {
self.isPlaying = NO;
self.verbose = YES;
if (self.verbose) NSLog(@"url: %@", urlString);
NSURL *url = [NSURL URLWithString:urlString];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
[self determineAudioPlayTime : self.playerItem];
self.lengthOfAudioInSeconds = @0.0f;
[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
}
return self;
}
// this is what gets called when the user clicks the play button after they have
// listened to the file and the AVPlayerItemDidPlayToEndTimeNotification has been received
- (void) playAgain {
[self.playerItem seekToTime:kCMTimeZero];
[self toggleState];
}
- (void) toggleState {
self.isPlaying = !self.isPlaying;
if (self.isPlaying) {
if (self.verbose) NSLog(@"is playing");
[self.player play];
if (!timer) {
NSLog(@"no timer");
CMTime audioTimer = CMTimeMake(0, 1);
[self.player seekToTime:audioTimer];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateProgress)
userInfo:nil
repeats:YES];
}
} else {
if (self.verbose) NSLog(@"paused");
[self.player pause];
}
}
-(void)itemDidFinishPlaying:(NSNotification *) notification {
if (self.verbose) NSLog(@"audio player has finished playing audio");
[[NSNotificationCenter defaultCenter] postNotificationName:@"audioFinished" object:self];
[timer invalidate];
timer = nil;
self.totalSecondsPlayed = [NSNumber numberWithInt:0];
self.isPlaying = NO;
}
Run Code Online (Sandbox Code Playgroud)
小智 19
当玩家收到AVPlayerItemDidPlayToEndTimeNotification时,您可以调用seekToTime方法
func itemDidFinishPlaying() {
self.player.seek(to: CMTime.zero)
self.player.play()
}
Run Code Online (Sandbox Code Playgroud)
小智 5
Apple 建议AVQueueplayer
与AVPlayerLooper
.
这是苹果的(稍作修改的)示例代码:
AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] init];
AVAsset *asset = // AVAsset with its 'duration' property value loaded
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
// Create a new player looper with the queue player and template item
self.playerLooper = [AVPlayerLooper playerLooperWithPlayer:queuePlayer
templateItem:playerItem];
// Begin looping playback
[queuePlayer play];
Run Code Online (Sandbox Code Playgroud)
它AVPlayerLooper
会为您监听和播放所有事件,并且队列播放器用于创建他们所谓的“跑步机模式”。此模式本质上是在队列播放器中链接相同的多个实例AVAssetItem
,并将每个完成的资产移回队列的开头。
这种方法的优点是,它使框架能够在下一个资产(在本例中是相同的资产,但其开始仍然需要预滚动)到达之前对其进行预滚动,从而减少资产结束和循环开始之间的延迟。
视频中约 15:00 对此进行了更详细的描述:https://developer.apple.com/videos/play/wwdc2016/503/