Android简易Flash播放器(暂停、播放、重播)
- 格式:docx
- 大小:18.42 KB
- 文档页数:7
申明下:重播有js调用flash函数重播有问题。这个和安全沙箱有关系。我搞不定。所以这里的重播是刷新了WebView。
上一节,大体说了下在Android程序中嵌套Flash动画。这次按照上次的内容做个扩展,做个简易的flash播放器。
前提条件如上一节所说,需要Android2.2平台和安装flash的插件。
先看工程图和效果图:
工程源码:
1. package com.geolo.android.flash;
2.
3. import com.geolo.android.FileBrowser;
4. import com.geolo.android.R;
5.
6. import android.app.Activity;
7. import android.app.AlertDialog;
8. import android.app.ProgressDialog;
9. import android.content.DialogInterface;
10. import android.content.Intent;
11. import android.os.Bundle;
12. import android.os.Handler;
13. import android.os.Message;
14. import android.util.Log;
15. import android.view.KeyEvent;
16. import android.view.View;
17. import android.webkit.WebChromeClient;
18. import android.webkit.WebSettings.PluginState;
19. import android.webkit.WebView;
20. import android.widget.Button;
21. import android.widget.FrameLayout;
22. import android.widget.ProgressBar;
23.
24. public class FlashActivity extends Activity{
25.
26. private WebView mWebView;
27. private Button
playButton,pauseButton,rewindButton,exitButton,fileButton;
28. private ProgressBar mProgressBarHorizontal;
29. private final static int PROGRESSBARSIZE = 0x0000;
30. private final static int FLASH_START = 0x0001; 31. private String flashName ;
32. private boolean stopThread = false;
33. private ProgressDialog mProgressDialog;
34.
35. @Override
36. public void onCreate(Bundle savedInstanceState) {
37. super.onCreate(savedInstanceState);
38. setContentView(yout.main);
39.
40. mProgressDialog = new ProgressDialog(this);
41. mProgressDialog.setMessage("Flash动画正在加载,请稍等......");
42. mProgressDialog.show();
43.
44. Intent intent = this.getIntent();
45. String fileName = intent.getStringExtra("fileName");
46. if(fileName != null && !fileName.equals("")){
47. flashName = "file://"+fileName;
48. //flashName =
"javascript:setFlashPath(flashName)";
49. }else{
50. flashName =
"file:///android_asset/sample/flash.swf";
51. }
52. Log.d(this.getClass().getName(),
flashName);
53.
54. mWebView = (WebView)findViewById(R.id.webView01);
55. mProgressBarHorizontal =
(ProgressBar)findViewById(R.id.progress_horizontal);
56. this.setProgress(mProgressBarHorizontal.getProgress() * 100);
57. //this.setSecondaryProgress(mProgressBarHorizontal.getSecondaryProgress() * 100);
58. playButton = (Button)findViewById(R.id.playButton);
59. pauseButton =
(Button)findViewById(R.id.pauseButton);
60. rewindButton =
(Button)findViewById(R.id.rewindButton);
61. exitButton = (Button)findViewById(R.id.exitButton);
62. fileButton = (Button)findViewById(R.id.fileButton);
63. playButton.setOnClickListener(buttonListener);
64. pauseButton.setOnClickListener(buttonListener);
65. rewindButton.setOnClickListener(buttonListener); 66. exitButton.setOnClickListener(buttonListener);
67. fileButton.setOnClickListener(buttonListener);
68. mWebView.getSettings().setJavaScriptEnabled(true);
69. //mWebView.getSettings().setPluginsEnabled(true);
70. mWebView.getSettings().setPluginState(PluginState.ON);
71. mWebView.setWebChromeClient(new WebChromeClient());
72. mWebView.addJavascriptInterface(new CallJava(),
"CallJava");
73. mWebView.loadUrl("file:///android_asset/sample/index.html");
74. //mWebView.loadUrl("javascript:setFlashPath('"+flashName+"')");
75. startThread();
76. }
77.
78. Button.OnClickListener buttonListener = new
Button.OnClickListener() {
79. @Override
80. public void onClick(View v) {
81. int buttonID = v.getId();
82. switch (buttonID) {
83. case R.id.playButton:
84. mWebView.loadUrl("javascript:Play()");
85. showFlashProgress(5);
86. break;
87. case R.id.pauseButton:
88. mWebView.loadUrl("javascript:Pause()");
89. break;
90. case R.id.rewindButton:
91. //mWebView.loadUrl(flashName);
92. try {
93. mWebView.loadUrl("about:blank");
94. mWebView.loadUrl("file:///android_asset/sample/index.html");
95. Thread.sleep(1000);
96. mWebView.loadUrl("javascript:setFlashPath('"+flashName+"')");
97. } catch (InterruptedException e) { 98. Log.e(this.getClass().getName(), "Flash Rewind error: ", e);
99. }
100. break;
101. case R.id.fileButton:
102. Intent intent = new Intent();
103. intent.setClass(FlashActivity.this, FileBrowser.class);
104. startActivity(intent);
105. stopThread = true;
106. FlashActivity.this.finish();
107. break;
108. case R.id.exitButton:
109. quitDialog();
110. break;
111. default:
112. break;
113. }
114. }
115. };
116.
117. public void showFlashProgress(float progressSize){
118. int size = (int)progressSize;
119. //Toast.makeText(this, size+"",
Toast.LENGTH_SHORT).show();
120. mProgressBarHorizontal.setProgress(size);
121. }
122.
123. private void quitDialog(){
124. new AlertDialog.Builder(this)
125. .setMessage("没胆就不要退出")
126. .setPositiveButton("比你有胆", new
AlertDialog.OnClickListener() {
127. @Override
128. public void onClick(DialogInterface
dialog, int which) {
129. stopThread = true;
130. FlashActivity.this.finish();
131. }
132. })
133. .setNegativeButton("怕你了", null)
134. .show();
135. }
136.