Здравствуйте, у меня есть 2 фрагмента MainFragment(1) и GalleryFragment(2), во 2 фрагменте у меня медиаплеер, кнопочка play запускает вещание, после чего нажимаю домой, но вещание не останавливается. Как сделать чтобы останавливалось при переходе на другой фрагмент?
код GalleryFragment. не до конца:
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.isPlaying()) {
mediaPlayer.stop();
mediaPlayer = null;
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);
}
}
});
} */
private void prepareItem() {
Item item = new Item(R.drawable.assa,"Радио Асса", "104,8 FM");
itemList.add(item);
}
MainFragment:
public class MainFragment extends AsaFragment {
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onDestroyView(){
super.onDestroyView();
stopRadio();
}
private void stopRadio() {
}
}