1. 程式人生 > >Android實現線上播放音樂

Android實現線上播放音樂

package com.wwj.download;import java.io.File;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import
android.widget.EditText;import android.widget.ProgressBar;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;import android.widget.Toast;import com.wwj.download.util.Player;import com.wwj.net.download.DownloadProgressListener;import com.wwj.net.download.FileDownloader;public
class MainActivity extends Activity private static final int PROCESSING = 1private static final int FAILURE = -1private EditText pathText; // url地址 private TextView resultView; private Button downloadButton; private Button stopButton; private ProgressBar progressBar; private Button playBtn; private
Player player; // 播放器 private SeekBar musicProgress; // 音樂進度 private Handler handler = new UIHandler(); private final class UIHandler extends Handler {  public void handleMessage(Message msg) {   switch (msg.what) {   case PROCESSING: // 更新進度    progressBar.setProgress(msg.getData().getInt("size"));    float num = (float) progressBar.getProgress()      / (float) progressBar.getMax();    int result = (int) (num * 100); // 計算進度    resultView.setText(result + "%");    if (progressBar.getProgress() == progressBar.getMax()) { // 下載完成     Toast.makeText(getApplicationContext(), R.string.success,       Toast.LENGTH_LONG).show();    }    break;   case FAILURE: // 下載失敗    Toast.makeText(getApplicationContext(), R.string.error,      Toast.LENGTH_LONG).show();    break;   }  } } @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  pathText = (EditText) findViewById(R.id.path);  resultView = (TextView) findViewById(R.id.resultView);  downloadButton = (Button) findViewById(R.id.downloadbutton);  stopButton = (Button) findViewById(R.id.stopbutton);  progressBar = (ProgressBar) findViewById(R.id.progressBar);  ButtonClickListener listener = new ButtonClickListener();  downloadButton.setOnClickListener(listener);  stopButton.setOnClickListener(listener);  playBtn = (Button) findViewById(R.id.btn_online_play);  playBtn.setOnClickListener(listener);  musicProgress = (SeekBar) findViewById(R.id.music_progress);  player = new Player(musicProgress);  musicProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent()); } private final class ButtonClickListener implements View.OnClickListener {  @Override  public void onClick(View v) {   switch (v.getId()) {   case R.id.downloadbutton: // 開始下載    // http://abv.cn/music/光輝歲月.mp3,可以換成其他檔案下載的連結    String path = pathText.getText().toString();    String filename = path.substring(path.lastIndexOf('/') + 1);    try {     // URL編碼(這裡是為了將中文進行URL編碼)     filename = URLEncoder.encode(filename, "UTF-8");    } catch (UnsupportedEncodingException e) {     e.printStackTrace();    }    path = path.substring(0, path.lastIndexOf("/") + 1) + filename;    if (Environment.getExternalStorageState().equals(      Environment.MEDIA_MOUNTED)) {     // File savDir =     // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);     // 儲存路徑     File savDir = Environment.getExternalStorageDirectory();     download(path, savDir);    } else {     Toast.makeText(getApplicationContext(),       R.string.sdcarderror, Toast.LENGTH_LONG).show();    }    downloadButton.setEnabled(false);    stopButton.setEnabled(true);    break;   case R.id.stopbutton: // 暫停下載    exit();    Toast.makeText(getApplicationContext(),      "Now thread is Stopping!!", Toast.LENGTH_LONG).show();    downloadButton.setEnabled(true);    stopButton.setEnabled(false);    break;   case R.id.btn_online_play:    new Thread(new Runnable() {     @Override     public void run() {      player.playUrl(pathText.getText().toString());     }    }).start();    break;   }  }  /*   * 由於使用者的輸入事件(點選button, 觸控式螢幕幕....)是由主執行緒負責處理的,如果主執行緒處於工作狀態,   * 此時使用者產生的輸入事件如果沒能在5秒內得到處理,系統就會報“應用無響應”錯誤。   * 所以在主執行緒裡不能執行一件比較耗時的工作,否則會因主執行緒阻塞而無法處理使用者的輸入事件,   * 導致“應用無響應”錯誤的出現。耗時的工作應該在子執行緒裡執行。   */  private DownloadTask task;  private void exit() {   if (task != null)    task.exit();  }  private void download(String path, File savDir) {   task = new DownloadTask(path, savDir);   new Thread(task).start();  }  /**   *    * UI控制元件畫面的重繪(更新)是由主執行緒負責處理的,如果在子執行緒中更新UI控制元件的值,更新後的值不會重繪到螢幕上   * 一定要在主執行緒裡更新UI控制元件的值,這樣才能在螢幕上顯示出來,不能在子執行緒中更新UI控制元件的值   *    */  private final class DownloadTask implements Runnable {   private String path;   private File saveDir;   private FileDownloader loader;   public DownloadTask(String path, File saveDir) {    this.path = path;    this.saveDir = saveDir;   }   /**    * 退出下載    */   public void exit() {    if (loader != null)     loader.exit();   }   DownloadProgressListener downloadProgressListener = new DownloadProgressListener() {    @Override    public void onDownloadSize(int size) {     Message msg = new Message();     msg.what = PROCESSING;     msg.getData().putInt("size", size);     handler.sendMessage(msg);    }   };   public void run() {    try {     // 例項化一個檔案下載器     loader = new FileDownloader(getApplicationContext(), path,       saveDir, 3);     // 設定進度條最大值     progressBar.setMax(loader.getFileSize());     loader.download(downloadProgressListener);    } catch (Exception e) {     e.printStackTrace();     handler.sendMessage(handler.obtainMessage(FAILURE)); // 傳送一條空訊息物件    }   }  } } // 進度改變 class SeekBarChangeEvent implements OnSeekBarChangeListener {  int progress;  @Override  public void onProgressChanged(SeekBar seekBar, int progress,    boolean fromUser) {   // 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()   this.progress = progress * player.mediaPlayer.getDuration()     / seekBar.getMax();  }  @Override  public void onStartTrackingTouch(SeekBar seekBar) {  }  @Override  public void onStopTrackingTouch(SeekBar seekBar) {   // seekTo()的引數是相對與影片時間的數字,而不是與seekBar.getMax()相對的數字   player.mediaPlayer.seekTo(progress);  } } @Override protected void onDestroy() {  super.onDestroy();  if (player != null) {   player.stop();   player = null;  } }}