Android uses webview as a plug-in to play SWF and only displays some images.

Source: Internet
Author: User

In my application, webview is placed in the layout as a plug-in. The plug-in location and size are not fixed, but are specified by the user.

The problem arises. When the layout. x coordinate for loading webview is not 0, some of the right side of flash will not be displayed,

For example, if the X coordinate of layout is 100, the screen width of 100 on the Right of Flash cannot be played.

No solution was found on the network, and it was suspected that the flash plug-in was a problem.

The final solution is to keep the coordinates of layout unchanged, which is (0, 0). Only the coordinates of webview are moved, and the webview does not directly load SWF, but is played by loading webpages containing SWF. Specify the flash size on the webpage.

The main code is as follows:


// Flashactivity. Java

Public class flashactivity extends activity {

Private string strfile;

Private webview WV, webview;
Private int playindex = 0;
Private areabean = new areabean ();

Private int m_width, m_height;
Private linearlayout webviewlayout;
Private linearlayout webviewlayout1;

Private Final Static string flashdri = "file: // MNT/sdcard/flash /";

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Try {
Inputstream is;
Is = getassets (). Open ("index.html ");
Strfile = readtextfile (is );
Is. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

WV = new webview (this. getparent () = NULL? This: This. getparent ());
WV. getsettings (). setjavascriptenabled (true );
WV. getsettings (). setallowfileaccess (true );
WV. getsettings (). setpluginsenabled (true); // All versions are supported.
WV. getsettings (). setpluginstate (pluginstate. On); // only Versions later than 2.2 are supported.
WV. setwebchromeclient (New webchromeclient ());
WV. addjavascriptinterface (New calljava (), "calljava ");

Webviewlayout = (linearlayout) findviewbyid (R. Id. webviewlayout );
Webviewlayout1 = (linearlayout) findviewbyid (R. Id. webviewlayout1 );
Webviewlayout1.addview (WV );

Playindex = 0;
Object object = getintent (). getextras (). Get ("plugin ");
Getpluginareabean (object );

M_width = areabean. getwidthrate (); // custom flash width
M_height = areabean. getheightrate (); // custom flash height

WV. setx (areabean. getleftrate (); // coordinates of the upper left corner of the webview
WV. sety (areabean. gettoprate ());

WV. setbackgroundcolor (color. argb (0, 0, 0, 0 ));
Webviewlayout1.setbackgroundcolor (color. argb (0, 0, 0, 0 ));
Webviewlayout. setbackgroundcolor (color. argb (0, 0, 0, 0 ));
Linearlayout. layoutparams Params = new linearlayout. layoutparams (m_width + (INT) WV. getx (), layoutparams. match_parent); // the key reason is not explained.
Webviewlayout1.setlayoutparams (Params );

If (areabean. getareaplanbeans (). Size ()> 0 ){
Setflashpath (flashdri + areabean. getareaplanbeans (). Get (playindex). getfilename ());
}


}

Private void setflashpath (String SWF)
{
WV. loaddatawithbaseurl (null,
Strfile. Replace ("flash.swf", SwF)
. Replace ("Height = 100", "Height =" + m_height)
. Replace ("width = 100", "width =" + m_width ),
Text/html, UTF-8, null );
}

Private final class calljava {
Public void consoleflashprogress (float progresssize ){

}
Public void flashloaded (){

}
}


Private string readtextfile (inputstream ){
Bytearrayoutputstream outputstream = new bytearrayoutputstream ();

Byte Buf [] = new byte [1024];

Int Len;

Try {

While (LEN = inputstream. Read (BUF ))! =-1 ){

Outputstream. Write (BUF, 0, Len );

}

Outputstream. Close ();

Inputstream. Close ();

} Catch (ioexception e ){

}
Return outputstream. tostring ();
}

/**

* Call the hidden webview method <br/>

* Note: webview completely exits SWF to stop playing the sound.

* @ Param name

*/

Private void callhiddenwebviewmethod (string name ){
If (WV! = NULL ){
Try {
Method method = webview. Class. getmethod (name );
Method. Invoke (WV); // call
} Catch (nosuchmethodexception e) {// there is no such method
Log. I ("No such method:" + name, E. tostring ());
} Catch (illegalaccessexception e) {// illegal access
Log. I ("illegal access:" + name, E. tostring ());
} Catch (invocationtargetexception e) {// the target of the call is abnormal.
Log. D ("invocation target exception:" + name, E. tostring ());
}
}
}

 

@ Override

Protected void onpause (){
Super. onpause ();
WV. pausetimers ();
If (isfinishing ()){
WV. loadurl ("about: blank ");
Setcontentview (New framelayout (this ));
}
Callhiddenwebviewmethod ("onpause ");
}

 
@ Override

Protected void onresume (){
Super. onresume ();
WV. resumetimers ();
Callhiddenwebviewmethod ("onresume ");
}
}

// Main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "@ drawable/bg3"
Android: Id = "@ + ID/webviewlayout">

<Linearlayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Id = "@ + ID/webviewlayout1"
>
</Linearlayout>
</Linearlayout>

// Index.html

<HTML>
<Head>
<Title> Android Flash Player </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<SCRIPT type = "text/JavaScript">
VaR playover = true; // play directly
VaR total; // defines the total number of clips in flash.
VaR frame_number =-1; // defines the current number of clips for a flash video.

// Dynamically display the current progress of the Video/total progress (progress bar)
Function showcount (){
// Calljava. leleflashprogress (5) is available );
VaR num = 1 + fmovie. currentframe ();
VaR millisec = 150;
If (Num <frame_number | num = total)
{Calljava. leleflashprogress (100); millisec = 1000 ;}
Else
Calljava. leleflashprogress (100 * (Num/total ));

Frame_number = num;
// Geolo. innertext = "N/m:" + frame_number + "/" + total
SetTimeout (showcount, millisec );
}
Function isload (){
If (fmovie. percentloaded () = 100) frame_number = 0;
}
// Load the video
Function load (){
If (frame_number> = 0 ){
Total = fmovie. totalframes ();
If (playover & total> 1) Play ();
Calljava. flashloaded ();
SetTimeout (showcount, 100 );
} Else {
SetTimeout (load, 100 );
SetTimeout (isload, 50 );
}
}
// Play the video
Function play (){
Playover = true;
Fmovie. Play ();
}
// Pause the playback.
Function pause (){
Fmovie. stopplay ();
}
// Film height and width
Function setwh (x, y ){
VaR M = Document. getelementbyid ("fmovie ")
M. width = X;
M. Height = y; // M. width = X;
}
SetTimeout (load, 100 );
</SCRIPT>

</Head>
<Body leftmargin = 0 topmargin = 0 rightmargin = 0 bottommargin = 0>
<Object classid = "CLSID: D27CDB6E-AE6D-11cf-96B8-444553540000"
Codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"

Align = "Middle">
<Param name = "movie" value = "about: blank"/>
<Param name = "quality" value = "high"/>
<Embed src = "flash.swf" bgcolor = "#575757" id = "fmovie" Height = 100 width = 100> </embed>
</Object>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.