Android 应用程序在调用 onPause 时崩溃

发布于 2024-12-08 07:21:57 字数 1003 浏览 0 评论 0原文

你好,我是 Android 开发新手,我正在使用 Eclipse,并且一直在开发一些音板。我的问题是这样的:

public class BernardsActivity extends Activity {

    MediaPlayer eileanBeag;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bernards_layout);

        Button eileanBeagButton = (Button) findViewById(R.id.ButtonB1);

        eileanBeagButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                eileanBeag = MediaPlayer.create(getApplicationContext(), R.raw.eilean_beag);
                eileanBeag.start();
            }    
      });  

    ***public void onPause() {
        eileanBeag.release();                   
    }***    
}

应用程序工作正常,所有音频都播放良好,但是当我使用后退按钮离开应用程序时,它告诉我该过程已意外停止,我必须强制退出。

当我在 onCreate 方法之外定义 MediaPlayer 时,该方法工作正常,但我希望使用许多声音并减少加载时间,我只在按下按钮后才定义它。

我尝试过使用 OnCompleteListener,它似乎确实有效,但它会提前切断我的声音剪辑。

因此,我们将非常感谢对此的任何帮助。

谢谢。

Hello I am newish to android dev, I am using Eclipse and I have been developing some soundboards. my issue is this:

public class BernardsActivity extends Activity {

    MediaPlayer eileanBeag;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bernards_layout);

        Button eileanBeagButton = (Button) findViewById(R.id.ButtonB1);

        eileanBeagButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                eileanBeag = MediaPlayer.create(getApplicationContext(), R.raw.eilean_beag);
                eileanBeag.start();
            }    
      });  

    ***public void onPause() {
        eileanBeag.release();                   
    }***    
}

the app works fine, all audio is playing well, but when I leave the app using the back button, it tells me that the process has stop unexpectedly and I have to force quit.

the method works fine when I define the MediaPlayer outside of the onCreate method, but I wish to use many sounds and to cut down on loading time I only define it once the button has been pushed.

I have tried using an OnCompleteListener and it does seem to work but then it cuts my sound clip off early.

so any help on this would be very much appreciated.

thanks.

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

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

发布评论

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

评论(3

漫漫岁月 2024-12-15 07:21:57

唯一看起来有问题的是,
在调用eileanBeag.release();之前,您应该调用eileanBeag.stop();。当你正在玩的时候你不能直接释放一个Player。您还缺少 super.onPause();

The only thing that looks like a problem is,
before calling eileanBeag.release(); you should call eileanBeag.stop();. You cannot directly release a Player when you are playing. Also you are missing super.onPause();

我是男神闪亮亮 2024-12-15 07:21:57

应该是这样的:

public void onPause() {
    super.onPause();
    eileanBeag.pause();
}

protected void onDestroy() {
    super.onDestroy();
    if(eileanBeag != null) {
        eileanBeag.stop();  
        eileanBeag.release();
    }
}

It should be like this:

public void onPause() {
    super.onPause();
    eileanBeag.pause();
}

protected void onDestroy() {
    super.onDestroy();
    if(eileanBeag != null) {
        eileanBeag.stop();  
        eileanBeag.release();
    }
}
白色秋天 2024-12-15 07:21:57

您需要调用 super.onPause() 作为 onPause 方法内的第一个语句

You need to call super.onPause() as the first statement inside of your onPause method

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