【Android源码 栏目提醒】:网学会员在Android源码 频道为大家收集整理了“android视频播放器-源码 - 技术总结“提供大家参考,希望对大家有所帮助!
本文讲的是“
android 视频播放器”并附有播放器源代码 1开发环境 eclipse3.6 ADT-0.9.7 AVD1.6 2程序运行效果 A启动AVD虚拟设备在应用程序界面主界面我们可以看到“艾文播放器” B点击打开后会播放默认的一个coco的广告 C点击标题栏的按钮可以打开文件浏览器 D点击视频文件会提示使用的播放器 E选择“艾文视频播放器”打开后按ctrlf12切换到横屏发现视频并没有中断而是继续播放 3
源码分析 A图片都是网上找的还有就是自己收藏的地方翻出来的不多讲了。
B布局文件 main.xml期中包含一个videoview用于播放视频图像 titlebar.xml主视图的标题栏布局主要用于添加一个菜单按钮点击后打开文件浏览器 myfile.xml:为文件浏览器布局 CVideoPlay.java view plaincopy to clipboardprint 1. package com.bestaone 2. 3. import
android.app.Activity 4. import
android.content.Intent 5. import
android.net.Uri 6. import
android.os.Bundle 7. import
android.util.Log 8. import
android.view.View 9. import
android.view.View.OnClickListener 10. import
android.view.Window 11. import
android.view.WindowManager 12. import
android.widget.ImageButton 13. import
android.widget.MediaController 14. import
android.widget.TextView 15. import
android.widget.VideoView 16. 17. public class VideoPlay extends Activity 18. 19. private VideoView videoView 20. private static int index 0 21. 22. Override 23. public void onCreateBundle savedInstanceState 24. super.onCreatesavedInstanceState 25. requestWindowFeatureWindow.FEATURE_CUSTOM_TITLE 26. setContentViewR.layout.main 27. getWindow.setFlagsWindowManager.LayoutParams.FLAG_FULLSCREEN WindowManager.LayoutParams.FLAG_FULLSCREEN// 28. //设置标题栏的布局 29. getWindow.setFeatureIntWindow.FEATURE_CUSTOM_TITLE R.layout.titlebar 30. //这个空间暂时没用 31. final TextView titleTV TextView findViewByIdR.id.title 32. titleTV.setText 33. ImageButton titleButton ImageButton findViewByIdR.id.titleButton 34. //为按钮添加鼠标点击事件 35. titleButton.setOnClickListenernew OnClickListener 36. Override 37. public void onClickView v 38. Intent intent new Intent 39. intent.setClassVideoPlay.this MyFile.class 40. //打开MyFile activity 41. startActivityintent 42. 43. 44. Intent intent getIntent 45. String value intent.getDataString 46. videoView VideoView findViewByIdR.id.VideoView01 47. ifvaluenull 48. //加载默认视频 49. videoView.setVideoURIUri.parseandroid.resource://com.bestaone/ R.drawable.coco 50. else 51. //通过文件浏览器传过来的视频路径播放 52. videoView.setVideoPathvalue 53. 54. videoView.setMediaControllernew MediaControllerVideoPlay.this 55. videoView.requestFocus 56. 57. 58. //启动 59. Override 60. protected void onStart 61. super.onStart 62. Log.imp4 on start 63. 64. 65. Override 66. protected void onResume 67. super.onResume 68. videoView.seekToindex 69. videoView.start 70. Log.imp4 on resume 71. 72. 73. //暂停 74. Override 75. protected void onPause 76. super.onPause 77. Log.imp4 on pause 78. 79. 80. //停止 81. Override 82. protected void onStop 83. super.onStop 84. videoView.pause 85. //在这里记住视频播放的位置当屏幕横竖切换的时候可以从记录点继续播放 86. index videoView.getCurrentPosition 87. Log.imp4 on stop 88. 89. 90. //销毁 91. Override 92. protected void onDestroy 93. super.onDestroy 94. // videoView.destroyDrawingCache 95. index videoView.getCurrentPosition 96. Log.imp4 on destroy 97. 98. 99. package com.bestaoneimport
android.app.Activityimport
android.content.Intentimport
android.net.Uriimport
android.os.Bundleimport
android.util.Logimport
android.view.Viewimport
android.view.View.OnClickListenerimport
android.view.Windowimport
android.view.WindowManagerimport
android.widget.ImageButtonimport
android.widget.MediaControllerimport
android.widget.TextViewimport
android.widget.VideoViewpublic class VideoPlay extends Activity MyFile.java view plaincopy to clipboardprint 1. package com.bestaone 2. 3. import java.io.File 4. import java.util.ArrayList 5. import java.util.List 6. 7. import
android.app.ListActivity 8. import
android.content.Intent 9. import
android.net.Uri 10. import
android.os.Bundle 11. import
android.view.KeyEvent 12. import
android.view.View 13. import
android.widget.AdapterView 14. import
android.widget.AdapterView.OnItemLongClickListener 15. import
android.widget.Button 16. import
android.widget.EditText 17. import
android.widget.ImageButton 18. import
android.widget.ListView 19. import
android.widget.TextView 20. import
android.widget.Toast 21. 22. public class MyFile extends ListActivity implements OnItemLongClickListener 23. 24. //支持的视频格式 25. private final String FILE_MapTable 26. 27. // 后缀名 MIME类型 28. .3gp video/3gpp 29. .mov video/quicktime 30. .avi video/x-msvideo 31. .rmvb audio/x-pn-realaudio 32. .wmv audio/x-ms-wmv 33. 34. 35. 36. private List items null // items存放显示的名称 37. private List paths null // paths存放文件路径 38. private List sizes null // sizes文件大小 39. private String rootPath / // rootPath起始文件夹 40. private TextView path_edit 41. private ImageButton rb_qry 42. private int isZoom 0 43. private int isOpen 0 44. 45. 46. / 47. 重写返回键功能:返回上一级文件夹 48. / 49. Override 50. public boolean onKeyDownint keyCode KeyEvent event 51. // 是否触发按键为back键 52. if keyCode KeyEvent.KEYCODE_BACK 53. path_edit EditText findViewByIdR.id.path_edit 54. File file new Filepath_edit.getText.toString 55. if rootPath.equalspath_edit.getText.toString 56. return super.onKeyDownkeyCode event 57. else 58. getFileDirfile.getParent 59. return true 60. 61. // 如果不是back键正常响应 62. else 63. return super.onKeyDownkeyCode event 64. 65. 66. 67. Override 68. protected void onCreateBundle icicle 69. super.onCreateicicle 70. setContentViewR.layout.myfile 71. 72. path_edit EditText findViewByIdR.id.path_edit 73. rb_qry ImageButton findViewByIdR.id.qry_button 74. rb_qry.setOnClickListenerlistener_qry 75. getListView.setOnItemLongClickListenerthis 76. getFileDirrootPath 77. 78. 79. Button.OnClickListener listener_qry new Button.OnClickListener 80. public void onClickView arg0 81. File file new Filepath_edit.getText.toString 82. if file.exists 83. if file.isFile 84. openFilefile 85. else 86. getFileDirpath_edit.getText.toString 87. 88. else 89. Toast.makeTextMyFile.this 找不到该位置请确定位置是否正确Toast.LENGTH_SHORT.show 90. 91. 92. 93. 94. / 95. 设置ListItem被点击时要做的动作 96. / 97. Override 98. protected void onListItemClickListView l View v int position long id 99. File file new Filepaths.getposition 100. fileOrDirHandlefile 101. 102. 103. 104. / 105. 处理文件或者目录的方法 106. param file 107. param flag 108. / 109. private void fileOrDirHandlefinal File file 110. if file.isDirectory 111. getFileDirfile.getPath 112. else 113. openFilefile 114. 115. 116. 117. / 118. 取得文件结构的方法 119. param filePath 120. / 121. private void getFileDirString filePath 122. / 设置目前所在路径 / 123. path_edit.setTextfilePath 124. items new ArrayList 125. paths new ArrayList 126. sizes new ArrayList 127. File f new FilefilePath 128. File files f.listFiles 129. if files null 130. / 将所有文件添加ArrayList中 / 131. for int i 0 i files.length i 132. if filesi.isDirectory 133. items.addfilesi.getName 134. paths.addfilesi.getPath 135. sizes.add 136. 137. 138. 139. for int i 0 i files.length i 140. if filesi.isFile 141. String fileName filesi.getName 142. int index fileName.lastIndexOf. 143. ifindex0 144. String endName fileName.substringindexfileName.length.toLowerCase 145. String type null 146. forint x0 x