原文地址:http://blog.csdn.net/wwj_748/article/details/20933055 在此感谢原作者
效果实现:
package com.wwj.download.util; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnBufferingUpdateListener; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.os.Handler; import android.util.Log; import android.widget.SeekBar; public class Player implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener { public MediaPlayer mediaPlayer; // 媒体播放器 private SeekBar seekBar; // 拖动条 private Timer mTimer = new Timer(); // 计时器 // 初始化播放器 public Player(SeekBar seekBar) { super(); this.seekBar = seekBar; try { mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置媒体流类型 mediaPlayer.setOnBufferingUpdateListener(this); mediaPlayer.setOnPreparedListener(this); } catch (Exception e) { e.printStackTrace(); } // 每一秒触发一次 mTimer.schedule(timerTask, 0, 1000); } // 计时器 TimerTask timerTask = new TimerTask() { @Override public void run() { if (mediaPlayer == null) return; if (mediaPlayer.isPlaying() && seekBar.isPressed() == false) { handler.sendEmptyMessage(0); // 发送消息 } } }; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { int position = mediaPlayer.getCurrentPosition(); int duration = mediaPlayer.getDuration(); if (duration > 0) { // 计算进度(获取进度条最大刻度*当前音乐播放位置 / 当前音乐时长) long pos = seekBar.getMax() * position / duration; seekBar.setProgress((int) pos); } }; }; public void play() { mediaPlayer.start(); } /** * * @param url * url地址 */ public void playUrl(String url) { try { mediaPlayer.reset(); mediaPlayer.setDataSource(url); // 设置数据源 mediaPlayer.prepare(); // prepare自动播放 } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 暂停 public void pause() { mediaPlayer.pause(); } // 停止 public void stop() { if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } // 播放准备 @Override public void onPrepared(MediaPlayer mp) { mp.start(); Log.e("mediaPlayer", "onPrepared"); } // 播放完成 @Override public void onCompletion(MediaPlayer mp) { Log.e("mediaPlayer", "onCompletion"); } /** * 缓冲更新 */ @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { seekBar.setSecondaryProgress(percent); int currentProgress = seekBar.getMax() * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration(); Log.e(currentProgress + "% play", percent + " buffer"); } }
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 = 1; private static final int FAILURE = -1; private 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; } } }