實現一個android的音樂播放器

實現功能,播放,暫停,重置,進度條的使用

String文件:

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <resources> 
  3.  
  4.     <stringname="app_name">MusicPlayer</string> 
  5.     <stringname="music_name">歌曲:</string> 
  6.     <stringname="play_text">播放</string> 
  7.     <stringname="pause_text">暫停</string> 
  8.     <stringname="continue_text">繼續</string> 
  9.     <stringname="reset_text">重置</string> 
  10.     <stringname="stop_text">關閉</string> 
  11.     <stringname="notfoundfile_text">媒體文件不存在</string> 
  12.     <stringname="notfoundSdcard_text">SDCard不存在</string> 
  13.  
  14. </resources> 

 

佈局:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <TextView 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="@string/music_name" /> 
  11.  
  12.     <EditText 
  13.         android:id="@+id/musicEt" 
  14.         android:layout_width="fill_parent" 
  15.         android:layout_height="wrap_content" 
  16.         android:text="a.mp3" 
  17.          /> 
  18.     <SeekBar 
  19.         android:id="@+id/seekBar" 
  20.         android:layout_width="match_parent" 
  21.         android:layout_height="wrap_content" 
  22.         android:max="10000" 
  23.          /> 
  24.     <TableLayout 
  25.         android:layout_width="fill_parent" 
  26.         android:layout_height="wrap_content" 
  27.         android:stretchColumns="*" > 
  28.  
  29.         <TableRow > 
  30.  
  31.             <Button 
  32.                 android:id="@+id/playBtn" 
  33.                 android:layout_width="wrap_content" 
  34.                 android:layout_height="wrap_content" 
  35.                 android:text="@string/play_text" /> 
  36.  
  37.             <Button 
  38.                 android:id="@+id/pauseBtn" 
  39.                 android:layout_width="wrap_content" 
  40.                 android:layout_height="wrap_content" 
  41.                 android:text="@string/pause_text" /> 
  42.  
  43.             <Button 
  44.                 android:id="@+id/resetBtn" 
  45.                 android:layout_width="wrap_content" 
  46.                 android:layout_height="wrap_content" 
  47.                 android:text="@string/reset_text" /> 
  48.  
  49.             <Button 
  50.                 android:id="@+id/stopBtn" 
  51.                 android:layout_width="wrap_content" 
  52.                 android:layout_height="wrap_content" 
  53.                 android:text="@string/stop_text" /> 
  54.         </TableRow> 
  55.     </TableLayout> 
  56. </LinearLayout> 

Java源碼:

  1. package cn.class3g.musicplayer; 
  2.  
  3. import java.io.File; 
  4. import java.io.IOException; 
  5.  
  6. import android.app.Activity; 
  7.  
  8. import android.media.MediaPlayer; 
  9. import android.os.Bundle; 
  10. import android.os.Environment; 
  11. import android.os.Handler; 
  12. import android.util.Log; 
  13. import android.view.View; 
  14. import android.view.View.OnClickListener; 
  15. import android.widget.Button; 
  16. import android.widget.EditText; 
  17. import android.widget.SeekBar; 
  18. import android.widget.SeekBar.OnSeekBarChangeListener; 
  19. import android.widget.Toast; 
  20.  
  21. public class MusicPlayerActivity extends Activity implements OnClickListener, 
  22.         OnSeekBarChangeListener { 
  23.  
  24.     private static final String TAG = "MusicPlayerActivity"
  25.     EditText musicEt; 
  26.     Button playBtn, pauseBtn, resetBtn, stopBtn; 
  27.     Handler handler = null
  28.     Handler fHandler = null
  29.     MediaPlayer player = null
  30.  
  31.     SeekBar seekbar = null
  32.  
  33.     public void onCreate(Bundle savedInstanceState) { 
  34.         super.onCreate(savedInstanceState); 
  35.         setContentView(R.layout.main); 
  36.  
  37.         player = new MediaPlayer(); 
  38.  
  39.         findViews(); 
  40.  
  41.     } 
  42.  
  43.     private void findViews() { 
  44.         musicEt = (EditText) this.findViewById(R.id.musicEt); 
  45.         playBtn = (Button) this.findViewById(R.id.playBtn); 
  46.         pauseBtn = (Button) this.findViewById(R.id.pauseBtn); 
  47.         resetBtn = (Button) this.findViewById(R.id.resetBtn); 
  48.         stopBtn = (Button) this.findViewById(R.id.stopBtn); 
  49.         seekbar = (SeekBar) this.findViewById(R.id.seekBar); 
  50.         playBtn.setOnClickListener(this); 
  51.         pauseBtn.setOnClickListener(this); 
  52.         resetBtn.setOnClickListener(this); 
  53.         stopBtn.setOnClickListener(this); 
  54.         seekbar.setOnSeekBarChangeListener(this); 
  55.     } 
  56.  
  57.     public void onClick(View v) { 
  58.         String fileName = musicEt.getText().toString(); 
  59.  
  60.         // 確定sdcard狀態是否爲已加載 
  61.         if (Environment.getExternalStorageState().equals( 
  62.                 Environment.MEDIA_MOUNTED)) { 
  63.             File file = new File(Environment.getExternalStorageDirectory(), 
  64.                     fileName); 
  65.  
  66.             // 判斷所播放的媒體文件是否存在 
  67.             if (file.exists()) { 
  68.  
  69.                 try
  70.                     switch (v.getId()) { 
  71.                     case R.id.playBtn: 
  72.                         playMusic(file); 
  73.                         break
  74.                     case R.id.pauseBtn: 
  75.                         if (player.isPlaying()) { 
  76.                             player.pause(); 
  77.                             pauseBtn.setText(R.string.continue_text); 
  78.                         } else
  79.                             player.start(); 
  80.                             pauseBtn.setText(R.string.pause_text); 
  81.                         } 
  82.                         break
  83.                     case R.id.resetBtn: 
  84.                         if (player.isPlaying()) { 
  85.                             player.seekTo(0); 
  86.                         } else
  87.                             playMusic(file); 
  88.                         } 
  89.                         break
  90.                     case R.id.stopBtn: 
  91.                         if (player.isPlaying()) { 
  92.                             player.stop(); 
  93.                         } 
  94.                         break
  95.                     } 
  96.                 } catch (Exception e) { 
  97.                     Log.e(TAG, e.toString()); 
  98.                 } 
  99.             } else
  100.                 Toast.makeText(this, R.string.notfoundfile_text, 
  101.                         Toast.LENGTH_SHORT).show(); 
  102.             } 
  103.         } else
  104.             Toast.makeText(this, R.string.notfoundSdcard_text, 
  105.                     Toast.LENGTH_SHORT).show(); 
  106.         } 
  107.     } 
  108.  
  109.     protected void onDestroy() { 
  110.         if (player != null) { 
  111.             if (player.isPlaying()) { 
  112.                 player.stop(); 
  113.             } 
  114.             player.release(); 
  115.         } 
  116.         super.onDestroy(); 
  117.     } 
  118.  
  119.     protected void onPause() { 
  120.         if (player != null) { 
  121.             if (player.isPlaying()) { 
  122.                 player.pause(); 
  123.             } 
  124.         } 
  125.         super.onPause(); 
  126.     } 
  127.  
  128.     protected void onResume() { 
  129.         if (player != null) { 
  130.             if (!player.isPlaying()) { 
  131.                 player.start(); 
  132.             } 
  133.         } 
  134.         super.onResume(); 
  135.     } 
  136.  
  137.     private void playMusic(File file) throws IllegalArgumentException, 
  138.             IllegalStateException, IOException { 
  139.         if (player != null) { 
  140.             player.reset(); 
  141.             player.setDataSource(file.getAbsolutePath()); 
  142.             player.prepare(); 
  143.             player.start(); 
  144.  
  145.             seekbar.setMax(player.getDuration()); 
  146.             Log.i(TAG, String.valueOf(player.getDuration())); 
  147.  
  148.             new Thread(new seekBar()).start(); 
  149.         } 
  150.     } 
  151.  
  152.     class seekBar implements Runnable { 
  153.  
  154.         int position = 0
  155.  
  156.         public void run() { 
  157.             while (player != null) { 
  158.                 position = player.getCurrentPosition(); 
  159.                 seekbar.setProgress(position); 
  160.  
  161.                 try
  162.                     Thread.sleep(100); 
  163.                 } catch (InterruptedException e) { 
  164.                     Log.e(TAG, e.toString()); 
  165.                 } 
  166.             } 
  167.  
  168.         } 
  169.  
  170.     } 
  171.  
  172.     public void onProgressChanged(SeekBar seekBar, int progress, 
  173.             boolean fromUser) { 
  174.         if (fromUser) 
  175.             player.seekTo(progress); 
  176.     } 
  177.  
  178.     public void onStartTrackingTouch(SeekBar seekBar) { 
  179.  
  180.     } 
  181.  
  182.     public void onStopTrackingTouch(SeekBar seekBar) { 
  183.  
  184.     } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章