簡體   English   中英

按下后退按鈕播放歌曲

[英]Playing song when pressed back button

我正在嘗試播放歌曲,但是當我按下后退按鈕時停止播放歌曲,所以我覆蓋了onbackpress方法但是當我這樣做時它直接將我帶到主屏幕。我正在顯示不同片段的多個歌曲類別通過界面傳遞arraylist和位置。這部分工作正常但我怎么能按下后退按鈕時播放歌曲,我試圖覆蓋暫停方法但也得到一些錯誤。什么是實現這一點的正確方法。我的玩法。

  @Override
public void onFragmentInteraction(ArrayList<Songfileinfo> songarr, int position) {
    playAudio(songarr, position);
    Log.v(""+songarr.size(),"I GOT THE SIZE");
}


private void playAudio(ArrayList<Songfileinfo> arrayList, int audioIndex) {
    //Check is service is active
    if (!serviceBound) {
        //Store Serializable audioList to SharedPreferences
        StorageUtil storage = new StorageUtil(getApplicationContext());
        storage.storeAudio(arrayList);
        storage.storeAudioIndex(audioIndex);

        Intent playerIntent = new Intent(this, MediaPlayerService.class);
        startService(playerIntent);
        bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    } else {
        //Store the new audioIndex to SharedPreferences
        StorageUtil storage = new StorageUtil(getApplicationContext());
        storage.storeAudio(arrayList);
        storage.storeAudioIndex(audioIndex);

        //Service is active
        //Send a broadcast to the service -> PLAY_NEW_AUDIO
        Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
        sendBroadcast(broadcastIntent);
    }
}

當我覆蓋背壓方法時,它會播放歌曲。但是從任何片段中我都會將其帶到主屏幕

你必須在播放時使服務前景。 這樣,服務不會在關閉活動時被銷毀。 不播放任何歌曲時從前台刪除服務。 你不需要覆蓋后退按鈕。

您的Service受您的活動約束。 請閱讀此處: https//developer.android.com/guide/components/bound-services.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM