Implementation of "Loading ..." message in WebPlayer

1

Well, I've left the code more readable ...

The same is a web radio player.

It has functionality but I would like to add a wait / wait message when the video / music takes to load.

Where should I implement this functionality in the code below?

package carcleo.com.player;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;

public class player extends AppCompatActivity {
    private MediaPlayer player;
    private String URL;
    private Button btnPlayPause;
    private Boolean conexao = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        URL = "rtsp://cdn-the-2.musicradio.com:80/LiveAudio/Capital";
        btnPlayPause = (Button) findViewById(R.id.btnPlayPause);

        btnPlayPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (player == null) player = new MediaPlayer();
                    tocaPausa();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

private void createConexao() throws IOException {
    String url = "rtsp://cdn-the-2.musicradio.com:80/LiveAudio/Capital"; // your URL here
    player = new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setDataSource(url);
    player.prepare(); // might take long! (for buffering, etc)
    btnPlayPause.setText("AGUARDE...");
    player.start();
    btnPlayPause.setText("PAUSAR");
    conexao = true;
}

    private void tocaPausa() throws IOException {
        if (conexao == true) {
            if (!player.isPlaying()) {
                player.start();
                btnPlayPause.setText("PAUSAR");
            } else {
                player.pause();
                btnPlayPause.setText("TOCAR");
            }
        } else {
            createConexao();
        }
    }

}
    
asked by anonymous 24.12.2018 / 17:52

1 answer

0

I did it. But unfortunately using AsyncTask.

package carcleo.com.player;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;

public class player extends AppCompatActivity {
    private MediaPlayer player;
    private String URL;
    private Button btnPlayPause;
    private Boolean conexao = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        btnPlayPause = (Button) findViewById(R.id.btnPlayPause);

        btnPlayPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (player == null) player = new MediaPlayer();
                    tocaPausa();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void tocaPausa() throws IOException {
        if (conexao == true) {
            if (!player.isPlaying()) {
                player.start();
                btnPlayPause.setText("PAUSAR");
            } else {
                player.pause();
                btnPlayPause.setText("TOCAR");
            }
        } else {
            String url = "rtsp://cdn-the-2.musicradio.com:80/LiveAudio/Capital"; // your URL here
            new Play().execute(url);
        }
    }

    class Play extends AsyncTask<String, Boolean, Boolean> {

        @Override
        protected void onPreExecute() {

            btnPlayPause.setText("AGUARDE...");

        }

        @Override
        protected void onPostExecute(Boolean result) {

            if(result == true){
                conexao = true;
                player.start();
                btnPlayPause.setText("PAUSAR");
            } else {
                conexao = false;
                btnPlayPause.setText("TOCAR");
            }

        }

        @Override
        protected Boolean doInBackground(String... params) {

            try {
                player = new MediaPlayer();
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.setDataSource(params[0]);
                player.prepare(); // might take long! (for buffering, etc)
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }

        }

    }

}

If someone knows how to do it without using AsyncTask or how not to use it in the main class, I'll be grateful.

    
24.12.2018 / 21:06