Implementation of "Loading..." message in Webplayer

Asked

Viewed 35 times

1

Well, I made the code more readable...

The same is true of a web radio player.

It has functionality however I would like to add a message waiting / wait, when the video/music takes long to load.

Where I should 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();
        }
    }

}

1 answer

0

I did. 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 else how not to use it in the main class, I will be grateful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.