【问题标题】:Electron video cannot play电子视频无法播放
【发布时间】:2019-07-31 03:01:28
【问题描述】:

我有

<video id="video_player">
    <source id="video_player_source"/>
</video>

在我的 html 文件中,并且

const videoPlayer = document.getElementById("video_player") as HTMLVideoElement;
const videoPlayerSource = document.getElementById("video_player_source") as HTMLElement;
videoPlayer.setAttribute("style", `top: 0; left: 0; width: 100%; `);
videoPlayerSource.setAttribute("src", "https://www.html5rocks.com/en/tutorials/track/basics/treeOfLife/video/developerStories-en.webm");
videoPlayer.play();

在我的渲染器进程中(视频是演示视频)。 视频确实加载了(屏幕上显示了一帧视频),但是当我拨打 play() 时它没有播放。

我该如何解决这个问题?谢谢。

【问题讨论】:

    标签: typescript electron


    【解决方案1】:

    MediaElement.play() 返回一个 Promise 的版本:

    火狐:53

    'Chrome for Desktop'、'Chrome for Android'、'Android WebView':50

    'Opera','Opera for Android':37

    iOS Safari:iOS 10

    桌面 Safari:2017 年 6 月,所以可能是 v10.1.1

    所以要解决这个问题,你可以这样做

    HTML

    <video id="video_player">
        <source id="video_player_source"/>
    </video>
    

    JS

    const videoPlayer = document.getElementById("video_player");
    const videoPlayerSource = document.getElementById("video_player_source");
    videoPlayer.setAttribute("style", `top: 0; left: 0; width: 100%; `);
    videoPlayerSource.setAttribute("src","https://www.html5rocks.com/en/tutorials/track/basics/treeOfLife/video/developerStories-en.webm");
    videoPlayer.load();
    var playPromise = videoPlayer.play();
    
    // In browsers that don’t yet support this functionality,
    // playPromise won’t be defined.
    if (playPromise !== undefined) {
      playPromise.then(function() {
        // Automatic playback started!
      }).catch(function(error) {
        // Automatic playback failed.
        // Show a UI element to let the user manually start playback.
      });
    }
    

    【讨论】:

    • 是否可以在 Electron 中设置一个 Chromium 策略 AutoplayAllowed,这样即使用户没有手动开始播放,自动播放也会始终成功?
    • 对不起,我没试过所以我不知道。但据我所知,我提供的解决方案可以自动播放视频
    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 2020-06-04
    • 2014-01-19
    相关资源
    最近更新 更多