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

时间:2010-07-30 06:14:53

标签: android android-mediaplayer

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

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

这是例外:

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$3.onClick(Test.java:270)

5 个答案:

答案 0 :(得分:101)

感谢您的回答!欣赏它!

以下是我最终成功实现的方法:

            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();
                }

            });

答案 1 :(得分:24)

您也可以尝试:

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);
     }
});

答案 2 :(得分:4)

它可能会解决您的问题,

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();
    }
});

答案 3 :(得分:1)

你应该抓住异常。

试试这段代码:

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

答案 4 :(得分:0)

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

    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();

    }

享受。 :)