`
云端的Android
  • 浏览: 61655 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Android成长之路-音乐播放器的实现(转)

 
阅读更多

 

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">MusicPlayer</string>  
  5.     <string name="music_name">曲目</string>  
  6.     <string name="play_text">播放</string>  
  7.     <string name="pause_text">暂停</string>  
  8.     <string name="continue_text">继续</string>  
  9.     <string name="reset_text">重置</string>  
  10.     <string name="stop_text">停止</string>  
  11.     <string name="choose_text">选择</string>  
  12.     <string name="notfoundfile_text">媒体文件不存在</string>  
  13.     <string name="notfoundSdcard_text">SDcard不存在</string>  
  14.   
  15. </resources>  

 

 

 

main.xml

  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.     <TableLayout   
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:stretchColumns="0" >  
  16.   
  17.         <TableRow >  
  18.         <EditText  
  19.             android:id="@+id/musicEt"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:text="l.mp3" />  
  23.   
  24.         <Button  
  25.             android:id="@+id/chooseBtn"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:text="@string/choose_text" />  
  29.           
  30.         </TableRow>  
  31.     </TableLayout>  
  32.   
  33.     <SeekBar  
  34.         android:id="@+id/seekBar"  
  35.         android:layout_width="fill_parent"  
  36.         android:layout_height="wrap_content" />  
  37.     <SeekBar  
  38.         android:id="@+id/seekBarSound"  
  39.        android:layout_width="fill_parent"  
  40.         android:layout_height="wrap_content"  
  41.               android:max="100"    
  42.              android:progress="10"/>        
  43.   
  44.         />  
  45.       
  46.     <TextView  
  47.         android:id="@+id/time"  
  48.          android:layout_width="fill_parent"  
  49.         android:layout_height="wrap_content"  
  50.         />  
  51.   
  52.     <TableLayout  
  53.         android:layout_width="fill_parent"  
  54.         android:layout_height="wrap_content"  
  55.         android:stretchColumns="*" >  
  56.   
  57.         <TableRow >  
  58.   
  59.             <Button  
  60.                 android:id="@+id/playBtn"  
  61.                 android:layout_width="wrap_content"  
  62.                 android:layout_height="wrap_content"  
  63.                 android:text="@string/play_text" />  
  64.   
  65.             <Button  
  66.                 android:id="@+id/pauseBtn"  
  67.                 android:layout_width="wrap_content"  
  68.                 android:layout_height="wrap_content"  
  69.                 android:text="@string/pause_text" />  
  70.   
  71.             <Button  
  72.                 android:id="@+id/stopBtn"  
  73.                 android:layout_width="wrap_content"  
  74.                 android:layout_height="wrap_content"  
  75.                 android:text="@string/stop_text" />  
  76.         </TableRow>  
  77.     </TableLayout>  
  78.   
  79. </LinearLayout>  

 

MusicPlayerActivity.java

  1. package cn.csdn.playle;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.media.AudioManager;  
  10. import android.media.MediaPlayer;  
  11. import android.os.Bundle;  
  12. import android.os.Environment;  
  13. import android.os.Handler;  
  14. import android.util.Log;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18. import android.widget.EditText;  
  19. import android.widget.SeekBar;  
  20. import android.widget.SeekBar.OnSeekBarChangeListener;  
  21. import android.widget.TextView;  
  22. import android.widget.Toast;  
  23.   
  24. public class MusicPlayerActivity extends Activity implements OnClickListener,  
  25.         OnSeekBarChangeListener {  
  26.   
  27.     EditText musicEt;  
  28.     Button playBtn, pauseBtn, stopBtn, chooseBtn;  
  29.     TextView time;  
  30.     AudioManager audiomanage;  
  31.     int maxVolume, currentVolume;  
  32.     SeekBar seekBar, seekBarSound;  
  33.     MediaPlayer player = null;  
  34.   
  35.     File file = null;  
  36.   
  37.     int position = 0;  
  38.     int i = 0;  
  39.     Handler handler = new Handler();  
  40.   
  41.   
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.main);  
  45.   
  46.         player = new MediaPlayer();  
  47.   
  48.         findViews();  
  49.     }  
  50.   
  51.     private void findViews() {  
  52.         musicEt = (EditText) this.findViewById(R.id.musicEt);  
  53.         playBtn = (Button) this.findViewById(R.id.playBtn);  
  54.         pauseBtn = (Button) this.findViewById(R.id.pauseBtn);  
  55.         stopBtn = (Button) this.findViewById(R.id.stopBtn);  
  56.         chooseBtn = (Button) this.findViewById(R.id.chooseBtn);  
  57.         seekBar = (SeekBar) this.findViewById(R.id.seekBar);  
  58.         seekBarSound = (SeekBar) this.findViewById(R.id.seekBarSound);  
  59.         time = (TextView) this.findViewById(R.id.time);  
  60.   
  61.   
  62.         time.setOnClickListener(this);  
  63.         playBtn.setOnClickListener(this);  
  64.         pauseBtn.setOnClickListener(this);  
  65.         stopBtn.setOnClickListener(this);  
  66.         stopBtn.setEnabled(false);  
  67.         pauseBtn.setEnabled(false);  
  68.   
  69.         seekBar.setOnSeekBarChangeListener(this);  
  70.   
  71.         chooseBtn.setOnClickListener(new OnClickListener() {  
  72.   
  73.             public void onClick(View v) {  
  74.   
  75.                 Intent intent = new Intent(MusicPlayerActivity.this,  
  76.                         MusicListActivity.class);  
  77.                 startActivity(intent);  
  78.             }  
  79.         });  
  80.   
  81.         audiomanage = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
  82.         maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取系统最大音量  
  83.         seekBarSound.setMax(maxVolume); // 拖动条最高值与系统最大声匹配  
  84.         currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值  
  85.         seekBarSound.setProgress(currentVolume);  
  86.   
  87.         seekBarSound.setOnSeekBarChangeListener(new OnSeekBarChangeListener() // 调音监听器  
  88.                 {  
  89.                     public void onProgressChanged(SeekBar arg0, int progress,  
  90.                             boolean fromUser) {  
  91.                         audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC,  
  92.                                 progress, 0);  
  93.                         currentVolume = audiomanage  
  94.                                 .getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值  
  95.                         seekBarSound.setProgress(currentVolume);  
  96.   
  97.                     }  
  98.   
  99.                     public void onStartTrackingTouch(SeekBar seekBar) {  
  100.                           
  101.                     }  
  102.   
  103.                     public void onStopTrackingTouch(SeekBar seekBar) {  
  104.                           
  105.                     }  
  106.                 });  
  107.   
  108.         Intent intent = this.getIntent();  
  109.         String name = intent.getStringExtra("name");  
  110.         musicEt.setText(name);  
  111.     }  
  112.   
  113.     public void onClick(View v) {  
  114.   
  115.         String fileName = musicEt.getText().toString().trim();  
  116.   
  117.         // 获取sdcard的状态是否已加载 (MEDIA_MOUNTED 加载状态)  
  118.         if (Environment.getExternalStorageState().equals(  
  119.                 Environment.MEDIA_MOUNTED)) {  
  120.             file = new File(Environment.getExternalStorageDirectory(), fileName);  
  121.   
  122.             // 判断所播放的媒体文件是否存在  
  123.             if (file.exists()) {  
  124.   
  125.                 try {  
  126.                     switch (v.getId()) {// 返回一个int值  
  127.   
  128.                     case R.id.playBtn:  
  129.                         playMusic(file);  
  130.   
  131.                         break;  
  132.   
  133.                     case R.id.pauseBtn:  
  134.                         if (player.isPlaying()) {  
  135.                             player.pause();  
  136.                             pauseBtn.setText(R.string.continue_text);  
  137.                         } else {  
  138.                             player.start();  
  139.                             pauseBtn.setText(R.string.pause_text);  
  140.                         }  
  141.                         break;  
  142.   
  143.                     case R.id.stopBtn:  
  144.                         player.stop();  
  145.                         stopBtn.setEnabled(false);  
  146.                         seekBar.setProgress(0);  
  147.                         if (playBtn.getText().toString().equals("重置")) {  
  148.                             playBtn.setText(R.string.play_text);  
  149.                         }  
  150.                         if (pauseBtn.getText().toString().equals("继续")) {  
  151.                             pauseBtn.setText(R.string.pause_text);  
  152.                             pauseBtn.setEnabled(false);  
  153.                         } else {  
  154.                             pauseBtn.setEnabled(false);  
  155.                         }  
  156.   
  157.                         break;  
  158.   
  159.                     }  
  160.                 } catch (IllegalArgumentException e) {  
  161.                     Log.e("TAG", e.toString());  
  162.                 } catch (IllegalStateException e) {  
  163.                     Log.e("TAG", e.toString());  
  164.                 } catch (IOException e) {  
  165.                     Log.e("TAG", e.toString());  
  166.                 }  
  167.             } else {  
  168.                 Toast.makeText(this, R.string.notfoundfile_text,  
  169.                         Toast.LENGTH_LONG).show();  
  170.             }  
  171.         }  
  172.   
  173.         else {  
  174.             Toast.makeText(this, R.string.notfoundSdcard_text,  
  175.                     Toast.LENGTH_LONG).show();  
  176.         }  
  177.     }  
  178.   
  179.     protected void onDestroy() {  
  180.         if (player != null) {  
  181.             if (player.isPlaying()) {  
  182.                 player.stop();  
  183.             }  
  184.             player.release();  
  185.         }  
  186.         super.onDestroy();  
  187.     }  
  188.   
  189.     protected void onPause() {  
  190.         if (player != null) {  
  191.             if (player.isPlaying()) {  
  192.                 player.pause();  
  193.             }  
  194.         }  
  195.         super.onPause();  
  196.     }  
  197.   
  198.     private void playMusic(File file) throws IllegalStateException, IOException {  
  199.   
  200.         if (musicEt.getText().toString() == null  
  201.                 || "".equals(musicEt.getText().toString())) {  
  202.             Toast.makeText(this"没有选中歌曲", Toast.LENGTH_LONG).show();  
  203.         } else {  
  204.             if (player != null) {  
  205.                 pauseBtn.setEnabled(true);  
  206.   
  207.                 player.reset();  
  208.                 player.setDataSource(file.getAbsolutePath());  
  209.                 player.prepare();  
  210.                 player.start();  
  211.                 playBtn.setText(R.string.reset_text);  
  212.                 run();  
  213.             }  
  214.             if (playBtn.getText().toString().equals("重置")) {  
  215.                 pauseBtn.setEnabled(true);  
  216.                 stopBtn.setEnabled(true);  
  217.   
  218.             }  
  219.             if (pauseBtn.getText().toString().equals("继续")) {  
  220.                 pauseBtn.setText(R.string.pause_text);  
  221.             }  
  222.   
  223.         }  
  224.     }  
  225.   
  226.     private void run() {  
  227.         new Thread(new Runnable() {  
  228.             public void run() {  
  229.                 while (player != null) {  
  230.                     int TIME = player.getDuration();  
  231.                     seekBar.setMax(TIME);// 获取歌曲的最大值  
  232.                     position = player.getCurrentPosition();// 返回当前播放进度值  
  233.                     seekBar.setProgress(position);  
  234.   
  235.                     try {  
  236.                         Thread.sleep(3000);  
  237.                     } catch (InterruptedException e) {  
  238.                         e.printStackTrace();  
  239.                     }  
  240.   
  241.                 }  
  242.             }  
  243.   
  244.         }).start();  
  245.     }  
  246.   
  247.     public void onProgressChanged(SeekBar seekBar, int progress,  
  248.             boolean fromUser) {  
  249.   
  250.     }  
  251.   
  252.     public void onStartTrackingTouch(SeekBar seekBar) {  
  253.   
  254.         player.seekTo(seekBar.getProgress());  
  255.   
  256.     }  
  257.   
  258.     public void onStopTrackingTouch(SeekBar seekBar) {  
  259.   
  260.         player.seekTo(seekBar.getProgress());  
  261.     }  
  262.   
  263. }   
  264.  

     

    效果图:

     

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics