我正在调整demo project of Player.
请查看下面的“更新”部分而不是此处:
一位朋友告诉我,我可以使用这种方法,但在这种情况下,我无法真正了解如何调整这种方法.
[NSTimer scheduledTimerWithInterval:1.0 target:self selector:@selector(refreshCurrentTimeTextFIEld) userInfo:nil repeats:YES]-(voID)refreshCurrentTimeTextFIEld { NSTimeInterval currentTime; QTMovIE *movIE = [movIEVIEw movIE]; QTGetTimeInterval([movIE currentTime],¤tTime); int hours = currentTime/3600; int minutes = (currentTime/60) % 60; int seconds = currentTime % 60; Nsstring *timeLabel; if(hours > 0) { timeLabel = [Nsstring stringWithFormat:@"%02i:%02i:%02i",hours,minutes,seconds]; } else { timeLabel = [Nsstring stringWithFormat:@"%02i:%02i",seconds]; } [yourTextFIEld setStringValue:timeLabel];}
如果你们中的任何人使用或偶然发现这个库,有没有办法获得currentTime?有没有其他方法来访问/ *** 纵视频的当前时间?
更新:
有人显然给了导入和输入的代码.连接UiSlider for Objective-C用于等效库PBJVideoPlayer in here.但我无法在Swift for Player上采用相同的方法.
来自链接的片段:
diff --git a/Pods/PBJVIDeoPlayer/Source/PBJVIDeoPlayerController.m b/Pods/PBJVIDeoPlayer/Source/PBJVIDeoPlayerController.mindex be6a7d1..51a420e 100644--- a/Pods/PBJVIDeoPlayer/Source/PBJVIDeoPlayerController.m+++ b/Pods/PBJVIDeoPlayer/Source/PBJVIDeoPlayerController.m@@ -81,6 +81,8 @@ static Nsstring * const PBJVIDeoPlayerControllerReadyFordisplay = @"readyFordisp float _volume; }+@property (nonatomic,strong) UiSlider *slIDer;+ @end @implementation PBJVIDeoPlayerController@@ -93,6 +95,16 @@ static Nsstring * const PBJVIDeoPlayerControllerReadyFordisplay = @"readyFordisp #pragma mark - getters/setters+- (UiSlider *)slIDer {+ if (_slIDer == nil) {+ _slIDer = [[UiSlider alloc] initWithFrame:CGRectZero];+ _slIDer.minimumValue = 0.0;+ _slIDer.maximumValue = 0.0;+ _slIDer.continuous = YES;+ }+ return _slIDer;+}+ - (voID)setVIDeoFillMode:(Nsstring *)vIDeoFillMode { if (_vIDeoFillMode != vIDeoFillMode) {@@ -311,6 +323,59 @@ static Nsstring * const PBJVIDeoPlayerControllerReadyFordisplay = @"readyFordisp NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addobserver:self selector:@selector(_applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil]; [nc addobserver:self selector:@selector(_applicationDIDEnterBackground:) name:UIApplicationDIDEnterBackgroundNotification object:nil];++ self.slIDer.hIDden = YES;+ [self.slIDer addTarget:self action:@selector(moveScrubber:) forControlEvents:UIControlEventValueChanged];+ [self.vIEw addSubvIEw:self.slIDer];++ __weak typeof(self) weak = self;+ CMTime intervalSeconds = CMTimeMakeWithSeconds(0.25f,NSEC_PER_SEC);+ [_player addPeriodicTimeObserverForInterval:intervalSeconds+ queue:dispatch_get_main_queue()+ usingBlock:^(CMTime time) {+ [weak syncScrubber];+ }];+}++- (voID)syncScrubber+{+ CMTime duration = _player.currentItem.duration;+ if (CMTIME_IS_INVALID(duration)) {+ self.slIDer.maximumValue = 0.0;+ } else {+ float64 seconds = CMTimeGetSeconds(duration);+ if (isfinite(seconds)) {+ self.slIDer.maximumValue = seconds;+ float64 current = CMTimeGetSeconds([_player currentTime]);+ self.slIDer.value = current;+ }+ }+}++- (voID)moveScrubber:(UiSlider *)slIDer+{+ CMTime duration = _player.currentItem.duration;+ if (CMTIME_IS_INVALID(duration)) {+ self.slIDer.maximumValue = 0.0;+ } else {+ float64 seconds = CMTimeGetSeconds(duration);+ if (isfinite(seconds)) {+ self.slIDer.maximumValue = seconds;+ [_player seektotime:CMTimeMakeWithSeconds(slIDer.value,NSEC_PER_SEC)];+ }+ }+}++- (voID)vIEwDIDAppear:(BOol)animated+{+ [super vIEwDIDAppear:animated];++ CGRect slIDerRect = self.vIEw.frame;+ slIDerRect.origin.y = slIDerRect.size.height - 40;+ slIDerRect.size.height = 40;+ slIDerRect.origin.x = self.vIEw.frame.size.wIDth/4.0;+ slIDerRect.size.wIDth = self.vIEw.frame.size.wIDth/2.0;+ self.slIDer.frame = slIDerRect; } - (voID)vIEwDIDdisappear:(BOol)animated@@ -402,11 +467,13 @@ typedef voID (^PBJVIDeoPlayerBlock)(); case PBJVIDeoPlayerPlaybackStateStopped: { [self playFromBeginning];+ self.slIDer.hIDden = YES; break; } case PBJVIDeoPlayerPlaybackStatePaused: { [self playFromCurrentTime];+ self.slIDer.hIDden = YES; break; } case PBJVIDeoPlayerPlaybackStatePlaying:@@ -414,6 +481,7 @@ typedef voID (^PBJVIDeoPlayerBlock)(); default: { [self pause];+ self.slIDer.hIDden = NO; break; } }@@ -511,6 +579,14 @@ typedef voID (^PBJVIDeoPlayerBlock)(); _vIDeoVIEw.playerLayer.backgroundcolor = [[UIcolor blackcolor] CGcolor]; [_vIDeoVIEw.playerLayer setPlayer:_player]; _vIDeoVIEw.playerLayer.hIDden = NO;++ CMTime duration = _playerItem.duration;+ if (CMTIME_IS_INVALID(duration)) {+ self.slIDer.maximumValue = 0.0;+ } else {+ float64 seconds = CMTimeGetSeconds(duration);+ self.slIDer.maximumValue = seconds;+ } break; } case AVPlayerStatusFailed:解决方法 我只是运行GitHub项目,我通过以下代码修复它:
在您的Player.swift类中.
player.addPeriodicTimeObserverForInterval(CMTimeMake(1,100),queue: dispatch_get_main_queue()) { [uNowned self] time in let timeString = String(format: "%02.2f",CMTimeGetSeconds(time)) print("time is \(timeString)") self.delegate?.playerPlaybackstimer(timeString) }
在公共方便init(){funtion中添加的Above方法将如下所示:
public convenIEnce init() { self.init(nibname: nil,bundle: nil) self.player = AVPlayer() self.player.actionAtItemEnd = .Pause self.player.addobserver(self,forKeyPath: PlayerRateKey,options: ([NSkeyvalueObservingOptions.New,NSkeyvalueObservingOptions.old]),context: &PlayerObserverContext) self.playbackLoops = false self.playbackFreezesAtEnd = false self.playbackState = .Stopped self.bufferingState = .UnkNown player.addPeriodicTimeObserverForInterval(CMTimeMake(1,CMTimeGetSeconds(time)) print("time is \(timeString)") self.delegate?.playerPlaybackstimer(timeString) } }
之后,我在公共协议PlayerDelegate中创建一个Delegate方法:classthat将如下所示:
public protocol PlayerDelegate: class { func playerReady(player: Player) func playerPlaybackStateDIDChange(player: Player) func playerBufferingStateDIDChange(player: Player) func playerPlaybackWillStartFromBeginning(player: Player) func playerPlaybackDIDEnd(player: Player) func playerPlaybackstimer(Nsstring: String)}
现在进入VIEwController.swift类并添加以下函数:
func playerPlaybackstimer(Nsstring: String) { print("currunt time \(Nsstring)") }
现在运行您的项目并享受.
总结以上是内存溢出为你收集整理的ios – 获取视频的当前时间(播放器 – Swift)全部内容,希望文章能够帮你解决ios – 获取视频的当前时间(播放器 – Swift)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)