实现功能:播放网络音频,无需下载,边加载边下载,并支持拖动.此项目参看某一个前辈的博客.
如有问题可以回帖留言,能够得到大家的指导:
代码如下:
布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <Button
- android:id="@+id/btnPlayUrl"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="播放" >
- </Button>
- <SeekBar
- android:id="@+id/skbProgress"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:max="100"
- android:paddingLeft="10dip"
- android:paddingRight="10dip" >
- </SeekBar>
- </LinearLayout>
主Activity
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.SeekBar;
- public class MainActivity extends Activity implements OnClickListener {
- private Button btnPlayUrl;
- private SeekBar skbProgress;
- private Player player;
- int progress;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
- btnPlayUrl.setOnClickListener(this);
- skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
- skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
- player = new Player(skbProgress);
- }
- @Override
- public void onClick(View v) {
- int id = v.getId();
- switch (id) {
- case R.id.btnPlayUrl:
- //网络音频地址
- String url = "http://www........aaa.amr";
- player.playUrl(url);
- break;
- default:
- break;
- }
- }
- class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- MainActivity.this.progress = progress
- * player.mediaPlayer.getDuration() / seekBar.getMax();
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- player.mediaPlayer.seekTo(progress);
- }
- }
- }
播放器
- import java.io.IOException;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.widget.SeekBar;
- public class Player implements MediaPlayer.OnPreparedListener{
- public MediaPlayer mediaPlayer;
- private SeekBar skbProgress;
- private Timer mTimer=new Timer();
- public Player(SeekBar skbProgress)
- {
- this.skbProgress=skbProgress;
- try {
- mediaPlayer = new MediaPlayer();
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- mediaPlayer.setOnPreparedListener(this);
- } catch (Exception e) {
- Log.e("mediaPlayer", "error", e);
- }
- mTimer.schedule(mTimerTask, 0, 10);
- }
- TimerTask mTimerTask = new TimerTask() {
- @Override
- public void run() {
- if(mediaPlayer==null)
- return;
- if(mediaPlayer!=null){
- if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
- handleProgress.sendEmptyMessage(0);
- }
- }
- }
- };
- Handler handleProgress = new Handler() {
- public void handleMessage(Message msg) {
- if(mediaPlayer==null){
- return;
- }
- int position = mediaPlayer.getCurrentPosition();
- int duration = mediaPlayer.getDuration();
- if (duration > 0) {
- long pos = skbProgress.getMax() * position / duration;
- skbProgress.setProgress((int) pos);
- }
- };
- };
- public void playUrl(String videoUrl)
- {
- try {
- mediaPlayer.reset();
- mediaPlayer.setDataSource(videoUrl);
- mediaPlayer.prepareAsync();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onPrepared(MediaPlayer arg0) {
- arg0.start();
- Log.e("mediaPlayer", "onPrepared");
- }
- }
实现图片: