Android简易Flash播放器[转]

 

上一节,大体说了下在Android程序中嵌套Flash动画。这次按照上次的内容做个扩展,做个简易的flash播放器。

前提条件如上一节所说,需要Android2.2平台和安装flash的插件。

 

先看工程图和效果图:

Android简易Flash播放器[转]_第1张图片

Android简易Flash播放器[转]_第2张图片

 

Android简易Flash播放器[转]_第3张图片

 

 

工程源码:

package com.geolo.android.flash;  
import com.geolo.android.FileBrowser;  
import com.geolo.android.R;  
import android.app.Activity;  
import android.app.AlertDialog;  
import android.app.ProgressDialog;  
import android.content.DialogInterface;  
import android.content.Intent;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Message;  
import android.util.Log;  
import android.view.KeyEvent;  
import android.view.View;  
import android.webkit.WebChromeClient;  
import android.webkit.WebSettings.PluginState;  
import android.webkit.WebView;  
import android.widget.Button;  
import android.widget.FrameLayout;  
import android.widget.ProgressBar;  
public class FlashActivity extends Activity{  
    private WebView mWebView;  
    private Button playButton,pauseButton,rewindButton,exitButton,fileButton;  
    private ProgressBar mProgressBarHorizontal;  
    private final static int PROGRESSBARSIZE = 0x0000;  
    private final static int FLASH_START = 0x0001;  
    private String flashName ;  
    private boolean stopThread = false;  
    private ProgressDialog mProgressDialog;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        mProgressDialog = new ProgressDialog(this);  
        mProgressDialog.setMessage("Flash动画正在加载,请稍等......");  
        mProgressDialog.show();  
        Intent intent = this.getIntent();  
        String fileName = intent.getStringExtra("fileName");  
        if(fileName != null && !fileName.equals("")){  
            flashName = "file://"+fileName;  
            //flashName = "javascript:setFlashPath(flashName)";  
        }else{  
            flashName = "file:///android_asset/sample/flash.swf";  
        }  
        Log.d(this.getClass().getName(), flashName);          
        mWebView = (WebView)findViewById(R.id.webView01);   
        mProgressBarHorizontal = (ProgressBar)findViewById(R.id.progress_horizontal);  
        this.setProgress(mProgressBarHorizontal.getProgress() * 100);  
        //this.setSecondaryProgress(mProgressBarHorizontal.getSecondaryProgress() * 100);  
        playButton = (Button)findViewById(R.id.playButton);  
        pauseButton = (Button)findViewById(R.id.pauseButton);  
        rewindButton = (Button)findViewById(R.id.rewindButton);  
        exitButton = (Button)findViewById(R.id.exitButton);  
        fileButton = (Button)findViewById(R.id.fileButton);  
        playButton.setOnClickListener(buttonListener);  
        pauseButton.setOnClickListener(buttonListener);  
        rewindButton.setOnClickListener(buttonListener);  
        exitButton.setOnClickListener(buttonListener);  
        fileButton.setOnClickListener(buttonListener);  
        mWebView.getSettings().setJavaScriptEnabled(true);    
        //mWebView.getSettings().setPluginsEnabled(true);  
        mWebView.getSettings().setPluginState(PluginState.ON);  
        mWebView.setWebChromeClient(new WebChromeClient());   
        mWebView.addJavascriptInterface(new CallJava(), "CallJava");  
        mWebView.loadUrl("file:///android_asset/sample/index.html");   
        //mWebView.loadUrl("javascript:setFlashPath('"+flashName+"')");   
        startThread();  
    }  
    Button.OnClickListener buttonListener = new Button.OnClickListener() {    
        @Override  
        public void onClick(View v) {  
            int buttonID = v.getId();  
            switch (buttonID) {  
            case R.id.playButton:  
                mWebView.loadUrl("javascript:Play()");  
                showFlashProgress(5);  
                break;  
            case R.id.pauseButton:  
                mWebView.loadUrl("javascript:Pause()");  
                break;  
            case R.id.rewindButton:  
                //mWebView.loadUrl(flashName);  
                try {  
                    mWebView.loadUrl("about:blank");  
                    mWebView.loadUrl("file:///android_asset/sample/index.html");   
                    Thread.sleep(1000);  
                    mWebView.loadUrl("javascript:setFlashPath('"+flashName+"')");   
                } catch (InterruptedException e) {  
                    Log.e(this.getClass().getName(), "Flash Rewind error: ", e);  
                }  
                break;  
            case R.id.fileButton:  
                Intent intent = new Intent();  
                intent.setClass(FlashActivity.this, FileBrowser.class);  
                startActivity(intent);  
                stopThread = true;  
                FlashActivity.this.finish();  
                break;  
            case R.id.exitButton:  
                quitDialog();  
                break;  
            default:  
                break;  
            }  
        }  
    };  
    public void showFlashProgress(float progressSize){  
        int size = (int)progressSize;  
        //Toast.makeText(this, size+"", Toast.LENGTH_SHORT).show();  
        mProgressBarHorizontal.setProgress(size);  
    }  
    private void quitDialog(){  
        new AlertDialog.Builder(this)  
        .setMessage("没胆就不要退出")  
        .setPositiveButton("比你有胆", new AlertDialog.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                stopThread = true;  
                FlashActivity.this.finish();  
            }  
        })  
        .setNegativeButton("怕你了", null)  
        .show();  
    }  
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        switch (keyCode) {  
        case KeyEvent.KEYCODE_BACK:  
            quitDialog();  
            break;  
        default:  
            break;  
        }  
        return false;  
    }  
    @Override  
    protected void onPause(){  
        super.onPause();  
        mWebView.pauseTimers();  
        if(isFinishing()){  
            mWebView.loadUrl("about:blank");  
            setContentView(new FrameLayout(this));  
        }  
    }  
    @Override  
    protected void onResume(){  
        super.onResume();  
        mWebView.resumeTimers();  
    }  
    private final class CallJava{  
        public void consoleFlashProgress(float  progressSize){  
            showFlashProgress(progressSize);  
        }  
    }  
    private void startThread(){  
        //通过线程来改变ProgressBar的值  
        new Thread(new Runnable() {  
            @Override  
            public void run() {  
                try {  
                    Thread.sleep(2000);  
                    Message message = new Message();  
                    message.what = FlashActivity.FLASH_START;  
                    FlashActivity.this.myMessageHandler.sendMessage(message);  
                } catch (InterruptedException e1) {  
                    Thread.currentThread().interrupt();  
                }  
  
                while(!stopThread && !Thread.currentThread().isInterrupted()){  
                    try {  
                        Thread.sleep(2000);  
                        Message message2 = new Message();  
                        message2.what = FlashActivity.PROGRESSBARSIZE;  
                        FlashActivity.this.myMessageHandler.sendMessage(message2);  
                    } catch (Exception e) {  
                        Thread.currentThread().interrupt();  
                    }  
                }  
            }  
        }).start();  
    }  
    Handler myMessageHandler = new Handler() {  
        @Override  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case FlashActivity.PROGRESSBARSIZE:  
                mWebView.loadUrl("javascript:showcount()");  
                break;  
            case FlashActivity.FLASH_START:  
                mWebView.loadUrl("javascript:setFlashPath('"+flashName+"')");   
                Log.d(this.getClass().getName(),"Start flash : "+flashName);  
                mProgressDialog.dismiss();  
                break;  
            default:  
                break;  
            }  
            super.handleMessage(msg);  
        }  
    };  
}

 

package com.geolo.android;  
import java.io.File;  
import java.util.List;  
import android.content.Context;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.ArrayAdapter;  
import android.widget.TextView;  
public class FileListAdapter extends ArrayAdapter<File>{  
      
    public FileListAdapter(Context context, int Resource,List<File> objects) {  
        super(context,Resource,objects);  
    }  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        TextView view = (TextView)super.getView(position, convertView, parent);  
        File file = getItem(position);  
        if (position == 0){  
            view.setText("当前目录:/root" + file.getAbsolutePath());  
        }else if (position == 1 && !isRoot()){  
            view.setText("返回上一个目录");  
        }else{  
            view.setText(file.getName());  
        }  
        return view;  
    }  
      
    public boolean isRoot() {  
        return getItem(0).getParent() == null;  
    }  
}

 

package com.geolo.android;  
import java.io.File;  
import java.io.FileFilter;  
import java.util.ArrayList;  
import java.util.List;  
import com.geolo.android.flash.FlashActivity;  
import android.app.ListActivity;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.view.KeyEvent;  
import android.view.View;  
import android.widget.ListView;  
public class FileBrowser extends ListActivity {  
    private static final FileFilter FILTER = new FileFilter() {  
        public boolean accept(File f) {  
            //return f.isDirectory() || f.getName().matches("^.*?//.(jpg|png|bmp|gif)$");  
            return true;  
        }  
    };  
    private FileListAdapter fileList;  
    public void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  
        File sdcard = android.os.Environment.getExternalStorageDirectory();  
        fill(sdcard);  
    }  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if (keyCode == KeyEvent.KEYCODE_BACK && !fileList.isRoot()) {  
            fill(fileList.getItem(1));  
            Intent intent = new Intent();  
            intent.setClass(FileBrowser.this, FlashActivity.class);  
            startActivity(intent);  
            //return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }  
      
    private void fill(File folder) {  
        List<File> files = new ArrayList<File>();  
        files.add(folder);  
        if (folder.getParentFile() != null){  
            files.add(folder.getParentFile());  
        }  
        for (File file : folder.listFiles(FILTER)) {  
            files.add(file);  
        }  
        fileList = new FileListAdapter(this, android.R.layout.simple_list_item_1, files);  
        setListAdapter(fileList);  
    }  
      
    @Override  
    protected void onListItemClick(ListView l, View v, int position, long id) {  
        File file = fileList.getItem(position);  
        Intent intent = new Intent();  
        intent.setAction(android.content.Intent.ACTION_VIEW);  
        if (file.isDirectory()){  
            fill(file);  
        }else if(file.getName().matches("^.*?//.(jpg|png|bmp|gif)$")){  
            intent.setDataAndType(Uri.fromFile(file), "image/*");  
            startActivity(intent);  
        }else if(file.getName().matches("^.*?//.(swf)$")){  
            intent.setClass(FileBrowser.this, FlashActivity.class);  
            intent.putExtra("fileName", file.getAbsolutePath().replace("/mnt", ""));  
            startActivity(intent);  
            FileBrowser.this.finish();  
        }  
    }  
} 

 

<mce:script src="play.js" mce_src="play.js"></mce:script>  
<table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">  
  <tr>  
    <td>  
     <object id="movie" 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" />  
     </object>  
    </td>  
  </tr>  
</table>  
     
     
   <!-- <a href="javascript:CallJava.consoleFlashProgress(3)" mce_href="javascript:CallJava.consoleFlashProgress(3)">add Progress</a>  
   <a href="#" mce_href="#" onClick="loadSWF('','testFlash.swf','800','480')">TestButton</a>  -->  
    <p id="geolo"></p>  
<mce:script type="text/javascript"><!--  
  //loadSWF("testFlash.swf","800","480"); //loadSWF("flash地址","宽度","高度")  
  function setFlashPath(filePath){  
     var path = filePath;  
     loadSWF(path,"800","480"); //loadSWF("flash地址","宽度","高度")  
     //geolo.innerText = "abc: " + filePath.toString();  
  }  
// --></mce:script>

 

var total;//定义flash影片总桢数  
var frame_number;//定义flash影片当前桢数  
//以下是滚动条图片拖动程序  
var dragapproved=false;  
var z,x,y  
//动态显示播放影片的当前桢/总桢数(进度条显示)  
function showcount(){  
    //已测可用CallJava.consoleFlashProgress(5);  
    total = movie.TotalFrames;  
    frame_number=movie.CurrentFrame();  
    frame_number++;  
    var progressSize = 100*(frame_number/movie.TotalFrames());  
    CallJava.consoleFlashProgress(progressSize);  
}  
//播放影片   
function Play(){  
    movie.Play();  
}  
//暂停播放  
function Pause(){  
 movie.StopPlay();  
}  
//开始载入flash影片  
function loadSWF(fsrc,fwidth,fheight){  
 movie.LoadMovie(0, fsrc);  
 movie.width=fwidth;  
 movie.height=fheight;  
 frame_number=movie.CurrentFrame();  
}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>  
<!-- autor:geolo 声明:版权所有,违者必究 -->  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
    <WebView android:id="@+id/webView01" android:layout_width="wrap_content"  
        android:layout_height="wrap_content" />  
    <ProgressBar android:id="@+id/progress_horizontal"  
        style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:max="100"  
        android:progress="0" android:secondaryProgress="0" />  
    <LinearLayout android:orientation="horizontal"  
        android:layout_width="fill_parent" android:layout_height="wrap_content">  
        <Button android:id="@+id/playButton" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="播放" />  
        <Button android:id="@+id/pauseButton" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="暂停" />  
        <Button android:id="@+id/rewindButton" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="重播" />  
        <Button android:id="@+id/fileButton" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="打开文件" />  
        <Button android:id="@+id/exitButton" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:text="退出" />  
    </LinearLayout>  
</LinearLayout> 

 

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.geolo.android" android:versionCode="1"  
    android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".flash.FlashActivity" android:label="@string/app_name"  
            android:screenOrientation="landscape"  
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
        <activity android:name=".FileBrowser" />  
    </application>  
    <supports-screens android:resizeable="true"  
        android:smallScreens="true" android:largeScreens="true"  
        android:normalScreens="true" android:anyDensity="true"></supports-screens>  
</manifest>
 工程打包资源下载: http://download.csdn.net/source/2926306

 

你可能感兴趣的:(android)