有没有办法在 android 应用程序中播放 .flv 视频文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2013201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to play .flv video files in android app?
提问by Lint_
Is it possible out of the box or via a library?
是否可以开箱即用或通过图书馆?
回答by FrancescoR
you can play FLV using flash plugin inside a WebView. see here: http://www.synesthesia.it/playing-flash-flv-videos-in-android-applications
您可以在 WebView 中使用 Flash 插件播放 FLV。见这里:http: //www.synesthesia.it/playing-flash-flv-videos-in-android-applications
Often when you create an app displaying web contents in a mobile device you have to deal with FLV videos, still widely used in the web (until HTML5 will rule the world). The best thing to do is to convert them with some converter (like ffmpeg), but if you don'have access to original videos or for some other reasons you can't do the conversion in some other suitable format, here you can find a quick tutorial on how to embed and play Flash FLV Videos in an Android application.
通常,当您在移动设备中创建一个显示 Web 内容的应用程序时,您必须处理 FLV 视频,这些视频仍然广泛用于 Web(直到 HTML5 将统治世界)。最好的办法是使用一些转换器(如 ffmpeg)转换它们,但是如果您无法访问原始视频或由于其他原因无法以其他合适的格式进行转换,您可以在这里找到关于如何在 Android 应用程序中嵌入和播放 Flash FLV 视频的快速教程。
This is done by using a WebView, a SWF player capable of playing FLVs, and of course the Flash plugin for Android installed.
这是通过使用 WebView、能够播放 FLV 的 SWF 播放器以及安装的 Android 的 Flash 插件来完成的。
First, create a layout xml with a WebView, like this:
首先,创建一个带有 WebView 的布局 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="fill_parent" android:id="@+id/webview" android:layout_height="fill_parent"></WebView> </LinearLayout>
then, create the Activity class, here is an extract:
然后,创建 Activity 类,这里是一个摘录:
package it.synesthesia.flvplayer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
public class ViewVideo 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.view_video);
webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginsEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); //thanks Patrick!
htmlCode = htmlCode.replaceAll("@VIDEO@", video_link);
webView.loadDataWithBaseURL("fake://fake/fake", 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);
}
}
}
}
Some explanation:
一些解释:
- as said, you need a FLV player. I used the great & free FLVPlayer http://www.platipus.nl/flvplayer/download/1.0/
- FLV player must reside on a website on the net. I tried putting the .swf in the /assets folder of the app and calling it from there, but it failed to load the FLV video. if someone can fix this please let me know!
- htmlCode contains the code to show the video as "fullscreen" (filling to full size of the webview).
- callHiddenWebViewMethod is very important, otherwise the video will continue playing when the activity is not visible any more (and audio as well). Credis to this goes to the sliseshare poster linked in the comment. Thanks! WARNING: this is mostly an hack! beware, things could not work as expected, or can stop working in the future. Btw, it worked very well for me.
- 如前所述,您需要一个 FLV 播放器。我使用了很棒的免费 FLVPlayer http://www.platipus.nl/flvplayer/download/1.0/
- FLV 播放器必须驻留在网络上的网站上。我尝试将 .swf 放在应用程序的 /assets 文件夹中并从那里调用它,但无法加载 FLV 视频。如果有人可以解决这个问题,请告诉我!
- htmlCode 包含将视频显示为“全屏”(填充到 web 视图的全尺寸)的代码。
- callHiddenWebViewMethod 非常重要,否则当活动不再可见时视频将继续播放(以及音频)。对此的信任转到评论中链接的 sliseshare 海报。谢谢!警告:这主要是一个黑客!请注意,事情无法按预期进行,或者将来可能会停止工作。顺便说一句,它对我来说效果很好。
回答by Stelian Iancu
No, it's not possible. The OpenCORE media library that's providing the media playback on Android doesn't support .flv files.
不,这不可能。在 Android 上提供媒体播放的 OpenCORE 媒体库不支持 .flv 文件。
You can find a list of supported media formats here. These formats are supported on generic Android. On the same page you will find some additional formats that the T-Mobile G1 supports.
您可以在此处找到支持的媒体格式列表。通用 Android 支持这些格式。在同一页面上,您会发现 T-Mobile G1 支持的一些其他格式。
Of course, support for additional media formats might be possible on specific devices. But this can vary from device to device.
当然,在特定设备上可能支持其他媒体格式。但这可能因设备而异。