Mediaplayer in service for Radio Streaming app

1

I made an app for an online radio in a town, I would like to know if you can help me to play in the background in a service, so that I tell you to kill the main activity, the radio keeps listening.

I share code with you:

package ar.com.asset.www.radiostream;

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

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

Button btnplay;
Button btnstop;
MediaPlayer mediaPlayer;

boolean prepared = false;
boolean started = false;

String stream = "http://radioscoop.hu:80/live.mp3";

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

    btnplay = (Button) findViewById(R.id.btnplay);
    btnstop = (Button) findViewById(R.id.btnstop);
    btnplay.setEnabled(false);
    btnplay.setText("CARGANDO");
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    new PlayerTask().execute(stream);

    btnplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (started) {
                started = false;
                mediaPlayer.pause();
                btnplay.setText("Play");
            } else {
                started = true;
                prepared = true;
                mediaPlayer.start();
                btnplay.setText("Pause");
            }
        }
    });

    btnstop.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {
            started = false;
            mediaPlayer.stop();
            btnplay.setText("Play");

            try {
                mediaPlayer.prepare();
                prepared = true;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    });
}


class PlayerTask extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... strings) {
        try {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.prepare();
            prepared = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        btnplay.setEnabled(true);
        btnplay.setText("PLAY");
    }

}

@Override
protected void onPause() {
    super.onPause();

}

@Override
protected void onResume() {
    super.onResume();

}

@Override
protected void onDestroy() {
    super.onDestroy();

}

}

    
asked by Alvaro Deulofeu 25.03.2017 в 19:58
source

1 answer

0

When I tried to do this I found the problem of making the reproduction in the background, also Google Playstore by policy does not allow playback in the background, as an option is to always keep the screen visible, which allows the reproduction without problem:

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
answered by 28.03.2017 в 18:57