passing media player to AsyncTask

0

So following the tutorial below,

link

I arrived at the following% w / w Player that extends Class :

package carcleo.com.radiosingular.classes;

import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.Log;

public class Player extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... strings, MediaPlayer) {
        boolean prepared = false;

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

                }
            });
            mediaPlayer.prepare();
            prepared = true;

        }catch (Exception e){
            Log.e("MyAudioStreamingApp", e.getMessage());
            prepared = false;

        }

        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if(progressDialog.isShowing()) {
            progressDialog.cancell();
        }
        mediaPlayer.start();
        initialStage = false;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pogressDialog.setMessenger("Bufeing...");
        pogressDialog.show();
    }
}

And the AsyncTask primary that imports Activity ,

package carcleo.com.radiosingular;

import android.app.ProgressDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.stream.Stream;

import carcleo.com.radiosingular.classes.Player;

public class principal extends AppCompatActivity {

    private Button btn;
    private boolean playPause;
    private MediaPlayer mediaPlayer;
    private ProgressDialog progressDialog;
    private boolean initialStage = true;
    private String URL= "rtsp://cdn-the-2.musicradio.com:80/LiveAudio/Capital";


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


        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        progressDialog = new ProgressDialog(this);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!playPause) {
                    btn.setText("Pause Streaming");
                    if(initialStage){
                        new Player().execute(URL);
                    }else{
                        if(!mediaPlayer.isLooping()){
                            mediaPlayer.start();
                        }
                    }
                    playPause = true;
                }else {
                    btn.setText("Launcing Steamng");
                    if (mediaPlayer.isPlaying()){
                        mediaPlayer.pause();
                    }
                    playPause = false;
                }

            }
        });


    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mediaPlayer != null){
            mediaPlayer.release();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

}

My difficulty is if the tutorial puts the Player in the same file as AssyncTask and this causes the Activity elements to be available to Activity Player .

How do I pass the object Class to MediaPlayer in case it is not in the same file as AssyncTask ?

    
asked by anonymous 22.12.2018 / 14:18

0 answers