Hasmukh
Hasmukh

Reputation: 4670

media player issue while continuously click on button?

i am playing mp3 sound using default media player of android, my code for plying sound below

MediaPlayer mp=MediaPlayer.create(hello.this,R.raw.abc);
          if(mp!=null)
          {
              mp.start();
          }

i need to play same sound on same button click. when i click continuously on button after some time sound is not working and i getting error MediaPlayer(7395): error (-19, 0).

Does any body have idea for this issue, Please send me

Thank in advance.

Upvotes: 0

Views: 1552

Answers (5)

Hasmukh
Hasmukh

Reputation: 4670

I have found solution for this issue. i have correct this issue using below code, May help other also.

Thanks

Call Method like

PlaySound(R.raw.abc);

//Method

private void PlaySound(int Sound_id) 
    {
        mplayer = MediaPlayer.create(Act_Oceanwaves.this,Sound_id);
        if(mplayer!=null)
        {           
            mplayer.start();
        }
        mplayer.setOnCompletionListener(new OnCompletionListener() 
        {

            @Override
            public void onCompletion(MediaPlayer mp) 
            {               
                mp.release();
            }

        });   


    }

Upvotes: 0

Ankitkumar Makwana
Ankitkumar Makwana

Reputation: 3485

you can play music like below

 // for play the song

     MediaPlayer mp = new MediaPlayer();

                 try 
                {                   
                     mp.reset();
                     mp.setDataSource(songPath);                        
                     mp.prepare();
                     mp.start();                    
                     btnPlay.setBackgroundResource(R.drawable.img_btn_pause);                   

                 } catch (IOException e) {
                        Log.v(getString(R.string.app_name), e.getMessage());
                }

Upvotes: 0

Manoj Manani
Manoj Manani

Reputation: 507

    mp = new MediaPlayer();
    mp.create(this, R.raw.testmed);
    mp.setVolume(100, 100);
    mp.setOnPreparedListener(this);
    mp.prepare();

Then you will need to define this and it should work:

    public void onPrepared(MediaPlayer player) {
       mp.start();
    }

Upvotes: 0

Khan
Khan

Reputation: 7605

Make global variable of mediaplayer and try this way

         MediaPlayer mp;

          if (mp!=null) {
              mp.stop();
              mp.release();
            }
          mp= MediaPlayer.create(hello.this,R.raw.abc);
          mp.start();

Upvotes: 0

Fawad
Fawad

Reputation: 137

call

mp.reset();

because ever time you are decalaring object

or declare on class level Mediaplayer OBject

Upvotes: 2

Related Questions