8

Is it possible out of the box or via a library?

Trott
  • 66,479
  • 23
  • 173
  • 212
Lint_
  • 1,391
  • 3
  • 12
  • 10
  • There is a workaround which will also allow you to play from a local asset resource, please read my answer to this similar question for a full solution: http://stackoverflow.com/questions/6596243/problem-to-load-flv-video-in-webview/6855609#6855609 – Moog Jul 31 '11 at 13:37

3 Answers3

3

you can play FLV using flash plugin inside a WebView. see here: 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.

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 = "<!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.
FrancescoR
  • 520
  • 1
  • 4
  • 12
  • 1
    While this may answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include more of the essential parts of the answer here, and provide the link for reference. – Nathan Tuggy Nov 26 '15 at 00:24
  • i copy-pasted the article in the answer. – FrancescoR Dec 11 '15 at 08:48
  • doesn't seem to work. – Narendra Singh Jul 12 '17 at 10:54
  • this code is 4 years old, so it's possible that newer versions of Android, Flash Player, etc changed something so it does not work any more. Flash is almost disappeared from the web now, so not a big deal any more – FrancescoR Jul 27 '17 at 08:49
2

No, it's not possible. The OpenCORE media library that's providing the media playback on Android doesn't support .flv files.

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.

Of course, support for additional media formats might be possible on specific devices. But this can vary from device to device.

Stelian Iancu
  • 2,462
  • 3
  • 18
  • 28
  • The HTC Hero has a built-in Flash runtime, however this doesn't play standalone FLV files. – Christopher Orr Jan 06 '10 at 14:09
  • The Nexus One also apparently has Flash 10.1 on it, but I do not know whether it can play standalone FLV files. – CommonsWare Jan 06 '10 at 14:22
  • This is incorrect. Yes, it is possible. I worked for a company that built streaming HD video software for their hardware, viewable on iOS, Android, and in a web browser, using FLV. Unfortunately, can't share the code, but I can say it is definitely doable. – jungledev Feb 27 '17 at 16:57
  • That response is from 7 years ago :-). I would assume that in the meantime it's possible :-). OpenCORE is no longer the media framework on Android since a long long time. – Stelian Iancu Feb 28 '17 at 06:55
1

Try vitamio. It is a library similar to mediaplayer which can be imported into your project very easily. The only thing is that it supports flv video files too.

Suleman Khan
  • 634
  • 1
  • 10
  • 23