0

I'm new in android development and I really can't figure out how to do that. So, I'm building an android app that stream music online via url. There is an url that shows current song. here is the url

I created a textView in my activity_main file to put current song text inside it, but I can't figure out how. I understand that I need to make variable url, then I need to parse that URL and put it inside text view. Or not?

Here is my MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private TextView currentSong;
    private boolean playPause;
    private MediaPlayer mediaPlayer;
    private boolean initialStage = true;
    private ProgressBar pgsBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.audioStreamBtn);
        currentSong = (TextView) findViewById(R.id.currentSong);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        pgsBar = findViewById(R.id.progressBar);
        pgsBar.setVisibility(View.GONE);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!playPause) {
                    btn.setText("Stop");

                    if (initialStage) {
                        new Player().execute("stream url ...");
                    } else {
                        if (!mediaPlayer.isPlaying())
                            mediaPlayer.start();
                    }

                    playPause = true;

                } else {
                    btn.setText("Play");

                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                    }

                    playPause = false;
                }
            }
        });
    }

    class Player extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... strings) {
            Boolean prepared = false;

            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        initialStage = true;
                        playPause = false;
                        btn.setText("Play");
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                    }
                });

                mediaPlayer.prepare();
                prepared = true;

            } catch (Exception e) {
                prepared = false;
            }

            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            if (pgsBar.getVisibility() == View.VISIBLE) {
                pgsBar.setVisibility(View.GONE);
                btn.setEnabled(true);
            }

            mediaPlayer.start();
            initialStage = true;
        }

        @Override
        protected void onPreExecute() {
            mediaPlayer.reset();
            super.onPreExecute();
            pgsBar.setVisibility(View.VISIBLE);
            btn.setEnabled(false);
        }
    }
}

How do I pull out this text and put it in my textView "currentSong"?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I believe that you are working on a music app to stream music online please take a look at this project might help you a lot https://github.com/hpkaushik121/MusicApp – sourabh kaushik Nov 15 '19 at 12:33

2 Answers2

0

You can use okHttp library to make a network request to URL and it will get back the response as string. Modify the response according to your liking and use that value for setText() of text view .

Pratyush
  • 401
  • 3
  • 11
0

Use Retrofit or Volley to make a URL request.

possible duplicate of Make an HTTP request with android

Abhay Paul
  • 31
  • 4