在Android中,当我播放音频并转到其他活动时,音频在返回后不会停止

时间:2017-06-07 15:25:46

标签: android audio

我是Android环境中的新手,我的问题是在我播放音频文件并转到其他活动或按下后退按钮,然后再次打开我的应用程序,我已经播放了音频,它没有'按停止按钮时停止。当我按下主页按钮并尝试停止时,它工作。任何人都可以建议我做什么  这是我活动的代码

package com.example.mudassar.myaudioapp;

import android.content.Intent;
import android.os.Environment;
import android.os.NetworkOnMainThreadException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //MediaPlayer mp;
    Button playBtn, pauseBtn, stopBtn;
    SeekBar sb;
    Handler han = new Handler();
    MediaPlayer mp;// = MediaPlayer.create(MainActivity.this, R.raw.firefly);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        playBtn= (Button)findViewById(R.id.playBtn);
        pauseBtn= (Button)findViewById(R.id.pauseBtn);
        stopBtn=(Button) findViewById(R.id.stopBtn);




        try{
            //you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3
            //mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/raw/.mp3");
            mp = MediaPlayer.create(MainActivity.this, R.raw.firefly);
            mp.prepare();
        }catch(Exception e){e.printStackTrace();}

        sb = (SeekBar)findViewById(R.id.seekBar);
        sb.setMax(mp.getDuration());

        SeekUpdation();

        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SeekUpdation();
                //if (mp == null)
                if(mp.isPlaying() == false)
                    mp = MediaPlayer.create(MainActivity.this, R.raw.firefly);
                mp.start();
                sb.setMax(mp.getDuration());
                sb.setProgress(mp.getCurrentPosition());
            }
        });

        pauseBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if(mp.isPlaying() == false){
                    mp.start();
                    sb.setProgress(mp.getCurrentPosition());
                    Toast.makeText(MainActivity.this, "Playing", Toast.LENGTH_SHORT).show();
                }else if(mp.isPlaying() == true){
                    mp.pause();
                    sb.setProgress(mp.getCurrentPosition());
                    Toast.makeText(MainActivity.this, "Paused", Toast.LENGTH_SHORT).show();
                }

            }
        });

        stopBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //if (mp != null) {
                if(mp.isPlaying() == true){
                    mp.stop();
                    sb.setProgress(mp.getDuration());
                    //mp = null;
                }
                Toast.makeText(MainActivity.this, "Stoped", Toast.LENGTH_SHORT).show();
            }
        });

        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
            @Override
            public void onStopTrackingTouch(SeekBar seekBar){
                //if(mp!=null){
                if(!mp.isPlaying()){
                    mp.seekTo(seekBar.getProgress());
                    mp.start();
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar){
                //if(mp!=null)
                 if(mp.isPlaying()){
                     mp.pause();
                 }else
                     mp.start();
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){

            }
        });

        Button btn = (Button)findViewById(R.id.secondActivityButton);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, second.class));
            }
        });
    }

    public void MyOwnClick(View v){
        startActivity(new Intent(MainActivity.this, second.class));
    }

    public Runnable run = new Runnable() {
        @Override
        public void run() {
            SeekUpdation();
        }
    };

    public void SeekUpdation() {
        //if(mp!=null)
        if(mp.isPlaying() == true)
        {
            sb.setProgress(mp.getCurrentPosition());
            han.postDelayed(run, 1000);
            //Toast.makeText(MainActivity.this, "is Playing", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(MainActivity.this, "Seek", Toast.LENGTH_SHORT).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

要获得快速解决方案,您可以创建如下方法:

void resetMediaPlayer() {
   if(mediaPlayer!=null) {
       try {
             mediaPlayer.reset();
       }
       catch(Exception e) {
           e.printStackTrace();
       }
   }
}

然后覆盖onBackPressed中的activity方法并致电resetMediaPlayer

@Override
public void onBackPressed(){
    super.onBackPressed();
  resetMediaPlayer()
}

或者在需要停止media player的任何地方拨打电话。

这不是阻止媒体播放器的好方法。为了更好地实现,您必须更改resetMediaPlayer方法,如下所示:

void resetMediaPlayer() {
    if(mediaPlayer!=null) {
        try {
            mediaPlayer.reset();
            mediaPlayer.release();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

每当你想开始Media Player时,你必须创建一个新的Media Player实例,例如:

void startMediaPlayer(int selectedSong) {
    try {
        mediaPlayer = MediaPlayer.create(activity, selectedSong);
        mediaPlayer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

并使用resetMediaPlayer的{​​{1}}方法拨打onDestroy

activity