MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。

迷你音乐播放器

/Chapter11_Media_MiniPlayer/src/com/amaker/test/MainActivity.java

        
  1. 代码
  2. packagecom.amaker.test;
  3. importandroid.app.Activity;
  4. importandroid.app.AlertDialog;
  5. importandroid.media.MediaPlayer;
  6. importandroid.os.Bundle;
  7. importandroid.view.View;
  8. importandroid.widget.ImageButton;
  9. publicclassMainActivityextendsActivityimplementsMediaPlayer.OnCompletionListener{
  10. privateImageButtonplay,pause,stop;
  11. privateMediaPlayermp;
  12. @Override
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. play=(ImageButton)findViewById(R.id.play);
  17. pause=(ImageButton)findViewById(R.id.pause);
  18. stop=(ImageButton)findViewById(R.id.stop);
  19. play.setOnClickListener(newView.OnClickListener(){
  20. publicvoidonClick(Viewview){
  21. play();
  22. }
  23. });
  24. pause.setOnClickListener(newView.OnClickListener(){
  25. publicvoidonClick(Viewview){
  26. pause();
  27. }
  28. });
  29. stop.setOnClickListener(newView.OnClickListener(){
  30. publicvoidonClick(Viewview){
  31. stop();
  32. }
  33. });
  34. setup();
  35. }
  36. @Override
  37. publicvoidonDestroy(){
  38. super.onDestroy();
  39. if(stop.isEnabled()){
  40. stop();
  41. }
  42. }
  43. publicvoidonCompletion(MediaPlayermp){
  44. stop();
  45. }
  46. privatevoidplay(){
  47. mp.start();
  48. play.setEnabled(false);
  49. pause.setEnabled(true);
  50. stop.setEnabled(true);
  51. }
  52. privatevoidstop(){
  53. mp.stop();
  54. pause.setEnabled(false);
  55. stop.setEnabled(false);
  56. try{
  57. mp.prepare();
  58. mp.seekTo(0);
  59. play.setEnabled(true);
  60. }
  61. catch(Throwablet){
  62. error(t);
  63. }
  64. }
  65. privatevoidpause(){
  66. mp.pause();
  67. play.setEnabled(true);
  68. pause.setEnabled(false);
  69. stop.setEnabled(true);
  70. }
  71. privatevoidloadClip(){
  72. try{
  73. mp=MediaPlayer.create(this,R.raw.test);
  74. mp.setOnCompletionListener(this);
  75. }
  76. catch(Throwablet){
  77. error(t);
  78. }
  79. }
  80. privatevoidsetup(){
  81. loadClip();
  82. play.setEnabled(true);
  83. pause.setEnabled(false);
  84. stop.setEnabled(false);
  85. }
  86. privatevoiderror(Throwablet){
  87. AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
  88. builder
  89. .setTitle("报错啦!")
  90. .setMessage(t.toString())
  91. .setPositiveButton("确定",null)
  92. .show();
  93. }
  94. }

/Chapter11_Media_MiniPlayer/res/layout/main.xml

        
  1. 代码
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="迷你音乐播放器"/>
  10. <LinearLayout
  11. android:orientation="horizontal"android:layout_width="fill_parent"
  12. android:layout_height="fill_parent">
  13. <ImageButton
  14. android:id="@+id/play"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:src="@drawable/play"
  18. />
  19. <ImageButton
  20. android:id="@+id/pause"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:src="@drawable/pause"
  24. />
  25. <ImageButton
  26. android:id="@+id/stop"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:src="@drawable/stop"
  30. />
  31. </LinearLayout>
  32. </LinearLayout>

/Chapter11_Media_MiniPlayer/AndroidManifest.xml

        
  1. 代码
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  4. package="com.amaker.test"
  5. android:versionCode="1"
  6. android:versionName="1.0">
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".MainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. <uses-sdkandroid:minSdkVersion="3"/>
  17. </manifest>

更多相关文章

  1. Android进行单元测试难在哪-part4
  2. 基于eclipse的android项目实战—博学谷(十 八)关于视频播放问题
  3. android中WebView和javascript实现数据交互实例
  4. NDK Android* 应用移植方法 APP_ABI := armeabi armeabi-v7a x86
  5. Android代码性能优化技巧 (一)
  6. 关于WebVeiw的例子(一)
  7. 2012传智播客黑马程序员内部视频
  8. 【ArcGIS for Android】使用自定义Callout显示地图信息
  9. android studio 查找替换

随机推荐

  1. 小乔爱问
  2. Android(安卓)Phone蓝牙通信方式总结(Sock
  3. Android(安卓)组件系列-----Activity初步
  4. Oxpecker 让Android(安卓)Native插上“H5
  5. HTC 败诉对 Android(安卓)意味着什么?
  6. 仿MIUI的Toast动画效果实现
  7. Android官方开发文档Training系列课程中
  8. android 组件动画(一)——球的进入效果
  9. 按键从Linux到Android
  10. Android安全机制浅谈-android学习之旅(80)