您没有明确说明您的 Android 版本,但我可以确认 Flash 插件确实加载,并且电影播放器确实带有漩涡形状图形。我可以将其设置为全屏模式。我还可以播放在线视频,但不能从应用程序的资产文件夹中播放,但我确实有一个解决方法。
您的manifest.xml 中需要以下权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <!-- where required -->
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
有趣的是,我发现我只能在静态加载 html 时(即,当我创建 html 文件并使用 loadUrl() 直接从 asset 文件夹加载该文件时)才能从 asset 文件夹运行 SWF 播放器。使用 loadDataBaseUrl 方法加载 html 数据时,似乎有一些东西阻止了对应用程序目录的访问。
要成功从本地设备播放视频,静态 html 文件和视频必须位于 SD 卡上。我唯一的结论是,出于安全原因,资产文件夹禁止嵌入插件。
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。此外,您应该将视频从资产文件夹复制到 SD 卡上的同一路径中(正如您从我的示例中看到的那样,我创建了一个名为 FLVplayer 的文件夹)。
在运行 Froyo(2.2) 的 Samsung Galaxy Tab 上进行的测试
UPDATE
我包括一个已经过测试和工作的完整活动。这需要将 Flash 播放器、FLV 文件和 index.html 文件的副本放置在 sdcard 根目录下名为“FLVplayer”的文件夹中,您可以使用文件资源管理器进行复制。您可以编辑各种实用程序功能来修改从 assetes 文件夹复制文件的行为
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);
}
}
}
}
ADENDUM
我相信可能有一种方法可以通过拦截各种文件的 URL 并使用以下命令直接从资产文件夹返回数据来绕过安全问题WebViewClient.shouldInterceptRequest()但这需要 API 11,因此不适合作为通用解决方案。