Get the percentage of Web Streaming upload

0

I have the following code below,

private void contaBuffer () {

    textView2.setText(Integer.toString(mPlayer.getDuration()));
    mPlayer.setOnBufferingUpdateListener(
            new MediaPlayer.OnBufferingUpdateListener() {
                public void onBufferingUpdate(MediaPlayer mp, int percent)
                {
                    double ratio = percent / 100.0;
                    int bufferingLevel = (int)(mp.getDuration() * ratio);

                    sb.setSecondaryProgress(bufferingLevel);
                    textView2.setText(Integer.toString(bufferingLevel));
                }

            }

    );

}

Retired from:

link

Through an explanation of

link

But when I do

textView2.setText(Integer.toString(mPlayer.getDuration()));

I get the negative value below:

-1095246304

My goal with this code is to put within upload % of streaming strong> web radio the first time you load it.

Where am I going wrong?

Follow the complete code.

package carcleo.com.player;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
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.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.SeekBar;
import android.widget.TextView;
import java.io.IOException;

public class player extends AppCompatActivity implements OnBufferingUpdateListener{

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

    @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);
        textView2 = findViewById(R.id.textView2);

        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 contaBuffer () {

        textView2.setText(Integer.toString(mPlayer.getDuration()));
        mPlayer.setOnBufferingUpdateListener(
                new MediaPlayer.OnBufferingUpdateListener() {
                    public void onBufferingUpdate(MediaPlayer mp, int percent)
                    {
                        double ratio = percent / 100.0;
                        int bufferingLevel = (int)(mp.getDuration() * ratio);

                        sb.setSecondaryProgress(bufferingLevel);
                        textView2.setText(Integer.toString(bufferingLevel));
                    }

                }

        );

    }

    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);
        }
    }


    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 (){

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.home)
                        .setContentTitle("Rádio Capital")
                        .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());

    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        textView2.setText(Integer.toString(percent));
    }

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

        @Override
        protected void onPreExecute() {

            btnPlayPause.setBackgroundResource(R.drawable.carregando);
            btnPlayPause.setEnabled(false);
            contaBuffer();

        }

        @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(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 28.12.2018 / 15:26

0 answers