【问题标题】:How can i play video from full path(system location) using jsp and javascript?如何使用 jsp 和 javascript 从完整路径(系统位置)播放视频?
【发布时间】:2019-08-26 08:49:20
【问题描述】:

您好,我无法从系统位置播放 jsp 中的视频文件。但是,如果我将视频文件(example.mp4)放在 web-content 下,并在 jsp 中使用带有 fileName 的视频标签,如下所示,它将开始播放。 以下代码无效

src="C:/Users/DMS/Documents/NetBeansProjects/VideoView/web/video/example.mp4"

当我使用以下格式时,我可以播放视频

src="video/example.mp4"; //video文件夹在WEB-INF下

其他问题是当用户点击(x)或点击模态框外的任何地方时,模态框关闭,但视频在模态框后面播放时不可见。 如何在后台暂停或关闭视频?

完整代码如下
video.jsp

<%@page import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

 <link rel="stylesheet" type="text/css" href="css/modal.css" media="screen" />
 <link rel="stylesheet" type="text/css" href="css/hover.css" media="screen" />

</head>
<body>

<h2>Video Popup</h2>

<!-- Trigger/Open The Modal -->
<div class="dropdown">

  <button class="dropbtn">Dropdown</button>
  
  <div id="dcont" class="dropdown-content">
        <%
        String[] arr=new String[10];
        arr[1]="C:/Users/DMS/Documents/NetBeansProjects/VideoView/web/video/example.mp4";
        arr[2]="video/sample.mp4";
        arr[3]="video/example.mp4";

        %> 
        <a>
       <%for(int i=1;i<4;i++){%>  
     
       <input id="myBtn<%=i%>" onclick="javascript:vidsub(this.id)" type="submit" value="<%=arr[i]%>">
      
       
       <%}%>
        </a>
  </div>
     
</div>

 
<!-- The Modal -->
<div id="myModal" class="modal">
   
    <div class='modal-content'>
      <!-- Modal content -->
      <span class='close'>&times;</span>
      <div id="mcont"></div>      
    </div>
</div>

    <script>
            // Get the modal
            var modal = document.getElementById("myModal");   
           
            // Get the <span> element that closes the modal
            var span = document.getElementsByClassName("close")[0];

            // When the user clicks the button, open the modal
            function vidsub(clk) {
          
            var btn = document.getElementById(clk);
             
            var aval=btn.value;
                
            var temp=JSON.stringify(aval);
               
            document.getElementById("mcont").innerHTML =" <video autoplay id='vid' loop controls width='100%' height='100%'> <source src="+temp+" type='video/mp4'> <object data='js/video-js.swf' width='720' height='480'></object> </video>";
      
             modal.style.display = "block";
            };

            // When the user clicks on <span> (x), close the modal
        
            span.onclick = function() {
              modal.style.display = "none";
              x.pause();
            }

            // When the user clicks anywhere outside of the modal, close it
            window.onclick = function(event) {
              if (event.target == modal) {
                modal.style.display = "none";
              }
            }
    </script>
</body>
</html>
modal.css

/*modal.css */

body {font-family: Arial, Helvetica, sans-serif;}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: #666666; /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  background-color: #666666;
  margin: auto;
  padding: 20px;
  border:none;
  width: 50%;
}

/* The Close Button */
.close {
  color: #fff;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
    color: #ff3399;
  text-decoration: none;
  cursor: pointer;
}

【问题讨论】:

    标签: javascript html jsp video


    【解决方案1】:

    (1)使用本地文件作为元素源...

    “以下代码无效:
    src="C:/Users/DMS/Documents/NetBeansProjects/VideoView/web/video/example.mp4"

    尝试使用file,因为本地磁盘上没有可用的http

    src="file:///C:/Users/DMS/Documents/NetBeansProjects/VideoView/web/video/example.mp4"
    

    (2) 模态关闭时移除视频...

    您的视频是通过以下方式创建的:.innerHTML 方法,该方法动态创建 video&gt; 标记。

    a) 您可以尝试将其设为空白(或为空):

    document.getElementById("mcont").innerHTML ="";
    

    b) 您可以尝试暂停视频元素:

    var myVid = document.getElementById("vid");
    myVid.pause();
    

    示例代码:
    (一个是空白innerHTML,另一个是&lt;video&gt; pause,看看哪种方法最适合你):

    //# When the user clicks on <span> (x), close the modal
    span.onclick = function() 
    {
        document.getElementById("mcont").innerHTML =""; 
        modal.style.display = "none";
        x.pause();
    }
    
    //# When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) 
    {
        if (event.target == modal) 
        {
            document.getElementById("vid").pause();
            modal.style.display = "none";
        }
    }
    

    【讨论】:

    • 暂停视频元素对我有用。但arr[1]="file:///C:/Users/DMS/Documents/NetBeansProjects/VideoView/web/video/example.mp4"; 不起作用
    • var temp=JSON.stringify(aval); is 获取数组的值,但是当它放在源上时 ....like document.getElementById("mcont").innerHTML =" &lt;video autoplay id='vid' loop controls width='100%' height='100%'&gt; &lt;source src="+temp+" type='video/mp4'&gt; &lt;object data='js/video-js.swf' width='720' height='480'&gt;&lt;/object&gt; &lt;/video&gt;"; can't play video 。解决办法是什么?
    • 有什么解决办法吗?
    • 您使用的是哪个浏览器?对于 Chrome,加载 HTML 文件本身也必须位于 C 驱动器上的某个位置。如果 HTML 在线,则 (a) 用户必须通过文件浏览弹出窗口自己选择视频或 (b) 使用 PHP 访问本地文件并代理它.
    • 我正在使用 firefox。我在其他代码中尝试了 file(不在 script 中)并且它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2017-09-30
    • 2013-11-02
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多