我正在开发一个运行mp3文件的应用程序,我希望应用程序在活动关闭后运行mp3文件,在这种情况下我使用了服务,但我想将活动与媒体播放器连接以显示搜索条进展和停止并从活动中播放媒体播放器,我尝试在服务和活动之间使用广播,但似乎花费了大量资源,有更好的方法吗?
以下是我如何做到的一部分
public class Reciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int command = intent.getExtras().getInt("command");
if (command == 0) {
progressDialog.setMessage("loading...");
progressDialog.show();
}
if (command == 1) {
int value = intent.getExtras().getInt("value");
seekBar.setMax(value);
int seconds = (value / 1000) % 60;
int minutes = ((value / (1000 * 60)) % 60);
int hours = ((value / (1000 * 60 * 60)) % 24);
if (hours != 0) {
if (seconds < 10) {
durationTV.setText(hours + ":" + minutes + ":0" + seconds);
} else {
durationTV.setText(hours + ":" + minutes + ":" + seconds);
}
} else {
if (seconds < 10) {
durationTV.setText(minutes + ":0" + seconds);
} else {
durationTV.setText(minutes + ":" + seconds);
}
}
progressDialog.dismiss();
}
if (command == 2) {
int currentDuration = intent.getExtras().getInt("value");
if(!movingSeekBar) seekBar.setProgress(currentDuration);
int seconds = (currentDuration / 1000) % 60;
int minutes = ((currentDuration / (1000 * 60)) % 60);
int hours = ((currentDuration / (1000 * 60 * 60)) % 24);
if (hours != 0) {
if (seconds < 10) {
currentPositionTV.setText(hours + ":" + minutes + ":0" + seconds);
} else {
currentPositionTV.setText(hours + ":" + minutes + ":" + seconds);
}
} else {
if (seconds < 10) {
currentPositionTV.setText(minutes + ":0" + seconds);
} else {
currentPositionTV.setText(minutes + ":" + seconds);
}
}
}
if (command == 3) {
bufferPercent = intent.getExtras().getInt("value");
seekBar.setSecondaryProgress(bufferPercent);
Log.d("Media Player", "" + bufferPercent);
}
}
}
我不认为接收器是有效的,而且我的服务上还有另一个接收器,如何在从活动/服务控制媒体播放器的同时播放背景音乐