应用程序关闭时,Android工作室音频停止播放

时间:2016-03-28 12:52:55

标签: java android audio android-studio

我正在我的应用中制作音频播放器。它使用了一个视图列表,一切都运行良好。我可以开始暂停,下一个音乐流。我正在加载一个网址来播放音乐。当我关闭应用程序时,音乐停止播放。我需要继续比赛。我读过有关音频焦点的内容。但我不确定如何将其添加到代码中。我的清单中有wake_lock。

我对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 /////////////////////////////////////////

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您似乎在mediaPlayer方法中发布了onPause。关闭应用程序时始终会调用此方法。有关更多信息,请访问activity lifecycle。因此,如果您希望在暂停活动时继续播放音频,请将onPause方法替换为onDestroy(调用finish()时调用此方法。