How to Monitor Streaming Upload in MediaPlayer

-1

So, I'm creating the media player. Everything works. Now I would like to implement the streaming upload progress counter.

I'll post my whole code for you to take a look at.

Note that even if you implement OnBufferingUpdateListener in class Player , the code below does not work:

@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
    progressBar.setProgress(percent);
}

Where am I going wrong?

Follow the whole code!

package carcleo.com.player;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.session.MediaSession;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.Locale;

public class player extends AppCompatActivity implements OnBufferingUpdateListener{

    private MediaPlayer mPlayer;
    private String URL;
    private Button btnPlayPause;
    private Boolean conexao = false;
    private ProgressDialog progressDialog;
    private SeekBar sb;
    private TextView textView;
    private NotificationManager mNotificationManager;
    private AudioManager audioManager;
    private ProgressBar progressBar;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
        sb = findViewById(R.id.seekBar);
        textView = findViewById(R.id.textView);

        progressDialog = new ProgressDialog(this);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        btnPlayPause = (Button) findViewById(R.id.btnPlayPause);

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

    }

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

    @Override
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
        progressBar.setProgress(percent);
    }

    private void configuraAudioManager() {
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int volume    = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        sb.setMax(maxVolume);
        sb.setProgress(volume);

        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_SHOW_UI);

                Double total = progress * 6.666666666666667;
                String valor =Integer.toString(Integer.valueOf(total.intValue()));
                textView.setText(valor+" %");

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void notificacao (){

        RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.player);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.home)
                        .setContentTitle("Rádio Capital")
                        //.setCustomContentView(remoteViews)
                        .setContentText("Agora deu");

        Intent resultIntent = new Intent(this, player.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(player.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

    }

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

        @Override
        protected void onPreExecute() {

            btnPlayPause.setBackgroundResource(R.drawable.carregando);
            btnPlayPause.setEnabled(false);
            progressDialog.setMessage("Carregando...");
            //progressDialog.show();

        }

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

            try {
                mPlayer = new MediaPlayer();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mPlayer.setDataSource(params[0]);

                /*
                mPlayer.setOnBufferingUpdateListener(
                        (mediaPlayer, percent) ->
                                textView.setText(String.format(Locale.getDefault(), "Buffered to %d%%", percent)));
                )
                */

                mPlayer.prepare(); // might take long! (for buffering, etc)
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }

        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        protected void onPostExecute(Boolean result) {

            if (progressDialog.isShowing()) {
                progressDialog.cancel();
            }

            if(result == true){
                conexao = true;
                mPlayer.start();
                btnPlayPause.setBackgroundResource(R.drawable.pause);
            } else {
                conexao = false;
                btnPlayPause.setBackgroundResource(R.drawable.play);
            }

            btnPlayPause.setEnabled(true);

            notificacao();

        }

    }

}
    
asked by anonymous 27.12.2018 / 20:29

0 answers