如何在使用服务/活动控制媒体播放器的同时在后台播放媒体播放器?

时间:2016-11-22 12:04:03

标签: android android-activity android-service media-player

我正在开发一个运行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);
    }
}

}

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

1 个答案:

答案 0 :(得分:0)

使用&#34;绑定服务&#34;那么,很可能它将成为一个受限制的本地服务&#34;,可以找到有关此模式的更多信息here

相关问题