Time limit, asynchronous reproduction

1

I am programming an audio streming app, everything works, the problem is that if one of the url is offline, it stays loading, I would like to set a time limit, for example if in 10 seconds it has not loaded, that stop trying and notify. my code:

private void BotonReproducir() {
        if (block == 0) {
            if (url != "") {
                try {
                    Toast.makeText(getApplicationContext(), "Bufering...", Toast.LENGTH_LONG).show();
                    mediaPlayer.setDataSource(url);
                    fab.setImageResource(R.drawable.ic_media_pause);
                    mediaPlayer.prepareAsync();
                    block = 1;
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer arg0) {
                        //Inicia reproducción.
                        mediaPlayer.start();
                        block = 0;
                        // Lanzo la notificacion creada en el paso anterior
                    }
                });
            } else {
                Toast.makeText(getApplicationContext(), "Select a rario", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(getApplicationContext(), "Espere a que cargue la radio anterior", Toast.LENGTH_SHORT).show();
        }
    }
    
asked by Djdadi43 09.10.2017 в 14:34
source

2 answers

1

In this case there is no possibility to define a timeout but for this you can configure MediaPlayer.OnErrorListener which proposes the documentation, this to detect the error and perform another action.

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {

                //Realiza acción al ocurrir un error.
                Toast.makeText(getApplicationContext(), "No se pudo cargar Multimedia, intente más tarde.", Toast.LENGTH_SHORT).show();

                return false;
            }
        });
  

OnErrorListener :

     

Definition of the callback interface to be invoked when it has occurred   an error during an asynchronous operation in the MediaPlayer.

    
answered by 09.10.2017 / 16:22
source
1

MediaPlayer does not have a default timeout mechanism so you would have to create one.

The method start() puts the object in the state of INITIATED and to know if it has started we can use the method isPlaying() . To emulate the timeout we use Handler#postDelayed to run at 10 seconds and validate if you are playing. If it is not, we show the message:

 try {
        Toast.makeText(getApplicationContext(), "Bufering...", Toast.LENGTH_LONG).show();
        mediaPlayer.setDataSource(url);
        fab.setImageResource(R.drawable.ic_media_pause);
        mediaPlayer.prepareAsync();
        new android.os.Handler().postDelayed(new Runnable(){
            public void run(){
                if(!mediaPlayer.isPlaying())
                {
                 // mostramos el mensaje
                 Toast.makeText(getApplicationContext(), "No se pudo reproducir el audio", Toast.LENGTH_SHORT).show();          
                }
            }
        }, 10000);// 10000 = 10,000 milisegundos = 10 segundos 

//...
    
answered by 09.10.2017 в 15:06