Android - play sound on button click - Null pointer exception

前端 未结 5 1573
别那么骄傲
别那么骄傲 2020-11-28 22:15

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 <

相关标签:
5条回答
  • 2020-11-28 22:57

    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. :)

    0 讨论(0)
  • 2020-11-28 23:05

    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();
        }
    });
    
    0 讨论(0)
  • 2020-11-28 23:07

    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();
                    }
    
                });
    
    0 讨论(0)
  • 2020-11-28 23:15

    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);
         }
    
    0 讨论(0)
  • 2020-11-28 23:19

    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);
         }
    });
    
    0 讨论(0)
提交回复
热议问题