Java 如何在Android中停止活动?

Java 如何在Android中停止活动?,java,android,media-player,Java,Android,Media Player,我创建了一个包含媒体播放器的活动。当我启动活动时,音乐开始播放。但当歌曲完成并单击模拟器上的“上一步”按钮时,它会显示错误(illegelstateException) 这是我的密码 提前谢谢 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audio); init(); imgVw.setI

我创建了一个包含媒体播放器的活动。当我启动活动时,音乐开始播放。但当歌曲完成并单击模拟器上的“上一步”按钮时,它会显示错误(
illegelstateException

这是我的密码

提前谢谢

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.audio);
    init();
    imgVw.setImageResource(R.raw.teddy_two);

    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    mp=MediaPlayer.create(Audio_Activity.this,R.raw.ennamo_yadho);
    Log.e("Song is playing","in  Mediya Player ");
    Log.e("Current ","Position -> " + length);
    mp.setLooping(false);
    mp.start();
    btnChapter.setEnabled(false);

    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
    {
        @Override
        public void onCompletion(MediaPlayer mp) 
        {
            // TODO Auto-generated method stub
            mp.stop();
            mp.release();
            btnChapter.setEnabled(true);
            System.out.println("Music is over and Button is enable !!!!!!");
        }
    });

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPause()
    {
        super.onStop();
        SharedPreferences. Editor prefsEdit = prefs.edit();

        int position = mp.getCurrentPosition();
        prefsEdit.putInt("mediaPosition", position);
        prefsEdit.commit();
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        System.out.println("Activity is Resume !!!");
        int position = prefs.getInt("mediaPosition", 0);
        mp.seekTo(position);
        mp.start();
    }

    #Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) 
        { 
            if(mp!= null)
            {
                mp.pause();
            }
            //finish();
         return true;
     }
     return super.onKeyDown(keyCode, event);
 }
}

if((keyCode==KeyEvent.keyCode\u BACK))
中的上述代码片段似乎就是问题所在。你应该检查球员是否在比赛。如果正在播放,则只应调用暂停


if((keyCode==KeyEvent.keyCode\u BACK))
中的上述代码片段似乎就是问题所在。你应该检查球员是否在比赛。如果正在播放,则只应调用暂停。

我认为问题在于:

@Override
public void onPause()
{
   super.onPause();//should be onPause and not onStop

   SharedPreferences. Editor prefsEdit = prefs.edit();

   int position = mp.getCurrentPosition();
   prefsEdit.putInt("mediaPosition", position);
   prefsEdit.commit();
}

欲了解更多信息,请阅读。

我认为问题在于:

@Override
public void onPause()
{
   super.onPause();//should be onPause and not onStop

   SharedPreferences. Editor prefsEdit = prefs.edit();

   int position = mp.getCurrentPosition();
   prefsEdit.putInt("mediaPosition", position);
   prefsEdit.commit();
}

欲了解更多信息,请阅读。

以下是onStop()的一个实现,它将草稿注释的内容保存到永久存储:

@Override

protected void onStop() {
    super.onStop();  // Always call the superclass method first

    // Save the note's current draft, because the activity is stopping
    // and we want to be sure the current note progress isn't lost.
    ContentValues values = new ContentValues();
    values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
    values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());

    getContentResolver().update(
            mUri,    // The URI for the note to update.
            values,  // The map of column names and new values to apply to them.
            null,    // No SELECT criteria are used.
            null     // No WHERE columns are used.
            );
}
虽然在
onStop()
之前调用了
onPause()
方法,但是您应该使用
onStop()
执行更大、更CPU密集的关闭操作,例如将信息写入数据库


从android开发者页面:

这里是onStop()的一个实现,它将草稿笔记的内容保存到持久存储中:

@Override

protected void onStop() {
    super.onStop();  // Always call the superclass method first

    // Save the note's current draft, because the activity is stopping
    // and we want to be sure the current note progress isn't lost.
    ContentValues values = new ContentValues();
    values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
    values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());

    getContentResolver().update(
            mUri,    // The URI for the note to update.
            values,  // The map of column names and new values to apply to them.
            null,    // No SELECT criteria are used.
            null     // No WHERE columns are used.
            );
}
虽然在
onStop()
之前调用了
onPause()
方法,但是您应该使用
onStop()
执行更大、更CPU密集的关闭操作,例如将信息写入数据库

从android开发者页面:

使用
finish()活动类中编码>以停止活动并返回使用
finish()活动类中编写>以停止活动并返回请尝试以下操作

MediaPlayer player;


public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "service started", 100).show();
    if(player.isPlaying())
        player.stop();

    else
        player.start();
    return super.onStartCommand(intent, flags, startId);}
试试这个

MediaPlayer player;


public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "service started", 100).show();
    if(player.isPlaying())
        player.stop();

    else
        player.start();
    return super.onStartCommand(intent, flags, startId);}
onPause()上可以看到(IllegelStateException)

要解决这个问题,您需要

mp = null;
之后,

mp.stop();
mp.release();
因为当你检查mp=空值当时MediaPlayer已发布,但未发布空值。 所以,如果条件正确,转到mp.pause()mp已经释放,这就是为什么会出现错误

您还需要在onCompletion()中更改MediaPlayer实例的名称

public void onCompletion(MediaPlayer mp)
因为它在onCompletion()的mp中用于stop()release()。 因此,将mp更改为other(如:mp1)

public void onCompletion(MediaPlayer mp)
检查此选项是否对您有帮助。

您将在onPause()上获得(IllegelStateException)

要解决这个问题,您需要

mp = null;
之后,

mp.stop();
mp.release();
因为当你检查mp=空值当时MediaPlayer已发布,但未发布空值
。 所以,如果条件正确,转到mp.pause()mp已经释放,这就是为什么会出现错误

您还需要在onCompletion()中更改MediaPlayer实例的名称

public void onCompletion(MediaPlayer mp)
因为它在onCompletion()的mp中用于stop()release()。 因此,将mp更改为other(如:mp1)

public void onCompletion(MediaPlayer mp)

检查这是否对您有帮助。

这些答案都不够好吗?这些答案都不够好吗?