如何在使用服务/激活控制的同时,如何在背景中播放媒体播放器



我是一个正在开发运行mp3文件的应用程序,我希望该应用程序在关闭活动后运行mp3文件,在这种情况下,我使用了服务与媒体播放器一起展示Seekbar进度并停止并从活动中播放媒体播放器的活动,我试图在服务和活动之间使用广播,但似乎花费了很多资源,是否有更好的方法来解决这个问题?/p>

这是我完成的一部分

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);
    }
}

}

我不认为接收器是有效的,加上我的服务也有另一个接收器,如何在背景中播放音乐,同时从活动/服务中控制媒体播放器

使用"绑定服务",然后,它很可能是"绑定的本地服务",可以在此处找到有关此模式的更多信息

最新更新