加载ViewController时,UISlider的AVPlayer currentTime更新 [英] AVPlayer currentTime update for a UISlider when ViewController load

查看:92
本文介绍了加载ViewController时,UISlider的AVPlayer currentTime更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在AVPlayer中播放歌曲.我为媒体播放器和初始化创建了一个单独的视图控制器,并且我用于播放器的所有方法(播放,暂停,重复,随机播放)都在同一个视图控制器中.

I'm playing songs in AVPlayer. I have created a separate view controller for my media player and initialization, and all the methods that I'm using for the player (play, pause, repeat, shuffle) are there in the same view controller.

我更新了这样的滑块:

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(sliderUpdate:) userInfo:nil repeats:YES];`


- (void) sliderUpdate:(id) sender{
    int currentTime =   (int)((song.player.currentTime.value)/song.player.currentTime.timescale);
    slider.value=currentTime;
    NSLog(@"%i",currentTime);

    song.currentTime=currentTime;
    int currentPoint=(int)((song.player.currentTime.value)/song.player.currentTime.timescale);
    int pointMins=(int)(currentPoint/60);
    int pointSec=(int)(currentPoint%60);

    NSString *strMinlabel=[NSString stringWithFormat:@"%02d:%02d",pointMins,pointSec];
    lblSlidermin.text=strMinlabel;
    song.strslidermin=strMinlabel;
}

一旦我离开视图控制器,再次播放时,歌曲正在播放,但问题是滑块未更新.因此,我创建了一个singleton类来分配当前播放的歌曲详细信息.同样在滑块更新中,我为单例类变量分配了playerCurrentTime(slidercurrent值).而我分配的viewdidload方法是这样的:

Once I'm going out of the viewcontroller and when come again, song is playing but the problem is slider is not updating. So I created a singleton class to assign currently playing song details. Also inside the slider update I asigned playerCurrentTime (slidercurrent value) for a singleton class variable. And my viewdidload method I assigned like this:

if (song.isPlaying==NO) {
    [self prePlaySong];
}else{
    lblAlbum.text=song.currentAlbum;
    lblArtist.text=song.currentArtist;
    lblSong.text=song.currentSong;
    slider.value=song.currentTime;
    slider.maximumValue=song.sliderMax;
    slider.minimumValue=song.sliderMin;
    imgSong.image=song.songImage;
    [btnMiddle setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
}

,但是滑块没有更新.为什么会这样,我该如何解决呢?

but slider is not getting updated. Why is that and how I can solve this problem?

推荐答案

您不应为此使用计时器,AVPlayer提供您可以观察时间的API.

You should not use a timer for this, AVPlayer provides an API you observe the time.

像这样注册观察者(例如,在viewWillAppear中):

Register an observer (e.g. in viewWillAppear) like this:

CMTime interval = CMTimeMakeWithSeconds(1.0, NSEC_PER_SEC); // 1 second
self.playbackTimeObserver =
    [self.player addPeriodicTimeObserverForInterval:interval 
                                              queue:NULL usingBlock:^(CMTime time) {
    // update slider value here...
}];

要取消注册观察者(例如,在viewDidDisappear中),请执行以下操作:

To unregister the observer (e.g. in viewDidDisappear), do:

[self.player removeTimeObserver:self.playbackTimeObserver];

希望有帮助.

这篇关于加载ViewController时,UISlider的AVPlayer currentTime更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆