[英]Uislider with Avplayer
嗨,我创建了Avplayer来播放服务器中的音频,并且其工作正常,但是即使更新了UISlider方法后,UISlider也无法与音频同步,但是滑块可以正常移动,但无法与音频同步。我需要将该音频与UISlider进行匹配
seekbar =[[UISlider alloc]init];
seekbar.frame=CGRectMake(10,CGRectGetMinY(PlayAudio.frame)-50, CGRectGetWidth(self.view.frame)-20, 20);
[seekbar addTarget:self action:@selector(seektime:) forControlEvents:UIControlEventValueChanged];
seekbar.continuous=YES;
[self.view addSubview:seekbar];
-(void)Audio{
NSString *urlString= @"https:/embed-ssl.wistia.com/deliveries/85dcf8de9db3830f47f136a4dba89114be58403f/dhwpxq4l9l.wav";
audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[audioPlayer currentItem]];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
[audioPlayer play];
NSLog(@"Audio playing");
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == audioPlayer && [keyPath isEqualToString:@"status"]) {
if (audioPlayer.status == AVPlayerStatusFailed) {
NSLog(@"AVPlayer Failed");
} else if (audioPlayer.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayerStatusReadyToPlay");
[audioPlayer play];
} else if (audioPlayer.status == AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}
}
- (void)seektime:(UISlider*)sender {
CGFloat currentSongTime = CMTimeGetSeconds([audioPlayer currentTime]);
seekbar.value = currentSongTime;
seekbar.minimumValue=0;
seekbar.maximumValue=currentSongTime;
}
- (void)updateProgress {
[seekbar setValue:CMTimeGetSeconds(audioPlayer.currentTime)];
}
为什么每次更改都会更改最大值? 当您根据audioPlayer.currentItem.duration
获得AVPlayerStatusReadyToPlay时,可以设置minimumValue
和maximumValue
因此,除了seektime
您可以使用:
else if (audioPlayer.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayerStatusReadyToPlay");
[audioPlayer play];
seekbar.minimumValue = 0;
seekbar.maximumValue = CMTimeGetSeconds(audioPlayer.currentItem.duration);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.