How to make that when you lock the phone mediaplayer not stopped working?

Hello, I have a fragment that plays the radio, when you press the button. If the screen turns off, the radio turns also, what options are there?
code snippet
public class AsaFragment extends Fragment {
 private static final String LOG_TAG = "myLog";
 private List<item> itemList = new ArrayList<>();
 private RecyclerView recyclerView;
 private ToggleButton playPause;
 private ToggleButton stopButton;
 //private ToggleButton startButton;
 private MyAdapter myAdapter;
 private MediaPlayer mediaPlayer;
 private String STREAM_URL = "http://stream3.radiostyle.ru:8003/radioacca";

 public AsaFragment() {
 // Required empty public constructor
}

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 // Inflate the layout for this fragment

 View View = inflater.inflate(R. layout.fragment_asa, container, false);
 recyclerView = (RecyclerView)view.findViewById(R. id.recycler_view1);
 myAdapter = new MyAdapter(itemList);
//myAdapter.setClickListener(this);
 RecyclerView.MLayoutManager LayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
 recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(myAdapter);
prepareItem();
 playPause = (ToggleButton)view.findViewById(R. id.playpause1);
 stopButton = (ToggleButton)view.findViewById(R. id.stopButton1);
 //startButton = (ToggleButton)view.findViewById(R. id.1);
 playPause.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View View) {
 if (mediaPlayer == null){
 mediaPlayer = new MediaPlayer();
 Toast.makeText(getActivity(), "connecting to radio", Toast.LENGTH_LONG).show();
 try {
mediaPlayer.reset();
mediaPlayer.setDataSource(STREAM_URL);
mediaPlayer.prepareAsync();
 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
 public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
 } catch (IOException e) {
e.printStackTrace();
 Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
}
}
 else if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
 } else {
mediaPlayer.start();
}
 //Toast.makeText(getActivity(), "connecting to radio station..", Toast.LENGTH_LONG).show();
}
});
registerForContextMenu(playPause);
 stopButton.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View View) {
 if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
playPause.setChecked(false);
}
 else {
playPause.setChecked(true);
}
}

});
 playPause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
 public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
 if (compoundButton.getId() == playPause.getId() && b){
playPause.setBackgroundResource(R. drawable.toggle_playback);
}
}
});
registerForContextMenu(stopButton);
 return view;
}
 public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
 playPause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
 public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
 if (compoundButton.getId() == playPause.getId() && b){
playPause.setChecked(true);
}
}
});
}
 public void onStart() {
super.onStart();
 Log.d(LOG_TAG, "Fragment1 onStart");
}

 public void onResume() {
super.onResume();
 Log.d(LOG_TAG, "Fragment1 onResume");
}

 public void onPause() {
super.onPause();
 Log.d(LOG_TAG, "Fragment1 onPause");
}

 public void onStop() {
super.onStop();
if(canStop()){
mediaPlayer.stop();
}
 Log.d(LOG_TAG, "Fragment1 onStop");
}

 public void onDestroyView() {
super.onDestroyView();
 Log.d(LOG_TAG, "Fragment1 onDestroyView");
}

 public void onDestroy() {
super.onDestroy();
 Log.d(LOG_TAG, "Fragment1 onDestroy");
}

 public void onDetach() {
super.onDetach();
 Log.d(LOG_TAG, "Fragment1 onDetach");
}
 private void prepareItem() {
 Item item = new Item(R. drawable.assa,"Radio AssA", "104,8 FM");
itemList.add(item);
}

 boolean canStop()
{
 if(mediaPlayer == null)
{
 return false;
}
 return mediaPlayer.isPlaying();
}

}</item>
July 2nd 19 at 13:43
1 answer
July 2nd 19 at 13:45
Likely to use the service for continuous playback
https://developer.android.com/guide/components/ser...

Find more questions by tags AndroidJava