且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android中的多媒体应用MediaPlayer

更新时间:2022-10-03 10:16:28

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

 

迷你音乐播放器

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

 


  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import android.app.Activity;  
  6. import android.app.AlertDialog;  
  7. import android.media.MediaPlayer;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.ImageButton;  
  11.  
  12. public class MainActivity extends Activity implements MediaPlayer.OnCompletionListener {  
  13.       
  14.     private ImageButton play,pause,stop;  
  15.     private MediaPlayer mp;  
  16.       
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.           
  22.         play = (ImageButton)findViewById(R.id.play);  
  23.         pause = (ImageButton)findViewById(R.id.pause);  
  24.         stop = (ImageButton)findViewById(R.id.stop);  
  25.           
  26.         play.setOnClickListener(new View.OnClickListener() {  
  27.             public void onClick(View view) {  
  28.                 play();  
  29.             }  
  30.         });  
  31.           
  32.         pause.setOnClickListener(new View.OnClickListener() {  
  33.             public void onClick(View view) {  
  34.                 pause();  
  35.             }  
  36.         });  
  37.           
  38.         stop.setOnClickListener(new View.OnClickListener() {  
  39.             public void onClick(View view) {  
  40.                 stop();  
  41.             }  
  42.         });  
  43.           
  44.         setup();  
  45.     }  
  46.  
  47.     @Override 
  48.     public void onDestroy() {  
  49.         super.onDestroy();  
  50.         if (stop.isEnabled()) {  
  51.             stop();  
  52.         }  
  53.     }  
  54.       
  55.     public void onCompletion(MediaPlayer mp) {  
  56.         stop();  
  57.     }  
  58.       
  59.     private void play() {  
  60.         mp.start();  
  61.           
  62.         play.setEnabled(false);  
  63.         pause.setEnabled(true);  
  64.         stop.setEnabled(true);  
  65.     }  
  66.       
  67.     private void stop() {  
  68.         mp.stop();  
  69.         pause.setEnabled(false);  
  70.         stop.setEnabled(false);  
  71.           
  72.         try {  
  73.             mp.prepare();  
  74.             mp.seekTo(0);  
  75.             play.setEnabled(true);  
  76.         }  
  77.         catch (Throwable t) {  
  78.             error(t);  
  79.         }  
  80.     }  
  81.       
  82.     private void pause() {  
  83.         mp.pause();  
  84.         play.setEnabled(true);  
  85.         pause.setEnabled(false);  
  86.         stop.setEnabled(true);  
  87.     }  
  88.       
  89.     private void loadClip() {  
  90.         try {  
  91.             mp=MediaPlayer.create(this, R.raw.test);  
  92.             mp.setOnCompletionListener(this);  
  93.         }  
  94.         catch (Throwable t) {  
  95.             error(t);  
  96.         }  
  97.     }  
  98.       
  99.     private void setup() {  
  100.         loadClip();  
  101.         play.setEnabled(true);  
  102.         pause.setEnabled(false);  
  103.         stop.setEnabled(false);  
  104.     }  
  105.       
  106.     private void error(Throwable t) {  
  107.         AlertDialog.Builder builder=new AlertDialog.Builder(this);  
  108.         builder  
  109.             .setTitle("报错啦!")  
  110.             .setMessage(t.toString())  
  111.             .setPositiveButton("确定"null)  
  112.             .show();  
  113.     }  

/Chapter11_Media_MiniPlayer/res/layout/main.xml

 


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

/Chapter11_Media_MiniPlayer/AndroidManifest.xml

 


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

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1081707