Roland
Roland

Reputation: 866

How to make the Android MediaPlayer faster?

I have this code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btSobre = (Button)findViewById(R.id.btInfo);     
    btPlay = (Button)findViewById(R.id.btPlay);

    playTask = new PlayAsyncTask();
    mediaPlayer = new MediaPlayer();

    btPlay.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
        //playTask.execute();       
            try {
                mediaPlayer.setDataSource("http://************");
            } catch (Exception e) {                 
                e.printStackTrace();
            } 
            try {
                mediaPlayer.prepare();
            } catch (Exception e) {                 
                e.printStackTrace();
            } 

            if(mediaPlayer.isPlaying()){
                mediaPlayer.stop();
                imagemPlay();
            } else {
                mediaPlayer.start();
                imagemPlay();
            }               
        }
    });     

}

//Troca a imagem do botão play. 
private void imagemPlay() {
    if(mediaPlayer.isPlaying()){
        btPlay.setBackgroundResource(android.R.drawable.ic_media_pause);
    } else {
        btPlay.setBackgroundResource(android.R.drawable.ic_media_play);
    }

}

When I ckick to the button to play, it takes some time to play the music. How can i do to get it faster or put some kint of feedback to the user? Like a loader or something else.

Upvotes: 1

Views: 581

Answers (1)

Lucas Arrefelt
Lucas Arrefelt

Reputation: 3929

You prepare it everytime you click play. Try checking if its playing first and then just stop it. If its not playing, do the rest. Try this refactored code and see if it is somewhat faster:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btSobre = (Button)findViewById(R.id.btInfo);     
    btPlay = (Button)findViewById(R.id.btPlay);

    playTask = new PlayAsyncTask();
    mediaPlayer = new MediaPlayer();

    btPlay.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) 
        {

            try 
            {
                imagemPlay();

                if(mediaPlayer.isPlaying())
                {
                    mediaPlayer.stop();
                    return;
                } 

                mediaPlayer.setDataSource("http://************");
                mediaPlayer.prepare();
                mediaPlayer.start();
            } 
            catch (Exception e) 
            {                 
                e.printStackTrace();  
            }
    });     

}

//Troca a imagem do botão play. 
private void imagemPlay() {
    if(mediaPlayer.isPlaying()){
        btPlay.setBackgroundResource(android.R.drawable.ic_media_pause);
    } else {
        btPlay.setBackgroundResource(android.R.drawable.ic_media_play);
    }

}

Upvotes: 1

Related Questions