【问题标题】:Phonegap / Cordova Video Player for all video files on mobile手机上所有视频文件的 Phonegap / Cordova 视频播放器
【发布时间】:2015-04-24 08:42:20
【问题描述】:

我是移动应用程序开发的新手,我正在尝试在 html5 的帮助下使用 cordova/phonegap 制作视频播放器。经过大量搜索,我意识到没有原生android播放视频是不可能的。请在这方面帮助我,我希望我的应用程序从移动存储和 SD 卡中获取所有视频文件并列出所有这些文件。单击自定义视频播放器后开始播放。
我首先尝试了这个插件
https://github.com/amuelli/Html5Video
但没有成功。
然后,我尝试了其他可用的视频插件,但仍然没有成功。我在这里粘贴的代码给出了一个丑陋的输出,但这就是我已经达到的,这段代码没有任何插件。

<!DOCTYPE html>

<html>
<head>
<style>
    div#video_player_box{ width:550px; background:#000; margin:0px auto;}
    div#video_controls_bar{ background: #333; padding:10px; color:#CCC;
    font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
        button#playpausebtn{
            background:url(pause.png);
            border:none;
            width:16px;
            height:18px;
            cursor:pointer;
            opacity:0.5;
        }
        button#playpausebtn:hover{ opacity:1; }
        input#seekslider{ width:180px; }
        input#volumeslider{ width: 80px;}
        input[type='range'] {
            -webkit-appearance: none !important;
            background: #000;
            border:#666 1px solid;
            height:4px;
        }
        input[type='range']::-webkit-slider-thumb {
            -webkit-appearance: none !important;
            background: #FFF;
            height:15px;
            width:15px;
            border-radius:100%;
            cursor:pointer;
        }
</style>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and   
      height=device-height attributes. See 
    https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport" content="user-scalable=no, initial-scale=1, 
    maximum-scale=1, minimum-scale=1, width=device-width, height=device-
    height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>
    <script>
    var vid, playbtn, seekslider, curtimetext, durtimetext, mutebtn, 
    volumeslider, fullscreenbtn;
    $(document).on('ready',function(){
    var video = $("#video");

    function handleFileSelect(evt) {
        var files = evt.target.files; // FileList object

        // Loop through the FileList and render image files as thumbnails.
        for (var i = 0, f; f = files[i]; i++) {

          // Only process image files.
          if (!f.type.match('video.*')) {
            continue;
          }



          var reader = new FileReader();

          // Closure to capture the file information.
          reader.onload = (function(theFile) {
            return function(e) {
            video.get(0).src = e.target.result;
            console.log(video.get(0));
            video.get(0).play();


              var span = document.createElement('span');
              span.innerHTML = "a video file";
              document.getElementById('list').insertBefore(span, null);
            };
          })(f);

          // Read in the image file as a data URL.
          reader.readAsDataURL(f);
        }
      }

      document.getElementById('files').addEventListener('change', 
      handleFileSelect, false);

    });


    function intializePlayer(){
    // Set object references
    vid = document.getElementById("my_video");
    vid.addEventListener('click',function(){
    vid.play();
    },false);
    playbtn = document.getElementById("playpausebtn");
    seekslider = document.getElementById("seekslider");
    curtimetext = document.getElementById("curtimetext");
    durtimetext = document.getElementById("durtimetext");
    mutebtn = document.getElementById("mutebtn");
    volumeslider = document.getElementById("volumeslider");
    fullscreenbtn = document.getElementById("fullscreenbtn");
    // Add event listeners
    playbtn.addEventListener("click",playPause,false);
    seekslider.addEventListener("change",vidSeek,false);
    vid.addEventListener("timeupdate",seektimeupdate,false);
    mutebtn.addEventListener("click",vidmute,false);
    volumeslider.addEventListener("change",setvolume,false);
    fullscreenbtn.addEventListener("click",toggleFullScreen,false);
    }
    window.onload = intializePlayer;
    function playPause(){
        if(vid.paused){
            vid.play();
            playbtn.style.background = "url(pause.png)";
        } else {
            vid.pause();
            playbtn.style.background = "url(play.png)";
    }
        }
    function vidSeek(){
    var seekto = vid.duration * (seekslider.value / 100);
    vid.currentTime = seekto;
    }
    function seektimeupdate(){
    var nt = vid.currentTime * (100 / vid.duration);
    seekslider.value = nt;
    var curmins = Math.floor(vid.currentTime / 60);
    var cursecs = Math.floor(vid.currentTime - curmins * 60);
    var durmins = Math.floor(vid.duration / 60);
    var dursecs = Math.floor(vid.duration - durmins * 60);
    if(cursecs < 10){ cursecs = "0"+cursecs; }
    if(dursecs < 10){ dursecs = "0"+dursecs; }
    if(curmins < 10){ curmins = "0"+curmins; }
    if(durmins < 10){ durmins = "0"+durmins; }
    curtimetext.innerHTML = curmins+":"+cursecs;
    durtimetext.innerHTML = durmins+":"+dursecs;
    }
    function vidmute(){
        if(vid.muted){
            vid.muted = false;
            mutebtn.innerHTML = "Mute";
        } else {
        vid.muted = true;
        mutebtn.innerHTML = "Unmute";
        }
    }
    function setvolume(){
    vid.volume = volumeslider.value / 100;
    }
    function toggleFullScreen(){
        if(vid.requestFullScreen){
            vid.requestFullScreen();
        } else if(vid.webkitRequestFullScreen){
            vid.webkitRequestFullScreen();
        } else if(vid.mozRequestFullScreen){
            vid.mozRequestFullScreen();
        }
    }
    </script>

</head>
<body>
    <div id="video_player_box">
        <input type="file" id="files" name="files[]" multiple />
        <output id="list"></output>
        <video id="my_video" width="550" height="310" 
        onClick="this.play();">
        <source src="" type="video/mp4">
        </video>
        <div id="video_controls_bar">
            <button id="playpausebtn"></button>
            <input id="seekslider" type="range" min="0" max="100" value="0" 
            step="1">
            <span id="curtimetext">00:00</span> / <span 
             id="durtimetext">00:00</span>
            <button id="mutebtn">Mute</button>
            <input id="volumeslider" type="range" min="0" max="100" 
            value="100" step="1">
            <button id="fullscreenbtn">[ &nbsp; ]</button>
        </div>
    </div>

</body>
</html>   

【问题讨论】:

  • 你确定你已经测试了官方的cordova媒体插件吗? github.com/apache/cordova-plugin-media
  • 不,我没试过。
  • Sithys,该插件提供了在设备上录制和播放音频文件的能力。对视频文件有帮助吗?
  • 对那个 Manu 感到抱歉,这是正确的。科尔多瓦视频播放器怎么样? github.com/moust/cordova-plugin-videoplayer
  • Sithys 你在帮我。请不要抱歉。而且恐怕这个特定的插件不支持文件获取。

标签: javascript android html cordova android-video-player


【解决方案1】:

Cordova 流媒体插件

https://github.com/nchutchind/Streaming-Media-Cordova-Plugin

这是用于在 Cordova 中显示视频的工具。
它适用于本地和远程流媒体视频。

使用起来非常简单。
还有这个 step-by-step guide 用于创建一个简单的演示应用程序,调用它是我制作的。

【讨论】:

  • 最后,一个视频解决方案可以处理数据目录file:///中的本地文件,谢谢!分步指南是一个很好的补充。我希望所有插件都包含一个演示,所以我们可以有一个应该可以工作的版本。在我的环境中运行演示应用程序清楚地表明插件应该适用于我正在使用的平台,只要我得到正确的代码。当然,示例代码也很不错。
  • 你好,我以前永远不会尝试播放视频。我在 ionic 平台上工作,并为 android 和 iOS 平台创建与视频新闻相关的应用程序。所以我面对的所有视频都来自 Web API,并在我的应用程序 UI 中以多个缩略图呈现。那么您认为哪个视频插件可以很好地满足我的要求。?或者你对此有什么想法?。
  • 如果您想全屏播放视频,或者如果您想播放存储在设备本地(或与您的应用程序捆绑)的视频文件,请使用此插件。如果您只想在页面中嵌入缩略图,并且所有视频都来自互联网,那么您可以使用 html5
  • 我对这个答案投了 2 票 - 如果你投了反对票,请评论一下为什么..
  • 如果我将视频与 html 文件放在一起,我应该在这个插件中使用什么文件路径
猜你喜欢
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多