Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:Can';在网络视图中解压或复制视频后,不能播放视频_Android_Video - Fatal编程技术网

Android 安卓:Can';在网络视图中解压或复制视频后,不能播放视频

Android 安卓:Can';在网络视图中解压或复制视频后,不能播放视频,android,video,Android,Video,我的目标是: 下载包含视频和JS文件的zip文件。创建一个运行JS文件的webview,其中包含播放该视频文件的视频标记 问题:当我尝试播放视频时,我收到一条错误信息:“对不起,无法播放此视频”。在logcat中,我得到:错误(1,-2147483648) 以下是解压缩文件的代码: String path = context.getFilesDir().getPath()+"/"; InputStream is; ZipInputStream zis; try { is = new Fi

我的目标是:

下载包含视频和JS文件的zip文件。创建一个运行JS文件的webview,其中包含播放该视频文件的视频标记

问题:当我尝试播放视频时,我收到一条错误信息:“对不起,无法播放此视频”。在logcat中,我得到:错误(1,-2147483648)

以下是解压缩文件的代码:

String path = context.getFilesDir().getPath()+"/";
InputStream is;
ZipInputStream zis;
try {
    is = new FileInputStream(zip file);
    zis = new ZipInputStream( new BufferedInputStream(is));          
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        String filename = ze.getName();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int count;

        FileOutputStream fout = 
        _context.openFileOutput( path + filename, Context.MODE_WORLD_READABLE );

    while ((count = zis.read(buffer)) != -1) {
            baos.write(buffer, 0, count);
            baos.toByteArray();
            fout.write(baos.toByteArray());             
            baos.reset();
        }
        fout.close();               
        zis.closeEntry();
        baos.close();
    }
    zis.close();    
}
要显示视频,请覆盖MraidWebChromeClient.onShowCustomView:

super.onShowCustomView(view, callback);
if (view instanceof FrameLayout) {
  FrameLayout frame = (FrameLayout) view;
  if (frame.getFocusedChild() instanceof VideoView) {
   VideoView video = (VideoView) frame.getFocusedChild();
   frame.removeView(video);
   Activity a = (Activity)getContext();
   a.setContentView(video);
   video.setOnCompletionListener(this);
   video.setOnErrorListener(this);
   video.start();
  }
}  
我不认为视频文件、路径或JS存在错误,因为:

  • 如果视频作为资源包含在res中或通过外部http链接进行流式传输,则可以正常播放
  • 加载js文件时,我使用loadDataWithBaseURL,所有其他图像元素都显示良好
  • 我已将文件从res复制到本地应用程序文件夹(使用与解压代码类似的代码),出现了相同的错误
我想:

  • 解压/复制文件时文件已损坏
  • 从webview播放本地视频时存在权限问题(即使我已将文件设置为world_readable)
对此问题的任何见解都将不胜感激

K

Ps:有人知道为什么在普通的网络浏览器中,它会下载.mp4链接,而在其他情况下,它会尝试流式传输它们吗

编辑: 在研究了这两篇文章后,似乎认为Android有安全性来防止这种情况: 将文件复制到公共外部系统工作正常

关于Ps:为什么有些视频流和一些视频被下载,Android 3不支持https流,所以会下载文件

  • 由于您正在从SD卡解压/读取视频,请确保在清单文件中添加以下行:

  • 由于您使用的是JS,请确保在WebView中按如下方式启用它:

    private WebView mWebView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ...
            ...
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.getSettings().setJavaScriptEnabled(true);
            ...
    }
    
    Intent i = new Intent();
    i.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File(PATH_TO_YOUR_VIDEO);
    i.setDataAndType(Uri.fromFile(file), "video/*");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    mContext.startActivity(i);
    
  • 至于视频播放,到目前为止,我找到的最可靠的解决方案是将其交给本机media player应用程序(因为我发现WebView真的有问题)。为了实现这一点,您需要这样,当用户点击视频缩略图时,您可以创建相应的意图并按如下方式启动media player应用程序:

    private WebView mWebView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ...
            ...
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.getSettings().setJavaScriptEnabled(true);
            ...
    }
    
    Intent i = new Intent();
    i.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File(PATH_TO_YOUR_VIDEO);
    i.setDataAndType(Uri.fromFile(file), "video/*");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    mContext.startActivity(i);
    
  • [更新]根据官方要求(参见“访问外部存储器上的文件”)

  • “如果您使用的是API级别8或更高级别的,请使用getExternalFilesDir()打开一个文件,该文件表示应保存文件的外部存储目录。”

    “如果您使用的是API级别7或更低级别的,请使用getExternalStorageDirectory(),打开表示外部存储根目录的文件。”


    解压代码包含行
    String path=context.getFilesDir().getPath()
    ,试着按照上面的方法修改它。

    为了缩小问题范围,试着把它放在外部存储上作为测试。这也可以让你用另一个应用程序来播放写出来的文件作为额外的有效性测试。是的,我们将文件提取到外部存储,效果很好。我们认为这是安卓安全问题。有什么值得注意的吗当logcat无法从内部存储中播放时?啊,很抱歉,我在原始帖子中留了一个空白,但从未回来填写。在logcat中,我得到的是:MediaPlayer错误(1,-2147483648)谢谢你的建议。不幸的是,我已经完成了第1点和第2点。理论上,代码很好,因为如果js给视频一个外部url流,或者给嵌入式原始资源一个文件uri,它就可以播放视频。它只能播放我手动复制或解压缩到本地文件存储的文件,例如:data/data//files@iakiak:请参阅上面的更新(第4点)。如果有效,请告诉我!:)再次感谢您的帮助。我们希望存储和播放应用程序本地存储中的文件。从这两个帖子来看:由于安全原因,这似乎是不可能的。如果按照您的建议将文件复制到外部存储器,则视频播放效果良好。