当应用程序关闭时,android studio音频停止播放

rqdpfwrv  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(361)

我正在我的应用程序中制作一个音频播放器。它使用了一个viewlist,一切都很正常。我可以开始暂停,下一个音乐流。我正在加载一个url来播放音乐。当我关闭应用程序时,音乐停止播放。我需要继续比赛。我读过一些关于音频聚焦的文章。但我不知道如何将它添加到代码中。我的舱单上有唤醒锁。
我对android还很陌生,所以这是一个循序渐进的过程,但这个我就是搞不懂。另外,当我关闭应用程序并在播放音乐时再次启动它并重新启动时,应用程序崩溃。

public void controlMusicPlayer(String musicUrl) {

        final String playUrl = musicUrl;

        Log.d("Song url received:", playUrl);
        if (!mediaPlayer.isPlaying()) {
                    playercontrol.setVisibility(View.VISIBLE);
                    btn.setBackgroundResource(R.drawable.pause);

                    new Player()
                     .execute(playUrl);
            playPause = true;

            Log.d("Play State:", "Starting song");

        } else {

            Log.d("Play State:", "Next song is being started");

            playPause = true;

            mediaPlayer.reset();

            btn.setBackgroundResource(R.drawable.pause);
            if (mediaPlayer.isPlaying())
                mediaPlayer.pause();
            playPause = false;

            new Player()
            .execute(playUrl);

        }

            }

       public View.OnClickListener pausePlay = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!playPause) {

                    btn.setBackgroundResource(R.drawable.pause);
                    if (intialStage)
                        new Player()
                                .execute(Urlmusic);
                    else {
                        if (!mediaPlayer.isPlaying())
                            mediaPlayer.start();
                    }
                    playPause = true;
                } else {
                    btn.setBackgroundResource(R.drawable.play);
                    if (mediaPlayer.isPlaying())
                        mediaPlayer.pause();
                    playPause = false;
                }
            }
        };

    /**
     * preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread.
     * @author piyush
     *
     */

    class Player extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog progress;

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            Boolean prepared;
            try {

                mediaPlayer.setDataSource(params[0]);

                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        intialStage = true;
                        playPause=false;
                        btn.setBackgroundResource(R.drawable.play);
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                    }
                });
                mediaPlayer.prepare();
                prepared = true;
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                Log.d("IllegarArgument", e.getMessage());
                prepared = false;
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                prepared = false;
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                prepared = false;
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                prepared = false;
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (progress.isShowing()) {
                progress.cancel();
            }
            Log.d("Prepared", "//" + result);
            mediaPlayer.start();

            intialStage = false;
        }

        public Player() {
            progress = new ProgressDialog(MainActivity.this);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            this.progress.setMessage("Buffering...");
            this.progress.show();

        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (mediaPlayer != null) {
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    ////////////////////////////////// end music play list /////////////////////////////////////////

谢谢你的帮助。

e1xvtsh3

e1xvtsh31#

看起来你释放了 mediaPlayeronPause 方法。关闭应用程序时始终调用此方法。有关活动生命周期的更多信息,请访问google。因此,如果您想在活动暂停时继续播放音频,请更换 onPause 方法 onDestroy (调用finish()时调用此函数。

sxpgvts3

sxpgvts32#

运行后台服务并在该服务中播放音频(媒体播放器),您可以使用前台通知控制该通知的暂停播放或下一步等

相关问题