android webview播放swf文件

android webview播放swf文件

今天做了一个通过webview播放flash的文件,以前一直以为可以使用videoview视频播放播放flash的东西,今天才发现不行,好像现在除了自己做一个flash播放器,就是用webview去加载flash了。废话少说,代码如下:

在xml里面设置webview布局

<WebView
       android:id="@+id/web_flash"
    android:layout_width="578px"
    android:layout_height="400px"
    android:background="#00000000"
    android:layout_centerInParent="true"
    />

然后在activity里面加载进来

 public void loadFlash() {
     WebView mWebFlash = (WebView) findViewById(R.id.web_flash);
     WebSettings settings = mWebFlash.getSettings();
  settings.setPluginsEnabled(true);
  settings.setJavaScriptEnabled(true);
  settings.setAllowFileAccess(true);
  settings.setDefaultTextEncodingName("GBK");
  mWebFlash.setBackgroundColor(0);
  mWebFlash.loadUrl("file:///android_asset/abc.swf");
 }

在activity里面执行loadFlash就可以播放swf文件了。一开始的时候,我播放swf文件总是在视频边缘有一些白边不管怎么设置大小都去不掉,后来发现这些白边是webview的,可是在xml里面设置background为透明不管用,找美工发现也不是视频的问题,最后发现,还有一个参数可以设置webview的背景色透明,那就是mWebFlash.setBackgroundColor(0);果然设置之后,白边没有了。但是webview的大小必须和视频的实际大小一致,不然背景是白色的,这个通过设置xml的background和mWebFlash.setBackgroundColor(0);都没有办法去掉,暂时还没有想到方法解决,但是已经不影响我使用了所以就先这样呀呵呵

 

 

 

 

首先要说明,必须是Android2.2以及以上版本的手机或者平板电脑才能支持,而且并不是所有的都支持,需要装插件。不能播放也有可能是系统CUP不支持。 Android结构图: 

AndroidManifest.xml源代码  <?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.cn"        android:versionCode="1"       android:versionName="1.0">     <uses-permission  android:name="android.permission.INTERNET"></uses-permission>        <application android:icon="@drawable/icon" android:label="@string/app_name">          <activity android:name=".WebViewTest1"                   android:label="@string/app_name">             <intent-filter>                  <action android:name="android.intent.action.MAIN" />                 <category  android:name="android.intent.category.LAUNCHER" /> 

</intent-filter>         </activity> 

</application>       <uses-sdk android:minSdkVersion="7" /> </manifest>  Main.xml源代码  <?xml version="1.0" encoding="utf-8"?> <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent" android:layout_height="match_parent"   android:orientation="horizontal"  >   <LinearLayout android:id="@+id/linearLayout1"   android:layout_height="match_parent"  android:layout_marginLeft="5dp"       android:layout_weight="3"    android:layout_width="match_parent">    <ListView android:id="@+id/listView1"  android:layout_height="match_parent"   android:layout_width="match_parent"></ListView> 

</LinearLayout>   <LinearLayout android:id="@+id/linearLayout2"   android:layout_height="match_parent"    android:orientation="vertical" android:layout_marginLeft="15dp"    android:layout_weight="1"    android:layout_width="match_parent">     <WebView android:id="@+id/webView1"  android:layout_height="match_parent"  android:layout_width="match_parent"></WebView>  </LinearLayout> </LinearLayout>    Listadapter.xml源代码  <?xml version="1.0" encoding="utf-8"?> <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_height="match_parent" android:layout_width="match_parent">  

<ImageView android:layout_width="wrap_content"  android:src="@drawable/icon" android:id="@+id/imageViewCar" android:layout_height="wrap_content"></ImageView>     <TextView android:text="TextView" android:layout_width="wrap_content"  android:layout_height="wrap_content" android:layout_marginTop="15dp"  

 

android:id="@+id/textViewName"></TextView> </LinearLayout>    主类源代码:  public class WebViewTest1 extends Activity {  File name[];  WebView wv;   ListView listview;  String filename;  ImageView iv;   Bitmap flashtobitmap;  byte ImageByte[];    /** Called when the activity is first created. */  

@Override  

public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);    wv = (WebView) findViewById(R.id.webView1);   wv.getSettings().setPluginsEnabled(true);    wv.loadUrl("http://www.baidu.com");    listview = (ListView) findViewById(R.id.listView1);   listview.setAdapter(new MyListAdapter(WebViewTest1.this, getSD()));     listview.setOnItemClickListener(new OnItemClickListener() {     @Override     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,        long arg3) {      // TODO Auto-generated method stub     filename = name[arg2].getName();  

wv.loadUrl("file:///mnt/sdcard/X_flash/" + filename);       }   });  }    File[] getSD() {   File file = new File("/sdcard/X_flash/");    File name[] = file.listFiles(new FileFilter() { 

public boolean accept(File pathname) {     // TODO Auto-generated method stub      if (pathname.getName().lastIndexOf(".swf") > 0) {      return true;      }      return false;      }    });     return name;   }  

public class MyListAdapter extends BaseAdapter {   /* 声明变量 */    private Context mContext;    int car[] = { R.drawable.car1, R.drawable.car2, R.drawable.car3 };   int i;     MyListAdapter(Context c, File[] file) {    mContext = c;    name = file;     }   

  @Override    public int getCount() {    // TODO Auto-generated method stub    if(name!=null)    {     return name.length;    }     else return 0;   }   

@Override    public Object getItem(int position) {    // TODO Auto-generated method stub    return position;    }     @Override    public long getItemId(int position) {    // TODO Auto-generated method stub       return 0;  

}     @Override    public View getView(int position, View convertView, ViewGroup parent) {     // TODO Auto-generated method stub    /*      * TextView temp=new TextView(mContext);     * temp.setText(name[position].getName());  temp.setSingleLine(true);      * temp.setEllipsize(TruncateAt.MARQUEE);//设置为跑马灯     * temp.setTextSize(20); return temp;     */      LayoutInflater inflater = LayoutInflater.from(mContext);    View vw = (View) inflater.inflate(R.layout.listadapter, null);     iv = (ImageView) vw.findViewById(R.id.imageViewCar);    iv.setImageResource(car[position%3]); 

            TextView temp = (TextView)  vw.findViewById(R.id.textViewName);    temp.setSingleLine(true);    temp.setText(name[position].getName());     temp.setEllipsize(TruncateAt.MARQUEE);// 设置为跑马灯      temp.setTextSize(20);        return vw;      }   } }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值