서비스 / 액티비티로 제어하면서 백그라운드에서 미디어 플레이어를 재생하는 방법은 무엇입니까?

모하메드 나다

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

}

리시버가 효율적이라고 생각하지 않고 내 서비스에 다른 리시버도 있습니다. 활동 / 서비스에서 미디어 플레이어를 제어하면서 백그라운드에서 음악을 재생하는 방법

pskink

"바운드 서비스"를 사용하면 "바운드 로컬 서비스"가 될 가능성이 높습니다.이 패턴에 대한 자세한 정보는 여기 에서 찾을 수 있습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관