按下后退按钮时如何停止播放音频?

时间:2016-03-16 06:46:47

标签: android audio android-mediaplayer

我的媒体播放器和活动存在问题。当我尝试按“返回”按钮时,音频播放不会停止。请帮我解决这个问题。

我正在使用Android Studio 1.5版

这是我的代码:

 public class DoaBangunTidur extends ActionBarActivity implements Runnable,    View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private SeekBar seekBar;
    private Button startMedia;
    private Button stopMedia;
    private MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doa_bangun_tidur);
        seekBar = (SeekBar) findViewById(R.id.seekBar4);
        startMedia = (Button) findViewById(R.id.button10);
        stopMedia = (Button) findViewById(R.id.button11);
        startMedia.setOnClickListener(this);
        stopMedia.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(this);
        seekBar.setEnabled(false);
        setTitle("Doa Bangun Tidur");
    }
    public void run() {
        int currentPosition = mp.getCurrentPosition();
        int total = mp.getDuration();

        while (mp != null && currentPosition < total) {
            try {
                Thread.sleep(1000);
                currentPosition = mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            seekBar.setProgress(currentPosition);
        }
    }
    //---> Button ditekan
    public void onClick(View v) {
        if (v.equals(startMedia)) {
            if (mp == null) {
                mp = MediaPlayer.create(getApplicationContext(), R.raw.selepastidur);
                seekBar.setEnabled(true);
            }
            if (mp.isPlaying()) {
                mp.pause();
                mp.release();

            } else {
                mp.start();
                seekBar.setMax(mp.getDuration());
                new Thread(this).start();
            }
        }

        if (v.equals(stopMedia) && mp != null) {
            if (mp.isPlaying() || mp.getDuration() > 0) {
                mp.stop();
                mp = null;
                seekBar.setProgress(0);
            }
        }
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
                                  boolean fromUser) {
        try {
            if (mp.isPlaying() || mp != null) {
                if (fromUser)
                    mp.seekTo(progress);
            } else if (mp == null) {
                Toast.makeText(getApplicationContext(), "Media is not running",
                        Toast.LENGTH_SHORT).show();
                seekBar.setProgress(0);
            }
        } catch (Exception e) {
            Log.e("seek bar", "" + e);
            seekBar.setEnabled(false);

        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }
    public void text_doasebelumtidur(View view) {
        AlertDialog.Builder text = new AlertDialog.Builder(this);
        text.setMessage("bismillaahir rahmaanir rahiim. bismi kallaahumma ahyaa wa amuut.")
                .create();
        text.show();
    }
    //--->> Action Bar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_actions, menu);

        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.tentangaplikasi:
                // about apps
                MA();
                return true;
            case R.id.maklumbalas:
                // feedback
                MB();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    private void MA() {
        Intent a = new Intent(this, MengenaiAplikasi.class);
        startActivity(a);
    }
    private void MB() {
        Intent b = new Intent(this, MaklumBalas.class);
        startActivity(b);
    }
}

2 个答案:

答案 0 :(得分:0)

您可以覆盖java.lang.UnsupportedOperationException: Server push requests are not supported by the AJP protocol org.apache.coyote.ajp.AjpProcessor.action(AjpProcessor.java:612) org.apache.coyote.Request.action(Request.java:392) http2.SimpleImagePush.doGet(SimpleImagePush.java:73) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) ,以便在用户按下后退按钮时收到通知。不要忘记致电onBackPressed(),否则您的super.onBackPressed()仍在屏幕上。通常这不是用户期望的东西。

Activity

答案 1 :(得分:0)

试试这个

@Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mp.isPlaying()) {
        mp.stop(); // or mp.pause();
        mp.release();
        }
    }

这会停止你的音频。

希望它能帮到你