sjplyler
sjplyler

Reputation: 3

How can I play a swf file using WebView and accessing the file from my assets directory?

I am new to both android and flash development. I am creating a metronome for my guitar app by using flash to implement the metronome portion. I already have a working project for the guitar and a separate flash file that I created for the metronome animation.

I have done a lot of research on this website and cant seem to find anything that is working, my code is below. Am I just making a rookie mistake or is there something bigger that I can try that will work better for me.

My goal is to have the activity access the Metronome.swf file and play it just as the .apk generated by flash does.

Here is my metronome.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   />
</LinearLayout>

And this is my MetronomeActivity.java file:

package itcs4155.group4.guitarTeacher;


import android.os.Bundle;
import android.webkit.WebView;

public class MetronomeActivity extends BaseActivity {
   private WebView mWebView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.metronome);


        mWebView = (WebView)findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginsEnabled(true);


        String html = "<object width=\"768\" height=\"1024\"> <param name=\"movie\" value=\"file:///android_asset/Metronome.swf\"> <embed src=\"file:///android_asset/Metronome.swf\" width=\"768\" height=\"1024\"> </embed> </object>";
        String mimeType = "text/html";
        String encoding = "utf-8";
        mWebView.loadDataWithBaseURL("null", html, mimeType, encoding, "");
    }
}

Thanks for any help in advance!

Upvotes: 0

Views: 5947

Answers (2)

rsek
rsek

Reputation: 13

Like Akshay mentioned above,

<activity android:hardwareAccelerated="true" />

needs to be added, however I was having an issue where the swf would not load in full or load the initial swf. My solution was to point the

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.web_engine);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
    mWebView.setBackgroundColor(Color.parseColor("#000000"));
    mWebView.loadUrl("file:///android_asset/swfFile.html");


}

Keep in mind that to use setPluginsEnabled you need to downgrade your API version to 10 or 11.

And your html file is normally generated when publishing your swf file or you can add:

<div id="flashContent">
        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100%" height="100%" id=**"Something**" align="middle">
            <param name="movie" value="**Something.swf**" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#000000" />
            <param name="play" value="true" />
            <param name="loop" value="true" />
            <param name="wmode" value="direct" />
            <param name="scale" value="showall" />
            <param name="menu" value="true" />
            <param name="devicefont" value="false" />
            <param name="salign" value="" />
            <param name="allowScriptAccess" value="sameDomain" />
            <!--[if !IE]>-->
            <object type="application/x-shockwave-flash" data="**Something.swf**" width="100%" height="100%">
                <param name="movie" value="**Something.swf**" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#000000" />
                <param name="play" value="true" />
                <param name="loop" value="true" />
                <param name="wmode" value="direct" />
                <param name="scale" value="showall" />
                <param name="menu" value="true" />
                <param name="devicefont" value="false" />
                <param name="salign" value="" />
                <param name="allowScriptAccess" value="sameDomain" />
            <!--<![endif]-->
                <a href="http://www.adobe.com/go/getflash">
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Obtener Adobe Flash Player" />
                </a>
            <!--[if !IE]>-->
            </object>
            <!--<![endif]-->
        </object>
    </div>

Upvotes: 0

Akshay
Akshay

Reputation: 1137

Some times it may happen that the swf file get loaded on the web view by is not displayed.

Try adding android:android:hardwareAccelerated="true" in your manifest file.

<application android:hardwareAccelerated="true">
        <activity ... />
        <activity android:hardwareAccelerated="true" />
    </application>

It might help...

Upvotes: 2

Related Questions