以不同的时间和功能循环播放视频

时间:2019-05-16 11:17:29

标签: javascript jquery html5 video

我有2个功能,可播放相同的视频,但时序不同。 我不能播放使该功能正常工作。

该功能似乎没有重置其他功能

我尝试更改变量名称,但仍然更改了点击时间。

var video = document.getElementById('videoElm');

function playShortVideo() {
  var starttime = 0; // start at 0 seconds
  var endtime = 2; // stop at 2 seconds
  video.addEventListener("timeupdate", function() {
    if (this.currentTime >= endtime) {
      this.currentTime = 0; // change time index here
    }
  }, false);

  video.load();
  video.play();
}

function playFullVideo() {
var starttime = 0; // start at 0 seconds
var endtime = 24; // stop at 2 seconds
  video.addEventListener("timeupdate", function() {
    if (this.currentTime >= endtime) {
      this.currentTime = 0; // change time index here
    }
  }, false);

  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {
  
  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
    <video id="videoElm" autoplay muted controls loop>
    <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>
 
</div>

<button class="shortvideo">play 2 secs only</a><br>

<button class="fullvideo">loop full video</button>

2 个答案:

答案 0 :(得分:0)

那是因为侦听器仍在,您需要remove

请记住,为了删除它,您不能使用匿名函数作为回调,所以我将其变成了已定义的函数。

System.TypeInitializationException: The type initializer for 'MongoDB.Bson.Serialization.BsonClassMap' threw an exception. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Reflection.IntrospectionExtensions.GetTypeInfo(Type type)
at MongoDB.Bson.Serialization.BsonClassMap..cctor()
--- End of inner exception stack trace ---
at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type classType)
at MongoDB.Bson.Serialization.BsonClassMapSerializationProvider.GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Bson.Serialization.BsonSerializerRegistry.CreateSerializer(Type type)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializer[T]()
at MongoDB.Driver.MongoCollectionImpl`1..ctor(IMongoDatabase database, CollectionNamespace collectionNamespace, MongoCollectionSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
at MongoDB.Driver.MongoDatabaseImpl.GetCollection[TDocument](String name, MongoCollectionSettings settings)
at api.mstiDFE.Infra.Repositories.BaseRepository..ctor(IConfiguration config) in C:\Users\Source\Workspace\api.mstiDFE\api.mstiDFE\Infra\Repositories\BaseRepository.cs:line 27
var video = document.getElementById('videoElm');
const playShort = function() {
  if (this.currentTime >= 2) {
    this.currentTime = 0; // change time index here
  }
};
const playFull = function() {
  if (this.currentTime >= 24) {
    this.currentTime = 0; // change time index here
  }
};

function playShortVideo() {
  video.removeEventListener("timeupdate", playFull, false)
  video.addEventListener("timeupdate", playShort, false);

  video.load();
  video.play();
}

function playFullVideo() {
  video.removeEventListener("timeupdate", playShort, false)
  video.addEventListener("timeupdate", playFull, false);
  
  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {

  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});

答案 1 :(得分:0)

发生这种情况是因为您多次附加了timeUpdate事件侦听器。 您需要只使用一个或删除它,然后再附加一个新的。

var video = document.getElementById('videoElm');
var listener;
var starttime = 0;
var endtime = 2;

function updateVideo(e) {
  if (e.target.currentTime >= endtime) {
    e.target.currentTime = 0; // change time index here
  }
}

function playShortVideo() {
  starttime = 0; // start at 0 seconds
  endtime = 2; // stop at 2 seconds
  if (!listener) {
    listener = video.addEventListener("timeupdate", updateVideo, false);
  }
  video.load();
  video.play();
}

function playFullVideo() {
  starttime = 0; // start at 0 seconds
  endtime = 24; // stop at 2 seconds
  if (!listener) {
    listener = video.addEventListener("timeupdate", updateVideo, false);
  }
  video.load();
  video.play();
}

//play short video by default
playShortVideo();


//CLICK events 
var btnshort = $('.shortvideo');
var btnfull = $('.fullvideo');
btnshort.click(function() {

  playShortVideo();
});

btnfull.click(function() {
  playFullVideo();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <video id="videoElm" autoplay muted controls loop>
    <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>

</div>

<button class="shortvideo">play 2 secs only</a><br>

<button class="fullvideo">loop full video</button>