ckplayer-plus 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react.cjs CHANGED
@@ -212,7 +212,7 @@ var require_ckplayer = __commonJS({
212
212
  cdnFallback: true
213
213
  // getPath 失败时是否回退到 jsDelivr(内网可设 false,并配置 pluginPath)
214
214
  };
215
- var ckplayerPlusDefaultCdn = "https://cdn.jsdelivr.net/npm/ckplayer-plus@1.0.3/dist/";
215
+ var ckplayerPlusDefaultCdn = "https://cdn.jsdelivr.net/npm/ckplayer-plus@1.0.6/dist/";
216
216
  function ckplayerEmbed(videoObj) {
217
217
  var rightMenu = [
218
218
  {
@@ -237,6 +237,11 @@ var require_ckplayer = __commonJS({
237
237
  var paused = true;
238
238
  var loadTime = 0;
239
239
  var seekTime = 0;
240
+ var seekRetryTimer = null;
241
+ var seekRetryCount = 0;
242
+ var seekApplyLock = false;
243
+ var decodeRecoverCount = 0;
244
+ var hlsStartPositionUsed = false;
240
245
  var oldTime = 0, playTime = 0, firstSeekTime = -1, maxSeeTime = 0;
241
246
  var isChangeDef = true;
242
247
  var playType = "";
@@ -375,26 +380,34 @@ var require_ckplayer = __commonJS({
375
380
  } else {
376
381
  return embed(obj2);
377
382
  }
383
+ }, isUnreliablePluginBase = function(base2) {
384
+ if (!base2 || valType(base2) != "string") {
385
+ return true;
386
+ }
387
+ return /\/(?:assets|\.vite\/deps|_next\/static|chunks?)\/?$/i.test(base2);
378
388
  }, resolveAssetPath = function(folder, file) {
379
- var base = "";
389
+ var base2 = "";
380
390
  if (vars && vars["pluginPath"]) {
381
- base = vars["pluginPath"];
391
+ base2 = vars["pluginPath"];
382
392
  } else if (typeof window !== "undefined" && window.ckplayerBasePath) {
383
- base = window.ckplayerBasePath;
393
+ base2 = window.ckplayerBasePath;
394
+ }
395
+ if (base2 && isUnreliablePluginBase(base2)) {
396
+ base2 = "";
384
397
  }
385
- if (base) {
386
- if (base.slice(-1) !== "/") {
387
- base += "/";
398
+ if (base2) {
399
+ if (base2.slice(-1) !== "/") {
400
+ base2 += "/";
388
401
  }
389
- return base + folder + "/" + file;
402
+ return base2 + folder + "/" + file;
390
403
  }
391
404
  var fromScript = getPath(folder) + file;
392
- if (fromScript && fromScript.indexOf(folder + "/") > -1 && fromScript.indexOf("undefined") === -1) {
405
+ if (fromScript && fromScript.indexOf(folder + "/") > -1 && fromScript.indexOf("undefined") === -1 && !isUnreliablePluginBase(getPath())) {
393
406
  return fromScript;
394
407
  }
395
408
  var allowCdn = !vars || vars["cdnFallback"] !== false;
396
409
  var cdn = typeof window !== "undefined" && window.ckplayerPlusCdn ? window.ckplayerPlusCdn : ckplayerPlusDefaultCdn;
397
- if (!allowCdn || !cdn || cdn.indexOf("1.0.3") > -1) {
410
+ if (!allowCdn || !cdn || cdn.indexOf("1.0.6") > -1) {
398
411
  return folder + "/" + file;
399
412
  }
400
413
  if (cdn.slice(-1) !== "/") {
@@ -708,11 +721,24 @@ var require_ckplayer = __commonJS({
708
721
  }, hlsPlayer = function(url) {
709
722
  var startHls = function() {
710
723
  if (typeof Hls !== "undefined" && !isUndefined(Hls) && Hls.isSupported()) {
711
- hls = new Hls();
724
+ var hlsConf = {};
725
+ if (seekTime && seekTime > 0 && (isUndefined(vars["live"]) || !vars["live"])) {
726
+ hlsConf.startPosition = seekTime;
727
+ maxSeeTime = Math.max(maxSeeTime, seekTime);
728
+ if (firstSeekTime < 0) {
729
+ firstSeekTime = seekTime;
730
+ }
731
+ seekTime = 0;
732
+ hlsStartPositionUsed = true;
733
+ clearSeekRetry();
734
+ }
735
+ hls = new Hls(hlsConf);
712
736
  hls.loadSource(url);
713
737
  hls.attachMedia(video);
714
738
  hls.on(Hls.Events.MANIFEST_PARSED, function() {
715
- applyPendingSeek();
739
+ if (!hlsStartPositionUsed) {
740
+ applyPendingSeek();
741
+ }
716
742
  });
717
743
  hls.on(Hls.Events.ERROR, function(event2, data) {
718
744
  var code = 9;
@@ -730,6 +756,13 @@ var require_ckplayer = __commonJS({
730
756
  }
731
757
  }
732
758
  }
759
+ if (fatal && data.type === Hls.ErrorTypes.MEDIA_ERROR && hls && typeof hls.recoverMediaError === "function") {
760
+ try {
761
+ hls.recoverMediaError();
762
+ fatal = false;
763
+ } catch (errRecover) {
764
+ }
765
+ }
733
766
  CT.error = { code, message: message2 };
734
767
  eventTarget("error", CT.error);
735
768
  if (fatal) {
@@ -1411,8 +1444,28 @@ var require_ckplayer = __commonJS({
1411
1444
  checkTrack();
1412
1445
  }
1413
1446
  }
1414
- }, applyPendingSeek = function() {
1415
- if (!seekTime || !video) {
1447
+ }, timeInBuffered = function(t) {
1448
+ if (!video || isUndefined(video.buffered)) {
1449
+ return false;
1450
+ }
1451
+ try {
1452
+ var buf = video.buffered;
1453
+ for (var i2 = 0; i2 < buf.length; i2++) {
1454
+ if (t >= buf.start(i2) - 0.35 && t <= buf.end(i2) + 0.15) {
1455
+ return true;
1456
+ }
1457
+ }
1458
+ } catch (event2) {
1459
+ }
1460
+ return false;
1461
+ }, clearSeekRetry = function() {
1462
+ if (seekRetryTimer) {
1463
+ clearTimeout(seekRetryTimer);
1464
+ seekRetryTimer = null;
1465
+ }
1466
+ seekRetryCount = 0;
1467
+ }, applyPendingSeek = function(force) {
1468
+ if (!seekTime || !video || seekApplyLock) {
1416
1469
  return;
1417
1470
  }
1418
1471
  if (!isUndefined(vars["live"]) && (valType(vars["live"]) == "boolean" && vars["live"] || valType(vars["live"]) == "number")) {
@@ -1423,18 +1476,92 @@ var require_ckplayer = __commonJS({
1423
1476
  if (dur && isFinite(dur) && dur > 0 && t >= dur) {
1424
1477
  t = Math.max(0, dur - 0.3);
1425
1478
  }
1479
+ if (t <= 0) {
1480
+ seekTime = 0;
1481
+ clearSeekRetry();
1482
+ return;
1483
+ }
1484
+ var readyEnough = !isUndefined(video.readyState) && video.readyState >= 2;
1485
+ var bufferedOk = timeInBuffered(t);
1486
+ if (!force && !bufferedOk && (!readyEnough || video.readyState < 3)) {
1487
+ seekRetryCount++;
1488
+ if (seekRetryCount <= 50) {
1489
+ try {
1490
+ if (hls && typeof hls.startLoad === "function") {
1491
+ hls.startLoad(t);
1492
+ }
1493
+ } catch (event2) {
1494
+ }
1495
+ if (seekRetryTimer) {
1496
+ clearTimeout(seekRetryTimer);
1497
+ }
1498
+ seekRetryTimer = setTimeout(function() {
1499
+ applyPendingSeek(false);
1500
+ }, 200);
1501
+ return;
1502
+ }
1503
+ }
1504
+ clearSeekRetry();
1426
1505
  if (t > 0) {
1427
1506
  maxSeeTime = Math.max(maxSeeTime, t);
1428
1507
  if (firstSeekTime < 0) {
1429
1508
  firstSeekTime = t;
1430
1509
  }
1431
1510
  }
1511
+ seekApplyLock = true;
1432
1512
  seekTime = 0;
1433
1513
  try {
1434
1514
  video.currentTime = t;
1435
1515
  } catch (event2) {
1436
1516
  seekTime = t;
1517
+ seekApplyLock = false;
1518
+ }
1519
+ setTimeout(function() {
1520
+ seekApplyLock = false;
1521
+ }, 800);
1522
+ }, recoverDecodeError = function() {
1523
+ if (decodeRecoverCount >= 2) {
1524
+ return false;
1525
+ }
1526
+ decodeRecoverCount++;
1527
+ var resumeAt = playTime > 0 ? playTime : vars["seek"] ? parseFloat(vars["seek"]) : 0;
1528
+ if (isNaN(resumeAt) || resumeAt < 0) {
1529
+ resumeAt = 0;
1530
+ }
1531
+ try {
1532
+ if (hls && typeof hls.recoverMediaError === "function") {
1533
+ hls.recoverMediaError();
1534
+ if (resumeAt > 0) {
1535
+ seekTime = resumeAt;
1536
+ seekApplyLock = false;
1537
+ setTimeout(function() {
1538
+ applyPendingSeek(true);
1539
+ }, 300);
1540
+ }
1541
+ return true;
1542
+ }
1543
+ } catch (event2) {
1437
1544
  }
1545
+ try {
1546
+ var soft = Math.max(0, resumeAt - 1.5);
1547
+ seekTime = resumeAt;
1548
+ seekApplyLock = false;
1549
+ if (video) {
1550
+ video.currentTime = soft;
1551
+ }
1552
+ setTimeout(function() {
1553
+ applyPendingSeek(true);
1554
+ if (video && video.paused && vars["autoplay"]) {
1555
+ try {
1556
+ video.play();
1557
+ } catch (e2) {
1558
+ }
1559
+ }
1560
+ }, 400);
1561
+ return true;
1562
+ } catch (event2) {
1563
+ }
1564
+ return false;
1438
1565
  }, loadedMetaData = function() {
1439
1566
  eventTarget("loadedMetaData", {
1440
1567
  width: CK.getWidth(),
@@ -1611,6 +1738,11 @@ var require_ckplayer = __commonJS({
1611
1738
  if (!isUndefined(errorInfo.message)) {
1612
1739
  msg = errorInfo.message;
1613
1740
  }
1741
+ if (code === 3 && recoverDecodeError()) {
1742
+ CT.error = { code, message: msg };
1743
+ eventTarget("error", CT.error);
1744
+ return;
1745
+ }
1614
1746
  CT.error = { code, message: msg };
1615
1747
  showError();
1616
1748
  } else {
@@ -1660,6 +1792,8 @@ var require_ckplayer = __commonJS({
1660
1792
  eventTarget("error", CT.error);
1661
1793
  },
1662
1794
  seeked: function() {
1795
+ seekApplyLock = false;
1796
+ decodeRecoverCount = 0;
1663
1797
  if (paused) {
1664
1798
  player.play();
1665
1799
  }
@@ -2611,10 +2745,6 @@ var require_ckplayer = __commonJS({
2611
2745
  firstSeekTime = target;
2612
2746
  }
2613
2747
  }
2614
- try {
2615
- video.currentTime = target;
2616
- } catch (event2) {
2617
- }
2618
2748
  return player;
2619
2749
  }
2620
2750
  if (duration && isFinite(duration) && duration > 0 && target > duration) {
@@ -2626,6 +2756,11 @@ var require_ckplayer = __commonJS({
2626
2756
  if (vars["timeScheduleAdjust"] == 5 && target > maxSeeTime && seekTime && Math.abs(seekTime - target) < 0.5) {
2627
2757
  maxSeeTime = Math.max(maxSeeTime, target);
2628
2758
  }
2759
+ if (target > 0.5 && !timeInBuffered(target) && video.readyState < 3) {
2760
+ seekTime = target;
2761
+ applyPendingSeek(false);
2762
+ return player;
2763
+ }
2629
2764
  try {
2630
2765
  video.currentTime = target;
2631
2766
  seekTime = 0;
@@ -5785,7 +5920,7 @@ var require_ckplayer = __commonJS({
5785
5920
  }
5786
5921
  varsTime += time;
5787
5922
  return language["lookBack"] + date("H:i:s", varsTime);
5788
- }, loadJs = function(file, callback2) {
5923
+ }, loadJs = function(file, callback2, isRetry) {
5789
5924
  var fn2 = function() {
5790
5925
  };
5791
5926
  if (!isUndefined(callback2)) {
@@ -5820,19 +5955,37 @@ var require_ckplayer = __commonJS({
5820
5955
  ;
5821
5956
  }, 10);
5822
5957
  };
5823
- var w3c = function() {
5824
- if (valType(fn2) == "function") {
5825
- fn2();
5958
+ var removeScript = function() {
5959
+ try {
5960
+ if (script.parentNode) {
5961
+ script.parentNode.removeChild(script);
5962
+ }
5963
+ } catch (event2) {
5826
5964
  }
5965
+ };
5966
+ var w3c = function() {
5827
5967
  removeListener(script, "load", w3c);
5828
5968
  removeListener(script, "error", w3cError);
5969
+ doReady();
5829
5970
  };
5830
5971
  var w3cError = function() {
5831
- if (valType(fn2) == "function") {
5832
- fn2();
5833
- }
5834
5972
  removeListener(script, "load", w3c);
5835
5973
  removeListener(script, "error", w3cError);
5974
+ removeScript();
5975
+ if (!isRetry && (!vars || vars["cdnFallback"] !== false)) {
5976
+ var cdn = typeof window !== "undefined" && window.ckplayerPlusCdn ? window.ckplayerPlusCdn : ckplayerPlusDefaultCdn;
5977
+ var m2 = String(file).match(/\/(hls\.js|flv\.js|mpegts\.js|language)\/([^/?#]+)(?:[?#].*)?$/i);
5978
+ if (cdn && cdn.indexOf("1.0.6") === -1 && m2) {
5979
+ if (cdn.slice(-1) !== "/") {
5980
+ cdn += "/";
5981
+ }
5982
+ var retryUrl = cdn + m2[1] + "/" + m2[2];
5983
+ if (retryUrl !== file) {
5984
+ loadJs(retryUrl, callback2, true);
5985
+ return;
5986
+ }
5987
+ }
5988
+ }
5836
5989
  };
5837
5990
  try {
5838
5991
  addListener(script, "load", w3c);
@@ -6058,14 +6211,19 @@ var require_ckplayer = __commonJS({
6058
6211
  }
6059
6212
  function getPath(siz) {
6060
6213
  if (typeof window !== "undefined" && window.ckplayerBasePath) {
6061
- var base = window.ckplayerBasePath;
6062
- if (base.slice(-1) !== "/") {
6063
- base += "/";
6214
+ var base2 = window.ckplayerBasePath;
6215
+ if (/\/(?:assets|\.vite\/deps|_next\/static|chunks?)\/?$/i.test(base2)) {
6216
+ base2 = "";
6064
6217
  }
6065
- if (!isUndefined(siz)) {
6066
- return base + siz + "/";
6218
+ if (base2) {
6219
+ if (base2.slice(-1) !== "/") {
6220
+ base2 += "/";
6221
+ }
6222
+ if (!isUndefined(siz)) {
6223
+ return base2 + siz + "/";
6224
+ }
6225
+ return base2;
6067
6226
  }
6068
- return base;
6069
6227
  }
6070
6228
  var scriptList = document.scripts || [], thisPath = scriptList.length ? scriptList[scriptList.length - 1].src : "";
6071
6229
  for (var i2 = 0; i2 < scriptList.length; i2++) {
@@ -6587,13 +6745,17 @@ var import_ckplayer = __toESM(require_ckplayer());
6587
6745
  var import_meta = {};
6588
6746
  try {
6589
6747
  if (typeof window !== "undefined" && !window.ckplayerBasePath) {
6590
- window.ckplayerBasePath = new URL("./", import_meta.url).href;
6748
+ base = new URL("./", import_meta.url).href;
6749
+ if (/\/dist\/$/i.test(base) || /\/ckplayer-plus(?:@[^/]+)?\/(?:dist\/)?$/i.test(base) || /\/node_modules\/ckplayer-plus\/(?:dist\/)?$/i.test(base)) {
6750
+ window.ckplayerBasePath = base;
6751
+ }
6591
6752
  }
6592
6753
  } catch (e) {
6593
6754
  }
6755
+ var base;
6594
6756
  var index_default = import_ckplayer.default;
6595
6757
 
6596
- // src/react/CkPlayer.js
6758
+ // src/shared.js
6597
6759
  var EVENT_MAP = {
6598
6760
  play: "onPlay",
6599
6761
  pause: "onPause",
@@ -6609,6 +6771,34 @@ var EVENT_MAP = {
6609
6771
  function createUid() {
6610
6772
  return "ckp-" + Math.random().toString(36).slice(2, 10);
6611
6773
  }
6774
+ function buildPlayerConfig(input) {
6775
+ var opts = input.options || {};
6776
+ var config = {
6777
+ container: input.container,
6778
+ video: input.video,
6779
+ poster: input.poster || "",
6780
+ autoplay: !!input.autoplay,
6781
+ live: input.live != null ? input.live : false,
6782
+ plug: input.plug || "",
6783
+ autoPlug: input.autoPlug !== false,
6784
+ volume: input.volume != null ? input.volume : 0.8,
6785
+ cookie: opts.cookie || input.cookie || createUid()
6786
+ };
6787
+ if (input.pluginPath) {
6788
+ config.pluginPath = input.pluginPath;
6789
+ }
6790
+ for (var key in opts) {
6791
+ if (Object.prototype.hasOwnProperty.call(opts, key) && key !== "container") {
6792
+ config[key] = opts[key];
6793
+ }
6794
+ }
6795
+ if (typeof config.loaded === "string") {
6796
+ delete config.loaded;
6797
+ }
6798
+ return config;
6799
+ }
6800
+
6801
+ // src/react/CkPlayer.js
6612
6802
  var CkPlayer = (0, import_react.forwardRef)(function CkPlayer2(props, ref) {
6613
6803
  var containerRef = (0, import_react.useRef)(null);
6614
6804
  var playerRef = (0, import_react.useRef)(null);
@@ -6623,47 +6813,30 @@ var CkPlayer = (0, import_react.forwardRef)(function CkPlayer2(props, ref) {
6623
6813
  var autoPlug = props.autoPlug;
6624
6814
  var volume = props.volume;
6625
6815
  var pluginPath = props.pluginPath;
6626
- var options = props.options;
6627
6816
  var width = props.width != null ? props.width : "100%";
6628
6817
  var height = props.height != null ? props.height : "400px";
6629
6818
  var className = props.className || "";
6630
6819
  var style = props.style;
6631
6820
  (0, import_react.useEffect)(function() {
6632
- var cancelled = false;
6633
6821
  var el = containerRef.current;
6634
6822
  if (typeof window === "undefined" || !el) {
6635
6823
  return void 0;
6636
6824
  }
6637
- var opts = propsRef.current && propsRef.current.options || {};
6638
- var config = {
6825
+ var current = propsRef.current || {};
6826
+ var config = buildPlayerConfig({
6639
6827
  container: el,
6640
- video: propsRef.current.video,
6641
- poster: propsRef.current.poster || "",
6642
- autoplay: !!propsRef.current.autoplay,
6643
- live: propsRef.current.live != null ? propsRef.current.live : false,
6644
- plug: propsRef.current.plug || "",
6645
- autoPlug: propsRef.current.autoPlug !== false,
6646
- volume: propsRef.current.volume != null ? propsRef.current.volume : 0.8,
6647
- cookie: opts.cookie || instanceIdRef.current
6648
- };
6649
- if (propsRef.current.pluginPath) {
6650
- config.pluginPath = propsRef.current.pluginPath;
6651
- }
6652
- Object.keys(opts).forEach(function(key) {
6653
- if (key !== "container") {
6654
- config[key] = opts[key];
6655
- }
6828
+ video: current.video,
6829
+ poster: current.poster,
6830
+ autoplay: current.autoplay,
6831
+ live: current.live,
6832
+ plug: current.plug,
6833
+ autoPlug: current.autoPlug,
6834
+ volume: current.volume,
6835
+ pluginPath: current.pluginPath,
6836
+ options: current.options,
6837
+ cookie: instanceIdRef.current
6656
6838
  });
6657
- if (typeof config.loaded === "string") {
6658
- delete config.loaded;
6659
- }
6660
6839
  var player2 = new index_default(config);
6661
- if (cancelled) {
6662
- if (player2 && typeof player2.remove === "function") {
6663
- player2.remove();
6664
- }
6665
- return void 0;
6666
- }
6667
6840
  playerRef.current = player2;
6668
6841
  Object.keys(EVENT_MAP).forEach(function(name) {
6669
6842
  if (typeof player2[name] === "function") {
@@ -6679,7 +6852,6 @@ var CkPlayer = (0, import_react.forwardRef)(function CkPlayer2(props, ref) {
6679
6852
  propsRef.current.onReady(player2);
6680
6853
  }
6681
6854
  return function cleanup() {
6682
- cancelled = true;
6683
6855
  if (playerRef.current && typeof playerRef.current.remove === "function") {
6684
6856
  try {
6685
6857
  playerRef.current.remove();
@@ -6688,7 +6860,12 @@ var CkPlayer = (0, import_react.forwardRef)(function CkPlayer2(props, ref) {
6688
6860
  }
6689
6861
  playerRef.current = null;
6690
6862
  };
6691
- }, [video2, poster, autoplay, live, plug, autoPlug, volume, pluginPath]);
6863
+ }, [video2, poster, autoplay, live, plug, autoPlug, pluginPath]);
6864
+ (0, import_react.useEffect)(function() {
6865
+ if (playerRef.current && typeof volume === "number" && typeof playerRef.current.volume === "function") {
6866
+ playerRef.current.volume(volume);
6867
+ }
6868
+ }, [volume]);
6692
6869
  (0, import_react.useImperativeHandle)(ref, function() {
6693
6870
  return {
6694
6871
  play: function() {