HTML5 Video API: Cannot Pause On Last Frame If Looping Enabled

若如初见. 提交于 2019-12-25 18:49:06

问题


Trying to pause a video on the very last frame appears to not work if the video is on the very last frame.

var app = angular.module('myApp', []);

app.controller('MainCtrl', function ($scope) {

})

app.directive('someVideo', function ($window, $timeout) {
    return {
    	  controller: function($scope) {
        
        },
        link: function (scope, elm) {
            scope.videoPlayer = elm[0];
            scope.keyDownEvent = keyDownEvent;
            scope.currentTime = 0;
            scope.videoPlayer.loop = true;
            
            angular.element($window).bind('keydown',scope.keyDownEvent);
            
            function keyDownEvent(e){
            console.log(e.keyCode);
              	switch(e.keyCode) {
                    case 32: //space
                    	scope.videoPlayer.loop = !scope.videoPlayer.loop;
                    	console.log('spcae: ',scope.videoPlayer.loop);
                      break;
	              	  case 38: //up
	  			            scope.videoPlayer.currentTime = 0;
                      console.log('up: ', scope.videoPlayer.currentTime);
											break;
                		case 40: //down
                      scope.videoPlayer.pause();
											scope.videoPlayer.currentTime = scope.videoPlayer.duration;
                      console.log('down: ', scope.videoPlayer.currentTime);
											break;
              	}
            }
        }
    }
})
body {
  overflow: hidden;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<div ng-app="myApp">
    <div ng-controller="MainCtrl">
        <video some-video src="http://nagi.ca/u/google.mp4" autoplay></video>
    </div>
</div>

http://jsfiddle.net/4bygou77/4/

STEPS TO REPO:
1) launch the fiddle and click the video to set focus
2) use keys: space = toggle loop, down = last frame, up = first frame

RESULTS:
With Loop Enabled: video pauses, jumps to last frame, then jumps to first frame
With Loop Disabled: video pauses, jumps to last frame (My Desired Result)


回答1:


Change this

scope.videoPlayer.currentTime = scope.videoPlayer.duration;

to this

scope.videoPlayer.currentTime = scope.videoPlayer.duration - 1;

JSFiddle: http://jsfiddle.net/hopkins_matt/4bygou77/5/



来源:https://stackoverflow.com/questions/35209077/html5-video-api-cannot-pause-on-last-frame-if-looping-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!