`
jguangyou
  • 浏览: 375742 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.

This is done by using a WebView, a SWF player capable of playing FLVs, and of course the Flash plugin for Android installed.

First, create a layout xml with a WebView, like this:
<?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:

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 = &quot;&quot;;  
	String htmlCode = 
			&quot; &quot; +
			&quot;&quot;;
	String htmlPost = &quot;&quot;;

	@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(&quot;@VIDEO@&quot;, video_link);
		webView.loadDataWithBaseURL(&quot;fake://fake/fake&quot;, htmlPre+htmlCode+htmlPost, &quot;text/html&quot;, &quot;UTF-8&quot;, null);  
	}




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

        callHiddenWebViewMethod(&quot;onPause&quot;);

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

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

        callHiddenWebViewMethod(&quot;onResume&quot;);

        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(&quot;No such method: &quot; + name + e);
            } catch (IllegalAccessException e) {
                Lo.g(&quot;Illegal Access: &quot; + name + e);
            } catch (InvocationTargetException e) {
                Lo.g(&quot;Invocation Target Exception: &quot; + name + e);
            }
        }
    }

}

Some explanation:

  • as said, you need a FLV player. I used the great &amp; 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 &quot;fullscreen&quot; (filling to full size of the webview). I could not get rid of a small white row some pixel wide, that I suppose is the space for the webview scrollbar. If you found workaround for this, again, please let me know so I can update the tutorial thanks to Patrick van Coeverden for the fix suggestion - see http://stackoverflow.com/questions/2279978/webview-showing-white-bar-on-right-side
  • 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.
  • Have fun and let me know if you use this code! Francesco Ronchi (francesco [dot] ronchi [at] synesthesia [dot] it)

分享到:
评论

相关推荐

    The Android Developer’s Cookbook: Building Applications with the Android SDK

    Want to get started building applications for Android, the world’s hottest, fast-growing mobile platform? Already building Android applications and want to get better at it? This book brings together...

    The Android Developer's Cookbook Building Applications and Source Project

    The Android Developer's Cookbook Building Applications with the Android SDK and Source Project Code 源书(pdf)及源代码12章全都有(rar) Book Description Want to get started building applications for ...

    Android代码-Android Picture in Picture 效果 Demo。

    The video keeps on playing when the app is turned in to Picture-in-Picture mode. On Picture-in-Picture screen, the app shows an action item to pause or resume the video. Introduction As of Android O,...

    前端在线播放flv视频

    在前端开发中,实现FLV(Flash Video)视频的在线播放是常见的需求,尤其是在流媒体服务中。FLV是一种流行的视频格式,它依赖于Adobe Flash Player进行播放。然而,随着HTML5技术的发展,Flash逐渐被淘汰,因此需要...

    A media player playing videos from video sites..zip

    A media player playing videos from video sites..zip,播放优酷网和其他视频网站视频的视频播放器。

    flv flash播放器

    总结来说,FLV Flash播放器是基于Flash技术的视频播放解决方案,而"1.2 Playing video with components"可能涉及到如何使用ActionScript 3.0来创建自定义的视频播放器,包括视频流的获取、播放控制以及与用户的交互...

    Stop Auto-Playing videos in Ynet-crx插件

    标题“Stop Auto-Playing videos in Ynet-crx插件”涉及到的是一个针对Ynet网站的浏览器扩展程序,其主要功能是阻止视频自动播放。在现代网络浏览体验中,许多网站为了提升用户体验或增加广告曝光率,会在页面加载时...

    Android代码-Android Nougat Easter Egg

    I wanted to keep playing this game on Android O, so lifted the source from AOSP, cleaned it up a bit, fixed some things, put it in a Bazel workspace, and published it on Google Play Store. How to ...

    Sothing_Web_Video_Downloader_Setup.rar

    provided by Sothink Web Video Downloader, can detect any online videos played in your browser, and prompt you to download video with one-click.With the built-in Flash Video Player, playing the ...

    Mastering.Android.Game.Development.1783551771

    You will also create beautiful and responsive menus and dialogs and explore the different options for playing sound effects and music in Android. You will then learn the basics of creating a ...

    flvplayer.rar

    FLV.js提供了丰富的事件接口,如`on('error')`、`on('playing')`等,用于监听播放状态并做出相应处理。同时,还可以通过调用播放器对象的方法来控制播放,如`play()`、`pause()`、`seekTo()`等。 4. **性能优化与...

    Professional Android 4 Application Development 源代码

    Native Android Applications Android SDK Features Introducing the Open Handset Alliance What Does Android Run On? Why Develop for Mobile? Why Develop for Android? Introducing the Development Framework ...

    developing android application with adobe air.part3.rar

    Mobile Flash Player 10.1 Versus AIR 2.6 on Android Chapter 2 : Call Me, Text Me Setting Up Your Device Creating a Project Creating the Application Descriptor Writing the Code Packaging Your ...

    flv网页播放器

    在网页中播放视频已经成为现代网页设计不可或缺的一部分,而FLV(Flash Video)格式作为早期流行的视频格式,曾广泛应用于在线流媒体服务。Flowplayer是一款基于Flash技术的开源JavaScript播放器,它支持FLV以及其他...

    VB FLV控件 FLV视频播放器

    根据提供的文件信息,我们可以分析并总结出以下关于“VB FLV控件 FLV视频播放器”的相关知识点: ### 一、概述 此VB(Visual Basic)项目为一个FLV视频播放器控件,用于在应用程序中嵌入和播放FLV格式的视频文件。...

    professional.Android.Application.Development.2009.pdf

    Native Android Applications 4 Android SDK Features 5 Access to Hardware including Camera, GPS, and Accelerometer 6 Native Google Maps, Geocoding, and Location-Based Services 6 Background Services...

    Android代码-inline-youtube-view

    inline-youtube-view YouTube component for Android, iOS and React. This is a suite of utility libraries around using ...Playing them inline where in a list you can have more than one videos in a singl

    pcgtcg:Playing Cards Game 适用于 Android 的集换式卡牌游戏

    首先,我们来看“pcgtcg”这个名字,它是“Playing Cards Game”的缩写,暗示了游戏的核心机制——玩家通过收集不同的卡片,构建自己的牌组,进行策略性的对战。在Android平台上,这类游戏的实现通常涉及到以下几个...

    android webview autoplay video

    在Android开发中,WebView是一个非常重要的组件,它允许我们在应用程序中内嵌网页内容,实现与网页的交互。本文将深入探讨如何在Android的WebView中支持HTML5的video标签内容自动播放,以及相关的优化技巧和注意事项...

Global site tag (gtag.js) - Google Analytics