gpt4 book ai didi

android - 在 webview 中加载 flv 视频的问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:40 28 4
gpt4 key购买 nike

我想在 WebView 中加载 .flv 视频。

我已经从this link获得了帮助,但问题是我无法在模拟器中观看视频。

这是我的代码:

package com.FlvTester;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.FrameLayout;

public class FlvTester extends Activity {

WebView webView;
String htmlPre = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"></head><body style='margin:0; pading:0; background-color: black;'>";
String htmlCode =
" <embed style='width:100%; height:100%' src='http://www.platipus.nl/flvplayer/download/1.0/FLVPlayer.swf?fullscreen=true&video=@VIDEO@' " +
" autoplay='true' " +
" quality='high' bgcolor='#000000' " +
" name='VideoPlayer' align='middle'" + // width='640' height='480'
" allowScriptAccess='*' allowFullScreen='true'" +
" type='application/x-shockwave-flash' " +
" pluginspage='http://www.macromedia.com/go/getflashplayer' />" +
"";
String htmlPost = "</body></html>";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

webView = (WebView)findViewById(R.id.webview);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginsEnabled(true);


htmlCode = htmlCode.replaceAll("@VIDEO@", "file:///android_asset/expression_sad.flv");
webView.loadDataWithBaseURL("file:///android_asset/expression_sad.flv", htmlPre+htmlCode+htmlPost, "text/html", "UTF-8", null);
}




@Override
protected void onPause(){
super.onPause();

callHiddenWebViewMethod("onPause");

webView.pauseTimers();
if(isFinishing()){
webView.loadUrl("about:blank");
setContentView(new FrameLayout(this));
}
}

@Override
protected void onResume(){
super.onResume();

callHiddenWebViewMethod("onResume");

webView.resumeTimers();
}

private void callHiddenWebViewMethod(String name){
// credits: http://stackoverflow.com/questions/3431351/how-do-i-pause-flash- content-in-an-android-webview-when-my-activity-isnt-visible
if( webView != null ){
try {
Method method = WebView.class.getMethod(name);
method.invoke(webView);
} catch (NoSuchMethodException e) {
//Lo.g("No such method: " + name + e);
} catch (IllegalAccessException e) {
//Lo.g("Illegal Access: " + name + e);
} catch (InvocationTargetException e) {
//Lo.g("Invocation Target Exception: " + name + e);
}
}
}

}

我在 log cat 中遇到了这个错误

 07-06 12:00:48.567: WARN/dalvikvm(381): threadid=1: thread exiting with uncaught     exception (group=0x4001d800)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): FATAL EXCEPTION: main
07-06 12:00:48.597: ERROR/AndroidRuntime(381): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.FlvTester/com.FlvTester.FlvTester}: java.lang.ClassCastException: android.widget.VideoView
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.os.Looper.loop(Looper.java:123)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at java.lang.reflect.Method.invoke(Method.java:521)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at dalvik.system.NativeStart.main(Native Method)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): Caused by: java.lang.ClassCastException: android.widget.VideoView
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at com.FlvTester.FlvTester.onCreate(FlvTester.java:31)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-06 12:00:48.597: ERROR/AndroidRuntime(381): ... 11 more

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<WebView android:layout_width="match_parent" android:id="@+id/webview" android:layout_height="match_parent"></WebView>
</LinearLayout>

更新

this is new image

最佳答案

您没有明确说明您的 Android 版本,但我可以确认 flash 插件确实加载并且电影播放器​​确实嵌入了漩涡形状的图形。我可以让它进入全屏模式。我也可以播放在线视频,但不能播放应用程序 Assets 文件夹中的视频,但我有一个解决方法。

您的 manifest.xml 中需要以下权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission> <!-- where required -->
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

有趣的是,我发现我只能在静态加载 html 时从 Assets 文件夹运行 SWF 播放器,即当我创建一个 html 文件并使用 loadUrl() 直接从 Assets 文件夹加载它时。当使用 loadDataBaseUrl 方法加载 html 数据时,似乎有一些东西阻止了对应用程序目录的访问。

要成功地从本地设备播放视频,静态 html 文件和视频必须位于 sdcard 上。我唯一的结论是,出于安全原因, Assets 文件夹对嵌入式插件是禁止访问的。

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
webView.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/FLVplayer/index.html");
}

这是html文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body style="margin:0; pading:0; background-color: black;">
<embed
style="width:100%; height:100%"
src="./FLVPlayer.swf?fullscreen=true&video=./expression_sad.flv"
autoplay="true"
quality="high"
bgcolor="#000000"
name="VideoPlayer"
align="middle"
allowScriptAccess="*"
allowFullScreen="true"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
</embed>
</body>
</html>

这是一个 100% 有效的解决方案,但在调用 loadUrl 以动态替换视频文件名之前,您必须将 html 文件写入 sdcard。此外,您应该将 Assets 文件夹中的视频复制到 SD 卡上的相同路径中(正如您从我的例子中看到的那样,我创建了一个名为 FLVplayer 的文件夹。

在运行 Froyo(2.2) 的 Samsung Galaxy Tab 上进行的测试

更新

我包括了一个已经过测试和工作的完整 Activity 。这需要将 Flash 播放器、FLV 文件和 index.html 文件的副本放置在 SD 卡根目录下名为“FLVplayer”的文件夹中,您可以使用文件资源管理器复制该文件夹。您可以编辑各种实用程序函数来修改从 Assets 文件夹复制文件的行为

package com.FLVplayer;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.webkit.WebView;
import android.widget.FrameLayout;

public class FLVplayerActivity extends Activity {

private static final String TAG="FLVplayer";
private static WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

webView = (WebView)findViewById(R.id.webview);

//WebView webview = new WebView(this);
//setContentView(webview);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginsEnabled(true);

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {

//prepare the directory
File flvDirectory = new File(Environment.getExternalStorageDirectory(),"FLVplayer");

if(!flvDirectory.exists())
try {
flvDirectory.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

//specify the html file name to use
File flvHtml = new File(flvDirectory,"index.html");

//copy what you need
copySwfAsset(flvDirectory, "FLVPlayer.swf");
copyFlvAsset(flvDirectory, "20051210-w50s.flv");

//render the html
createVideoHtml(flvHtml, "20051210-w50s.flv");

//load the content into the webview
webView.loadUrl(Uri.fromFile(flvHtml).toString());

//sit back, watch FLV and eat popcorn
}
}

private void createVideoHtml(File htmlFile, String htmlFileName)
{
//TODO render your own html file to sdcard
}

private void copySwfAsset(File flvDirectory, String flashFileName)
{
//TODO copy your own SWF video player file from assets
}

private void copyFlvAsset(File flvDirectory, String videoFileName)
{
//TODO copy your oown FLV file from asset folder
}

@Override
protected void onPause(){
super.onPause();

callHiddenWebViewMethod("onPause");

webView.pauseTimers();
if(isFinishing()){
webView.loadUrl("about:blank");
setContentView(new FrameLayout(this));
}
}

@Override
protected void onResume(){
super.onResume();

callHiddenWebViewMethod("onResume");

webView.resumeTimers();
}

private void callHiddenWebViewMethod(String name){
// credits: http://stackoverflow.com/questions/3431351/how-do-i-pause-flash- content-in-an-android-webview-when-my-activity-isnt-visible
if( webView != null ){
try {
Method method = WebView.class.getMethod(name);
method.invoke(webView);
} catch (NoSuchMethodException e) {
//Lo.g("No such method: " + name + e);
} catch (IllegalAccessException e) {
//Lo.g("Illegal Access: " + name + e);
} catch (InvocationTargetException e) {
//Lo.g("Invocation Target Exception: " + name + e);
}
}
}

}

附录

我相信可以通过拦截各种文件的 URL 并使用 WebViewClient.shouldInterceptRequest() 直接从 Assets 文件夹返回数据来绕过安全问题。但这需要 API 11,因此不适合作为通用解决方案。

关于android - 在 webview 中加载 flv 视频的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6596243/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com