Using Seekbar in a simple way

Asked

Viewed 1,060 times

1

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

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

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

How do I do that?

Good, my SeekBar is 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’ve searched the list of andoids: but I can’t find any property atrele aSeekbaraoMediaplayer`

Look what I got:

Obs.:

THERE IS NO MISTAKE.

Just does not control the audio. That is, you can move 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);
            }

        }

    }

}

1 answer

1

To control audio from your device use the class Audiomanager.

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.

Browser other questions tagged

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