Problem to Load Flv Video in Webview

Problem to load flv video in webview

You don't state clearly what your Android version, however I can confirm that the flash plugin does load and the movie player does intilaize with the swirly shape graphic. I can engage it into fullscreen mode. I am also able to play online video but not from the application's assets folder, however I do have a workaround.

The following permissinos are required in your manifest.xml:

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

Interestingly I discovered I was only able to run the SWF player from the assets folder when the html was statically loaded i.e when I created an html file and loaded that directly from the assets folder using loadUrl(). There seems to be something blocking access to the application directory when html data is loaded using the loadDataBaseUrl method.

To succesfully get video to play from the local device a static html file and the video must be co-located on the sdcard. My only conclusion is that the assets folder is off limits to the embedded plugin for security reasons.

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

And here is the html file

<!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>

This is a 100% working solution but you would have to write your html file to the sdcard before calling loadUrl to dynaamically replace the video file name. In addition you should copy the video from your assets folder into the same path on the sdcard (as you can see from my exaple I created a folder called FLVplayer.

Test performed on Samsung Galaxy Tab running Froyo(2.2)

UPDATE

I am including a complete activity that has been tested and working. This expects a copy of the Flash player, the FLV file and the index.html file to be placed in a folder called "FLVplayer" on the root of your sdcard which you can copy across using the file explorer. You can edit the various utility functions to modify the behaviour to copy the files from your assetes folder

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

I believe there may be a way to bypass the security issues by intercepting the URL of the various files and returning the data directly from the assets folder using WebViewClient.shouldInterceptRequest() but that requires API 11 so not suitable as a generic solution.

How to play flv video in android?

You can play a FLV video using the flash plugin inside a WebView. See here.

Hope this will help you.

Edit:

These are done using Action Scripts.

Call the Action Script

Play flv using Action Script

flv programming

Also this

playing .flv videos in windows store apps

I think its not possible to play FLV video formats. You need to create your own decoder for it. Have a look at this official Microsoft answer: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/0b1d2d0d-f7ba-4a9e-80bb-d67059453c33/play-mpegflv-files-im-media-element

UPDATE

This thread might be useful to you in getting a hint about how to add codec packs in Metro Style app: http://answers.flyppdevportal.com/categories/metro/csharpvb.aspx?ID=054eca2f-4583-483b-8d46-ce7d6214e4f2

Using HTML5 video tag in a javafx application

You are encountering a codec issue.

From the JavaFX FAQ question 7, JavaFX (as of 2.0.2) only supports flv videos encoded using the on2 vp6 codec.

Additional codec support is scheduled for future releases. The relevant feature request is RT-18296 (login required, but anybody can sign up to view the JavaFX issue database and create feature requests, vote for issues or post comments).

A related StackOverflow question provides a summary of considerations for playing video in JavaFX 2.1 (including a JavaFX WebView).

To demonstrate a html5 video tag and video playback within a JavaFX WebEngine, run the following code, which plays an Oracle supplied vp6 encoded video.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewVideo extends Application {
public static void main(String[] args) { Application.launch(args); }
@Override public void start(Stage primaryStage) {
WebView root = new WebView();
root.getEngine().loadContent(
"<video width='320' height='240'controls='controls'>" +
"<source src='http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv'/>" +
"Your browser does not support the video tag." +
"</video>");
primaryStage.setScene(new Scene(root, 340, 260));
primaryStage.show();
}
}


Related Topics



Leave a reply



Submit