Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 停止MediaPlayer的问题_Java_Android_Audio_Android Mediaplayer - Fatal编程技术网

Java 停止MediaPlayer的问题

Java 停止MediaPlayer的问题,java,android,audio,android-mediaplayer,Java,Android,Audio,Android Mediaplayer,我的应用程序包含一个mediaplayer,在audio.xml中有一个(play/pause)按钮,但问题是我无法在页面被破坏之前找到正确的代码来停止mediaplayer,因此当我登录音频页面(xml)并在不运行mediaplayer的情况下退出时,应用程序崩溃,我使用的代码如下: public class audio extends Langue implements Runnable, OnClickListener, OnSeekBarChangeListener

我的应用程序包含一个mediaplayer,在audio.xml中有一个(play/pause)按钮,但问题是我无法在页面被破坏之前找到正确的代码来停止mediaplayer,因此当我登录音频页面(xml)并在不运行mediaplayer的情况下退出时,应用程序崩溃,我使用的代码如下:

 public class audio extends Langue implements Runnable,

        OnClickListener, OnSeekBarChangeListener {
             private SeekBar seekBar;
             private Button startMedia;
            // private Button stopMedia;
         private MediaPlayer mp;


         @Override
         public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.audio);
          seekBar = (SeekBar) findViewById(R.id.seekBar1);
          startMedia = (Button) findViewById(R.id.button1);
          //stopMedia = (Button) findViewById(R.id.button2);
          startMedia.setOnClickListener(this);
          //stopMedia.setOnClickListener(this);
          seekBar.setOnSeekBarChangeListener(this);
          seekBar.setEnabled(false);


         }

         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);
          }
         }

         public void onClick(View v) {
          if (v.equals(startMedia)) {
           if (mp == null) {
            mp = MediaPlayer.create(getApplicationContext(), R.raw.espoir);
            seekBar.setEnabled(true);
           }
           if (mp.isPlaying()) {
            mp.pause();
            startMedia.setText("Play");
           } else {
            mp.start();
            startMedia.setText("Pause");
            seekBar.setMax(mp.getDuration());
            new Thread(this).start();
           }
          }

         /*  if (v.equals(stopMedia) && mp != null) {
           if (mp.isPlaying() || mp.getDuration() > 0) {
            mp.stop();
            mp = null;
            startMedia.setText("Play");
            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
                                                          }

         @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case android.R.id.home:
                    this.finish();
                    return true;

                                          }
                return super.onOptionsItemSelected(item);
                                                                }
        // stop song
            @Override
            protected void onPause(){
             super.onPause();
             mp.stop();
            finish();
                                    } 


        }
公共类音频扩展语言实现可运行,
OnClickListener,OnSeekBarchangListener{
私人SeekBar SeekBar;
私人按钮开始媒体;
//私人媒体;
私人媒体播放器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
seekBar=(seekBar)findViewById(R.id.seekBar1);
startMedia=(按钮)findViewById(R.id.button1);
//stopMedia=(按钮)findViewById(R.id.button2);
startMedia.setOnClickListener(此);
//stopMedia.setOnClickListener(这个);
seekBar.setonseekbarchaneglistener(这个);
seekBar.setEnabled(false);
}
公开募捐{
int currentPosition=mp.getCurrentPosition();
int total=mp.getDuration();
while(mp!=null&¤tPosition0){
mp.stop();
mp=null;
startMedia.setText(“播放”);
seekBar.setProgress(0);
}
}
*/
}
更改了公共进程无效(SeekBar SeekBar,int progress,
布尔值(用户){
试一试{
if(mp.isPlaying()| | mp!=null){
如果(来自用户)
希克托议员(进步党);
}else if(mp==null){
Toast.makeText(getApplicationContext(),“媒体未运行”,
吐司。长度(短)。show();
seekBar.setProgress(0);
}
}捕获(例外e){
Log.e(“搜索栏”,即“+e”);
seekBar.setEnabled(false);
}
}
@凌驾
开始跟踪触摸时的公共无效(SeekBar SeekBar){
//TODO自动生成的方法存根
}
@凌驾
TopTrackingTouch(SeekBar SeekBar)上的公共无效{
//TODO自动生成的方法存根
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
这个;
返回true;
}
返回super.onOptionsItemSelected(项目);
}
//停止歌
@凌驾
受保护的void onPause(){
super.onPause();
mp.stop();
完成();
} 
}

在onPause()方法中执行类似操作以防止崩溃

try{
 mp.stop();
} catch(Exception e) {
}

finish();

它对我起作用了,再也没有撞车了,非常感谢你。