停止媒体播放器

发布于 2024-11-30 23:33:35 字数 1244 浏览 1 评论 0原文

我是 Android 新手,我还有另一个(简单?)问题。我不知道如何停止媒体播放器。这是我的简单代码:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        MediaPlayer mp;
        mp = MediaPlayer.create(this, R.raw.sauronsound);
        mp.setLooping(false);
        mp.start();
    @Override
    protected void onDestroy()
{

    // Stop play
        super.onDestroy();
        mp.stop();
    }
        }

按后退按钮后,应用程序转到我的第一个活动,但声音打开。当我离开应用程序时,它也会打开。我应该怎么做才能关闭声音?

一如既往,请原谅我糟糕的英语。

谢谢你们,我解决了这个问题。工作代码:

public class SauronEye extends Activity {
    private MediaPlayer mp;
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        mp = MediaPlayer.create(this, R.raw.sound);
        mp.setLooping(false);
        mp.start();


     // Get instance of Vibrator from current Context
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(10000);
        }
@Override
    protected void onStop()
{
    // Stop play
    super.onStop();
    mp.stop();
}
    }

它是否正确(有效)?谢谢你帮助我。

I am new in android and I have another (simple?) problem. I don't know how to stop Media Player. This is my simple code:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        MediaPlayer mp;
        mp = MediaPlayer.create(this, R.raw.sauronsound);
        mp.setLooping(false);
        mp.start();
    @Override
    protected void onDestroy()
{

    // Stop play
        super.onDestroy();
        mp.stop();
    }
        }

After pressing back button app goes to my first activity but sound is on. When I leave an app it is on too. What should I do to turn off the sound?

As always excuse me for my poor English.

I solved the problem thanks to you Guys. Working code:

public class SauronEye extends Activity {
    private MediaPlayer mp;
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        mp = MediaPlayer.create(this, R.raw.sound);
        mp.setLooping(false);
        mp.start();


     // Get instance of Vibrator from current Context
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(10000);
        }
@Override
    protected void onStop()
{
    // Stop play
    super.onStop();
    mp.stop();
}
    }

Is it correct (it works)? Thank you for helping me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萝莉病 2024-12-07 23:33:35

您在 onDestroy 上使用的 mp 引用与您在 onCreate 上使用的引用不同。将 MediaPlayer mp; 行移至 onCreate 类之外。

mp reference that you are using on onDestroy is different from the one you are using on onCreate. Move the MediaPlayer mp; line to outside the onCreate class.

你不是我要的菜∠ 2024-12-07 23:33:35

看看这个 http://developer.android.com/reference/android/media/ MediaPlayer.html
您可以根据您的要求调用停止或暂停。当您选择后退按钮时,您的 onpause 将被调用,在该方法中您可以调用 mp.stop(),只有当 Activity 完全销毁时才会调用 onDestroy

Check this out http://developer.android.com/reference/android/media/MediaPlayer.html
You can call stop or pause based on your requirement.When you select back button your onpause would be called, in that method you can call mp.stop(), onDestroy would be called only when activity is completely destroyed

遗弃M 2024-12-07 23:33:35

onDestroy 仅在 Activity 被系统终止时调用。您不应将其放置在 onDestroy 中,而应将其放置在 onPause() 中,每当您的 Activity 移至后台但仍保留在内存中时都会调用该方法。 (这就是按下后退按钮或离开应用程序时发生的情况)

@Override
protected void onPause() {
    super.onPause();
    mp.stop();
}

onDestroy is only called when the activity is killed by the system. Rather than placing it in onDestroy, you should put it in onPause(), which is what's called whenever your activity is moved to the background but remains in memory. (Which is what happens with a back button being pressed or leaving the app)

@Override
protected void onPause() {
    super.onPause();
    mp.stop();
}
眼眸 2024-12-07 23:33:35

您可以非常轻松地调用覆盖实现源代码并将它们添加到您的代码中。您需要做的就是右键单击所需的插入点,然后单击“源”->“覆盖/实现方法”。它会弹出一个对话框,你点击你需要的方法,尝试使用ondestroy、onpause、onstop。对于您的代码并在实现每个代码后,只需将以下内容添加到每个代码中即可。

    protected void onDestroy{
      super.onDestroy();
      mp.release();
   }

    protected void onStop{
      super.onStop();
      mp.stop();
   }

    protected void onPause{
      super.onPause();
      mp.pause();
   }

另外,如果您想要更多的声音代码,您可以尝试此链接
stealthcopters 链接 或者您可以尝试这个视频系列
cornboyzAndroid

you can call the override implements source codes really easily and add them each into your code. All you need to do is right click the insertion point where you want them and click on Source->Override/Implement Methods. It will bring up a dialog box and you click on the methods you need, try using ondestroy, onpause, onstop. For your code and after it implements each of them just add the following to each.

    protected void onDestroy{
      super.onDestroy();
      mp.release();
   }

    protected void onStop{
      super.onStop();
      mp.stop();
   }

    protected void onPause{
      super.onPause();
      mp.pause();
   }

Also if you want a little more with you soundcodes you can try this link
stealthcopters link or you can try this video series
cornboyzAndroid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文