重播有JS調用Flash函數重播有問題。這個和安全沙箱有關系。我搞不定。所以這裡的重播是刷新了WebVIEw。
上一節,大體說了下在android程序中嵌套Flash動畫。這次按照上次的內容做個擴展,做個簡易的Flash播放器。
前提條件如上一節所說,需要android2.2平台和安裝Flash的插件。
先看工程圖和效果圖:
工程源碼:
- 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);
- }
- };
- }