Using SeekBar in a simple way

1

I am, for study purposes, developing a player in Android Studio .

The player is ready. Now I want to control it.

Searching, I discovered the SeekBar component. But I'm not finding a way to tell her that the source of the sound is MediaPlayer that's playing.

How do I do this?

Well, my SeekBar looks like this:

<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="114dp"
    android:layout_marginLeft="114dp"
    android:layout_marginTop="163dp"
    android:max="100" />

I have already searched the list of andoids: but I do not find any property atrele a SeekBar ao MediaPlayer '

Look what I got:

Note:

DOES NOT GIVE ANY ERROR .

It just does not control the audio. That is, you can walk with the ball to the right or to the left that the volume does not change!

package carcleo.com.player;

import android.app.ProgressDialog;
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 android.widget.SeekBar;
import android.widget.Toast;

import java.io.IOException;

public class player extends AppCompatActivity {

    private MediaPlayer player;
    private String URL;
    private Button btnPlayPause;
    private Boolean conexao = false;
    private ProgressDialog progressDialog;
    private SeekBar sb;

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

        sb = (SeekBar) findViewById(R.id.seekBar);
        sb();

        progressDialog = new ProgressDialog(this);

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

        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            int progress = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {

                progress = progresValue;

                Toast.makeText(getApplicationContext(), "Changing seekbar's progress", Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onStartTrackingTouch(SeekBar seekBar) {

                Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {

                //textView.setText("Covered: " + progress + "/" + seekBar.getMax());

                Toast.makeText(getApplicationContext(), "Stopped tracking seekbar", Toast.LENGTH_SHORT).show();

            }

        });

    }


    private void tocaPausa() throws IOException {
        if (conexao == true) {
            if (!player.isPlaying()) {
                player.start();
                btnPlayPause.setBackgroundResource(R.drawable.pause);
            } else {
                player.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);
        }
    }

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

        @Override
        protected void onPreExecute() {

            progressDialog.setMessage("Carregando...");
            progressDialog.show();

        }

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

        }

        @Override
        protected void onPostExecute(Boolean result) {

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

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

        }

    }

}
    
asked by anonymous 25.12.2018 / 15:13

1 answer

1

To control audio on your device, use the AudioManager class.

private AudioManager audioManager;

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

        //Recupera os valores de volume máximo e o volume atual
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        //Configura os valores máximo para o seekBar
        seekVolume.setMax(maxVolume);

        //Configura o progresso atual do seekBar
        seekVolume.setProgress(volume);

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

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

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

The above method makes the necessary settings. Then just call this method on onCreate .

    
27.12.2018 / 02:00