如何在使用 service/activty 控制媒体播放器的同时在后台播放它?

How to play a mediaplayer in the background while controlling it with a service/activty?

我正在开发一个包含 运行s mp3 文件的应用程序,我希望该应用程序在 activity 关闭后 运行 mp3 文件,在这种情况下我使用了服务,但我想将 activity 与媒体播放器连接以显示搜索栏进度并停止并从 activity 播放媒体播放器,我尝试在服务和 activity 之间使用广播,但是好像很费资源,请问有更好的方法吗?

这是我的部分做法

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

}

我认为接收器效率不高,而且我的服务中还有另一个接收器,如何在从 activity/service[=12= 控制媒体播放器的同时在后台播放音乐]

使用 "bound service" 然后,它很可能会成为 "bound local service",可以找到有关此模式的更多信息 here