Android - 单击按钮时播放声音 - 空指针异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3369068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:55:22  来源:igfitidea点击:

Android - play sound on button click - Null pointer exception

androidandroid-mediaplayer

提问by Chris

I am trying to play a sound file on the click of a button. The sound is just 1 sec long. It plays well the first few times I click the button, but after a while it gives a NullPointerException. Here's the code:

我试图通过单击按钮播放声音文件。声音只有1秒长。我单击按钮的前几次播放效果很好,但过了一会儿,它给出了一个NullPointerException. 这是代码:

button[i].setOnClickListener(new OnClickListener() {
    public void onClick(View view) {        
        mp = MediaPlayer.create(Test.this, R.raw.mysound);   
        mp.start();
    }
});

And here's the exception:

这是例外:

07-29 23:07:27.690: ERROR/AndroidRuntime(10542): Uncaught handler: thread main exiting due to uncaught exception
07-29 23:07:27.710: ERROR/AndroidRuntime(10542): java.lang.NullPointerException
07-29 23:07:27.710: ERROR/AndroidRuntime(10542):     at com.example.mypackage.Test.onClick(Test.java:270)

回答by Chris

Thanks you for your answers! Appreciate it!

谢谢你的回答!欣赏它!

Here's how I finally managed to get it work:

这是我最终设法让它工作的方式:

            button[i].setOnClickListener(new OnClickListener() {
                public void onClick(View view) {

                    mp = MediaPlayer.create(Test.this, R.raw.mysound);
                    mp.setOnCompletionListener(new OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            // TODO Auto-generated method stub
                            mp.release();
                        }

                    });   
                    mp.start();
                }

            });

回答by Yoav Epstein

You can also try:

您也可以尝试:

final soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
final sound = soundPool.load(this, R.raw.mysound, 1);

button[i].setOnClickListener(new OnClickListener()
{
     public void onClick(View view)
     {       
         soundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
     }
});

回答by sohilv

It might solve your problem,

或许能解决你的问题

button[i].setOnClickListener(new OnClickListener() {
    public void onClick(View view) {     
        new Thread(){
            public void run(){
                mp = MediaPlayer.create(Test.this, R.raw.mysound);   
                 mp.start();
        }.start();
    }
});

回答by Vlad

You should catch Exception.

你应该捕捉异常。

try this code:

试试这个代码:

     try{
         MediaPlayer mplayer = MediaPlayer.create(contextTop, R.raw.<your sound>);
         mplayer.start();
     }catch(Exception e){
         Log.d("<your TAG here>" , "error: " + e);
     }

回答by Shreyash Mahajan

If still your issue not solved then try this as it help me.

如果您的问题仍未解决,请尝试此操作,因为它可以帮助我。

    public void playSound(int resources){
        try{
            boolean mStartPlaying = true;
            MediaPlayer  mPlayer=null;
            if (mStartPlaying==true){
                mPlayer = new MediaPlayer();

                Uri uri = Uri.parse("android.resource://YOUR_PACKAGENAME/" + resources);
                mPlayer.setDataSource(getApplicationContext(),uri);
                mPlayer.prepare();
                mPlayer.start();
            } 
            else{
                mPlayer.release();
                mPlayer = null;
            }
            mStartPlaying = !mStartPlaying;
        }
        catch (IOException e){
            Log.e(LOG_TAG, "prepare() failed");
        }

//       MediaPlayer mp = MediaPlayer.create(SpyMainActivity.this, resources);
//       mp.start();

    }

Enjoy. :)

享受。:)