停止媒体播放器
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在
onDestroy
上使用的mp
引用与您在onCreate
上使用的引用不同。将MediaPlayer mp;
行移至onCreate
类之外。mp
reference that you are using ononDestroy
is different from the one you are using ononCreate
. Move theMediaPlayer mp;
line to outside theonCreate
class.看看这个 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
onDestroy 仅在 Activity 被系统终止时调用。您不应将其放置在 onDestroy 中,而应将其放置在 onPause() 中,每当您的 Activity 移至后台但仍保留在内存中时都会调用该方法。 (这就是按下后退按钮或离开应用程序时发生的情况)
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)
您可以非常轻松地调用覆盖实现源代码并将它们添加到您的代码中。您需要做的就是右键单击所需的插入点,然后单击“源”->“覆盖/实现方法”。它会弹出一个对话框,你点击你需要的方法,尝试使用ondestroy、onpause、onstop。对于您的代码并在实现每个代码后,只需将以下内容添加到每个代码中即可。
另外,如果您想要更多的声音代码,您可以尝试此链接
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.
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