L
L
Lord Drous2016-11-28 15:10:44
Java
Lord Drous, 2016-11-28 15:10:44

How to make sure that when the phone is locked, mediaplayer does not stop working?

Hello, I have a fragment where the radio plays when a button is clicked. If the screen turns off, then the radio turns out too, what options are there?
fragment code

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.LayoutManager mLayoutManager = 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.play1);
        playPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer == null){
                    mediaPlayer = new MediaPlayer();
                    Toast.makeText(getActivity(), "Идет подключение к радиостанции", 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(), "Идет подключение к радиостанции..", 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,"Радио Асса", "104,8 FM");
        itemList.add(item);
    }

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

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivanessence, 2016-11-28
@ivanessence

Probably use the service for uninterrupted playback
https://developer.android.com/guide/components/ser...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question