音乐网络播放

package ***.***.***;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {
    private Button btnPause,btnPlayUrl,btnStop;
    private SeekBar skbProgress;
    private Player player;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnPause = (Button) findViewById(R.id.btnPause);
        btnPause.setOnClickListener(new ClickEvent());

        btnPlayUrl= (Button) findViewById(R.id.btnPlayUrl);
        btnPlayUrl.setOnClickListener(new ClickEvent());

        btnStop= (Button) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(new ClickEvent());

        skbProgress= (SeekBar) findViewById(R.id.skbProgress);
        skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
        player=new Player(skbProgress);
    }

    class ClickEvent implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            if (view==btnPause){
                if (!isPause) {
                    player.pause();
                    btnPause.setText("继续");
                    isPause = true;
                    btnPlayUrl.setEnabled(true);
                } else {
                    isPause = false;
                    player.play();
                    btnPause.setText("暂停");
                    btnPlayUrl.setEnabled(false);
                }
            }else if (view==btnPlayUrl){
               player.play();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String url = "http://music.wufazhuce.com/lkX5S8HVBeQglFRGjZKVodnz-25C";
                        player.playUrl(url);
                    }
                }).start();
                btnPause.setText("暂停");
                btnPlayUrl.setEnabled(false);
                btnPause.setEnabled(true);
                btnStop.setEnabled(true);
            }else if (view==btnStop){
                player.stop();
                isPause = false;
                btnPause.setText("暂停");
                btnPause.setEnabled(false);
                btnPlayUrl.setEnabled(true);
                btnStop.setEnabled(false);
            }

        }
    }
    class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{
        int progress;
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            //当前音乐播放位置*当前音乐时长/进度条最大刻度
            this.progress=i*player.mediaPlayer.getDuration()/seekBar.getMax();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // seekTo()的参数是相对与影片时间的数字,而不是与seekBar.getMax()相对的数字
            player.mediaPlayer.seekTo(progress);
        }
    }
}
package ***.***.***;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.SeekBar;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by user on 2016/9/4.
 */
public class Player implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener,
        MediaPlayer.OnPreparedListener {
    public MediaPlayer mediaPlayer; // 媒体播放器
    private SeekBar skbProgress;// 拖动条
    private Timer mTimer = new Timer();// 计时器

    // 初始化播放器
    public Player(SeekBar skbProgress) {
        this.skbProgress = skbProgress;

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mTimer.schedule(mTimerTask, 0, 1000);// 每一秒触发一次
    }

    //    通过定时器和Handler来更新进度条
    TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            if (mediaPlayer == null) {
                return;
            }
            if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
                handlerProgress.sendEmptyMessage(0);

            }
        }

    };
    Handler handlerProgress = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int position = mediaPlayer.getCurrentPosition();
            int duration = mediaPlayer.getDuration();
            if (duration > 0) {
                // 计算进度(获取进度条最大刻度*当前音乐播放位置 / 当前音乐时长)
                long pos = skbProgress.getMax() * position / duration;
                skbProgress.setProgress((int) pos);
            }

        }
    };

    public void play() {
        mediaPlayer.start();
    }

    public void playUrl(String vidoUrl) {//资源来源
        try {
           // mediaPlayer.reset();//音乐声明周期的开始
            mediaPlayer.setDataSource(vidoUrl);// 设置数据源
            mediaPlayer.prepare();//prepare后自动播放
        } 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 onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
        skbProgress.setSecondaryProgress(i);
        int currentProgress = skbProgress.getMax() * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
        Log.e(currentProgress + "% play", i + "% buffer");
    }

    // 播放完成
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {

    }

    // 播放准备
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();

    }
}

这里有demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值