【问题标题】:How to Play Video from asset in Webview Android如何在 Webview Android 中播放资源中的视频
【发布时间】:2014-06-13 04:07:37
【问题描述】:

我的activity_main.xml

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true" />

我的主要活动

ws = (WebSettings) wb.getSettings();
wb.setWebChromeClient(new WebChromeClient());
ws.setJavaScriptEnabled(true);
wb.loadUrl("File:///android_asset/www/html5.html");

我的html

<source src="playlist.mp4" type="video/mp4">

我正在从资产加载视频。目录我的视频资产/www/playlist.mp4 但它不起作用..

【问题讨论】:

标签: android


【解决方案1】:

如何在 Webview Android 中播放资源中的视频。

we can't play video in webview from asset folder. even its very difficult to play video 
from server url in webview. all we can do is to make a custom HTML5 webview and then we  
can play.but this is very long process.

建议与解决方案

最好使用android提供的videoview。把你的视频放在raw文件夹而不是assets中。使用下面的代码在你的应用程序中播放来自Raw文件夹的视频:

  getWindow().setFormat(PixelFormat.TRANSLUCENT);
  VideoView _view= new VideoView(this);
  _view.setMediaController(new MediaController(this));
  Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
  + R.raw.your_raw_file); //add file without any extension
  _view.setVideoURI(video);
  setContentView(_view);
  _view.start();

【讨论】:

    【解决方案2】:

    你最好使用VideoView

    VideoView videoHolder = new VideoView(this);
      setContentView(videoHolder);
      Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
        + R.raw.splash); // you file name
      videoHolder.setVideoURI(video);
    

    Reference Link to followthis

    【讨论】:

    • res.getString(R.string.XXXXXX) 是网址吗?
    • 您的链接不存在 :D
    【解决方案3】:

    我正在使用不同的方法。我没有将视频放入 assets 文件夹,而是对其进行 base64 编码并将其直接放入 html 文件/源代码中。 由于资产文件夹中的视频已修复,因此它们无论如何都不会更改,html文件也可能不会更改。所以,为什么不直接把视频放到html页面中呢。 效果是一样的,没有问题。

    示例:http://iandevlin.com/html5/data-uri/video.php

    Base64 编码器(在线):http://www.opinionatedgeek.com/dotnet/tools/base64encode/

    (它并没有真正回答关于资产文件夹中视频播放的问题,但结果是一样的恕我直言。)

    【讨论】:

    • 这是一个有趣的方法。
    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2013-07-20
    • 1970-01-01
    • 2014-07-22
    • 2013-12-18
    相关资源
    最近更新 更多