Android实现播放器功能

package com.example.yzj.android_7_27;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MusicActivity extends AppCompatActivity implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnPreparedListener {
private SeekBar sb;
private Button btn_play;
private Button btn_pause;
private Button btn_stop;
private Button btn_last;
private Button btn_next;
private MediaPlayer mediaPlayer;
private BtnListener btnlistener;
private MyHandler myHandler;
private Timer timer;
private Thread thread;
private boolean pause;
private ContentResolver resolver;
private List<MusicInfo> data;
private int currentMusic;
private int currentPosition;
private boolean stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
init();
setListener();
setMediaPlayer();
}

private void setListener() {
btn_play.setOnClickListener(btnlistener);
btn_pause.setOnClickListener(btnlistener);
btn_stop.setOnClickListener(btnlistener);
btn_last.setOnClickListener(btnlistener);
btn_next.setOnClickListener(btnlistener);
}


public void setMediaPlayer() {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(data.get(currentMusic).getStrMusicPath());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnPreparedListener(this);
} catch (IOException e) {
e.printStackTrace();
}
}

public void start() {
if (pause) {
float i=(float)sb.getProgress()/(float)sb.getMax();
float j=mediaPlayer.getDuration();
float n=i*j;
int m=(int)n;
mediaPlayer.seekTo(m);
mediaPlayer.start();
} else if (!mediaPlayer.isPlaying()) {
mediaPlayer.prepareAsync();
if(stop){
mediaPlayer.seekTo(0);
}
}
pause = false;
stop=false;
if (!thread.isAlive()) {
try{
thread.start();
}
catch (Exception e) {
thread=new Thread(timer);
thread.start();
}
}
}

public void pause() {
if (mediaPlayer != null) {
mediaPlayer.pause();
}
pause = true;
}

public void stop() {
mediaPlayer.stop();
stop=true;
}

private void init() {
sb = (SeekBar) findViewById(R.id.sb);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
currentPosition=sb.getProgress();
}
});
btn_play = (Button) findViewById(R.id.btn_play);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_last = (Button) findViewById(R.id.btn_last);
btn_next = (Button) findViewById(R.id.btn_next);
data=new ArrayList<>();
getMusicInfo();
btnlistener = new BtnListener();
myHandler = new MyHandler();
timer = new Timer();
thread = new Thread(timer);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {

}

@Override
public void onPrepared(MediaPlayer mp) {
float i=(float)currentPosition/(float)sb.getMax();
float j=mediaPlayer.getDuration();
float n=i*j;
int m=(int)n;
mediaPlayer.seekTo(m);
mediaPlayer.start();
}

private class BtnListener implements View.OnClickListener {

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:
start();
break;
case R.id.btn_pause:
pause();
break;
case R.id.btn_stop:
stop();
break;
case R.id.btn_last:
currentMusic--;
if (currentMusic==-1) {
currentMusic=data.size()-1;
}
if (pause) {
pause = false;
}
try{
              thread.start();
              }
            catch(Exception e){
                Log.v("msg","线程已经启动");
              }


mediaPlayer.reset();
try {
mediaPlayer.setDataSource(data.get(currentMusic).getStrMusicPath());
mediaPlayer.prepareAsync();

} catch (IOException e) {
e.printStackTrace();
}
thread.interrupt();
break;
case R.id.btn_next:
currentMusic++;
if (pause) {
pause = false;
}
             try{
              thread.start();
              }
            catch(Exception e){
                Log.v("msg","线程已经启动");
              }
                    mediaPlayer.reset();
if(currentMusic>data.size()-1){
currentMusic=0;
}
try {
mediaPlayer.setDataSource(data.get(currentMusic).getStrMusicPath());
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
thread.interrupt();
break;
}
}
}

private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123) {
int position = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
if (duration > 0) {
long pos = sb.getMax() * position / duration;
sb.setProgress((int) pos);
}
}
}
}

private class Timer implements Runnable {
@Override
public void run() {
while (!pause&&!stop) {
try {
Message msg = myHandler.obtainMessage();
msg.what = 0x123;
msg.sendToTarget();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private void getMusicInfo() {
resolver = getContentResolver();
Cursor musicCursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
//获取歌曲时间
while (musicCursor.moveToNext()) {
MusicInfo musicInfo = new MusicInfo();
int duration = musicCursor.getInt(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
String strMusicName = musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String strMusicPath = musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
musicInfo.setnMusicTime(duration);
musicInfo.setStrMusicName(strMusicName);
musicInfo.setStrMusicPath(strMusicPath);
data.add(musicInfo);
}
}
}





package com.example.yzj.android_7_27;

import java.io.Serializable;

/**
* Created by YZJ on 2016/7/27.
*/
public class MusicInfo implements Serializable {
private String strMusicName;
private int nMusicTime;
private String strMusicPath;

public String getStrMusicName() {
return strMusicName;
}

public void setStrMusicName(String strMusicName) {
this.strMusicName = strMusicName;
}

public int getnMusicTime() {
return nMusicTime;
}

public void setnMusicTime(int nMusicTime) {
this.nMusicTime = nMusicTime;
}

public String getStrMusicPath() {
return strMusicPath;
}

public void setStrMusicPath(String strMusicPath) {
this.strMusicPath = strMusicPath;
}
}
posted @ 2016-07-28 21:28  麦子TMAC  阅读(304)  评论(0编辑  收藏  举报