使用service实现播放器
播放器:bilibili IJKplayer 的 exoplayer 框架
播放器
播放器service代码:
public class PlayerService extends Service {
private IjkExoMediaPlayer player;
public static final int PLAYING = 1;//正在播放
public static final int PLAYSTOP = 2;//停止
public static final int PLAYPAUSE = 3;// 暂停
public int playerState = PLAYSTOP;
public ArrayList<ChapterItem> playerList;//播放资源列表
public int position;
public int currentTime;
public int seekTime = 0;
public int update = 0;
public int intenetState = NOINTENET;
public static final int NOINTENET = 0;//网络状态
public static final int WIFY = 1;
public static final int PHONE = 2;
public int buffering = 0;//缓冲进度
public int clock = 0; // 定时
public boolean isPauseForCall = false; //是否因为来电导致播放暂停
public int allTims=0; //播放总时长
PhoneStateListener phoneStateListener = new PhoneStateListener() {// 来电监听 来电暂停 结束继续播放
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int ringvolume = audioManager
.getStreamVolume(AudioManager.STREAM_RING);
if (ringvolume > 0) {
if (player != null && player.isPlaying()) {
isPauseForCall = true;
pause();
}
}
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
if (player != null && player.isPlaying()) {
isPauseForCall = true;
pause();
}
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
if (isPauseForCall) { //是否是电话导致播放器暂停
reStart();
isPauseForCall = false;
}
}
}
};
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Common.GET_OK:
setData(msg.obj.toString());
break;
case Common.INIT_OK:
handler.sendEmptyMessageDelayed(Common.INIT_OK, 1000);
if (player != null && player.getCurrentPosition() > 0) {//广播发送 缓冲 和播放时间
buffering=player.getBufferedPercentage();
currentTime = (int) player.getCurrentPosition();
Intent intent = new Intent();
intent.setAction(Common.PlayerTime);
intent.putExtra("currentTime", currentTime);
intent.putExtra("buffering", buffering);
sendBroadcast(intent);
}
if (update == 0) {//播放记录
update++;
if (sharedPreferences != null) {// 用户最后一次播放记录