Skip to content

Commit

Permalink
添加定时播放倒计时进度回调
Browse files Browse the repository at this point in the history
  • Loading branch information
EspoirX committed Jul 18, 2018
1 parent 9e31f15 commit 91804f3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ interface NotifyMusicSwitch {

interface NotifyTimerTask {
void notifyTimerTasFinish();

void onTimerTick(long millisUntilFinished, long totalTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface OnTimerTaskListener {
void onTimerFinish();

void onTimerTick(long millisUntilFinished,long totalTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package com.lzx.musiclibrary.aidl.source;
// Declare any non-default types here with import statements

import android.os.RemoteException;

public interface IOnTimerTaskListener extends android.os.IInterface {
/**
* Local-side IPC implementation stub class.
Expand All @@ -22,6 +24,7 @@ public Stub() {
/**
* Cast an IBinder object into an IOnTimerTaskListener interface,
* generating a proxy if needed.
*
* @param obj
* @return IOnTimerTaskListener
*/
Expand Down Expand Up @@ -54,6 +57,14 @@ public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel re
reply.writeNoException();
return true;
}
case TRANSACTION_onTimerTick: {
data.enforceInterface(DESCRIPTOR);
long _arg0 = data.readLong();
long _arg1 = data.readLong();
this.onTimerTick(_arg0, _arg1);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
Expand Down Expand Up @@ -87,10 +98,29 @@ public void onTimerFinish() throws android.os.RemoteException {
_data.recycle();
}
}

@Override
public void onTimerTick(long millisUntilFinished, long totalTime) throws RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeLong(millisUntilFinished);
_data.writeLong(totalTime);
mRemote.transact(Stub.TRANSACTION_onTimerTick, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}

static final int TRANSACTION_onTimerFinish = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_onTimerTick = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}

public void onTimerFinish() throws android.os.RemoteException;
void onTimerFinish() throws android.os.RemoteException;

void onTimerTick(long millisUntilFinished, long totalTime) throws android.os.RemoteException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ public void notifyTimerTasFinish() {
mOnTimerTaskListenerList.finishBroadcast();
}
}

@Override
public void onTimerTick(long millisUntilFinished, long totalTime) {
synchronized (PlayControl.class) {
final int N = mOnTimerTaskListenerList.beginBroadcast();
for (int i = 0; i < N; i++) {
IOnTimerTaskListener listener = mOnTimerTaskListenerList.getBroadcastItem(i);
if (listener != null) {
try {
listener.onTimerTick(millisUntilFinished, totalTime);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
mOnTimerTaskListenerList.finishBroadcast();
}
}
}

public Playback getPlayback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,16 @@ public int getAudioSessionId() {
}


public float getPlaybackSpeed() {
public float getPlaybackSpeed() {
return mPlayback.getPlaybackSpeed();
}


public float getPlaybackPitch() {
public float getPlaybackPitch() {
return mPlayback.getPlaybackPitch();
}

void pausePlayInMillis(long time) {
void pausePlayInMillis(final long time) {
mTimerTaskManager.cancelCountDownTask();
if (time != -1) {
mTimerTaskManager.starCountDownTask(time, new TimerTaskManager.OnCountDownFinishListener() {
Expand All @@ -267,7 +267,7 @@ public void onFinish() {

@Override
public void onTick(long millisUntilFinished) {

mNotifyTimerTask.onTimerTick(millisUntilFinished, time);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.lzx.musiclibrary.manager;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -14,7 +11,6 @@
import android.text.TextUtils;

import com.danikula.videocache.ProxyCacheUtils;
import com.lzx.musiclibrary.MusicService;
import com.lzx.musiclibrary.aidl.listener.OnPlayerEventListener;
import com.lzx.musiclibrary.aidl.listener.OnTimerTaskListener;
import com.lzx.musiclibrary.aidl.model.SongInfo;
Expand Down Expand Up @@ -49,7 +45,8 @@ public class MusicManager implements IPlayControl {
public static final int MSG_PLAYER_ERROR = 4;
public static final int MSG_BUFFERING = 5;
public static final int MSG_TIMER_FINISH = 6;
public static final int MSG_PLAYER_STOP = 7;
public static final int MSG_TIMER_TICK = 7;
public static final int MSG_PLAYER_STOP = 8;

private Context mContext;
private boolean isOpenCacheWhenPlaying = false;
Expand Down Expand Up @@ -176,6 +173,17 @@ public void onAsyncLoading(boolean isFinishLoading) {
public void onTimerFinish() {
mClientHandler.obtainMessage(MSG_TIMER_FINISH).sendToTarget();
}

@Override
public void onTimerTick(long millisUntilFinished, long totalTime) {
Bundle bundle = new Bundle();
bundle.putLong("millisUntilFinished", millisUntilFinished);
bundle.putLong("totalTime", totalTime);
Message message = Message.obtain();
message.setData(bundle);
message.what = MSG_TIMER_TICK;
mClientHandler.sendMessage(message);
}
};

private static class ClientHandler extends Handler {
Expand Down Expand Up @@ -218,7 +226,13 @@ public void handleMessage(Message msg) {
manager.mStateObservable.stateChangeNotifyObservers(MSG_BUFFERING);
break;
case MSG_TIMER_FINISH:
manager.notifyTimerTaskEventChange(MSG_TIMER_FINISH);
manager.notifyTimerTaskEventChange(MSG_TIMER_FINISH, -1, -1);
break;
case MSG_TIMER_TICK:
Bundle bundle = msg.getData();
long millisUntilFinished = bundle.getLong("millisUntilFinished");
long totalTime = bundle.getLong("totalTime");
manager.notifyTimerTaskEventChange(MSG_TIMER_TICK, millisUntilFinished, totalTime);
break;
case MSG_PLAYER_STOP:
manager.notifyPlayerEventChange(MSG_PLAYER_STOP, null, null, false);
Expand Down Expand Up @@ -299,10 +313,12 @@ private void notifyPlayerEventChange(int msg, SongInfo info, String errorMsg, bo
}
}

private void notifyTimerTaskEventChange(int msg) {
private void notifyTimerTaskEventChange(int msg, long millisUntilFinished, long totalTime) {
for (OnTimerTaskListener listener : mOnTimerTaskListeners) {
if (msg == MSG_TIMER_FINISH) {
listener.onTimerFinish();
} else if (msg == MSG_TIMER_TICK) {
listener.onTimerTick(millisUntilFinished, totalTime);
}
}
}
Expand Down

0 comments on commit 91804f3

Please sign in to comment.