There is a fragment Fragment1 . The logic of his work is as follows: From the activity, we transfer data (id, songTitle, songImage, songFile, etc.) into a piece and music is played.

In the fragment there are buttons Next and Prev . Clicking on any of them calls the selectData() method, to which the id next or previous song is selectData() . In the selectData() method, using RxJava is a selection from the database of the desired song by its ID . In the onSuccess method, onSuccess always get information about the desired (next or previous) song and write the data about the song into the variables id, songTitle, songImage, songFile . And this data is used in the startPlayerService method, which starts playback.

But the problem is that when I click Next or Prev in the onSuccess method, onSuccess get info about the desired song, and the information about the previous song gets into the startPlayerService method.

How can this be fixed / solved?

I hope to explain clearly.

Fragragment 1

 public class Fragment1 extends Fragment implements View.OnClickListener { private RoundedImageView imgFairytales; private TextView txtFairytales; private TextView txtAuthor; private AdView mAdView; private ImageButton exo_pause; private ImageButton exo_play; private ImageButton btnNext; private ImageButton btnPrev; private int songsCount = 0; private int id; private int currentID; private String songTitle; private String songImage; private String songFile; private String songAuthor; private String songText; static boolean isPlay = false; private OnDataSelectListener onDataSelectListener; public static Fragment1 newInstance(int id, String songTitle, String songImage, String songFile, String songAuthor, String songText, int songsCount) { Bundle args = new Bundle(); args.putInt("id", id); args.putString("songTitle", songTitle); args.putString("songImage", songImage); args.putString("songFile", songFile); args.putString("songAuthor", songAuthor); args.putString("songText", songText); args.putInt("songsCount", songsCount); Fragment1 fragment1 = new Fragment1(); fragment1.setArguments(args); return fragment1; } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment1, container, false); imgFairytales = rootView.findViewById(R.id.imgFairytales); txtFairytales = rootView.findViewById(R.id.txtFairytales); txtAuthor = rootView.findViewById(R.id.txtAuthor); KidsPlayer.playerView = new PlayerView(getContext()); KidsPlayer.playerView = rootView.findViewById(R.id.player); exo_pause = rootView.findViewById(R.id.exo_pause); exo_play = rootView.findViewById(R.id.exo_play); btnNext = rootView.findViewById(R.id.btnNext); btnPrev = rootView.findViewById(R.id.btnPrev); mAdView = rootView.findViewById(R.id.adView); return rootView; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); id = getArguments().getInt("id", 0); songTitle = getArguments().getString("songTitle"); songImage = getArguments().getString("songImage"); songFile = getArguments().getString("songFile"); songAuthor = getArguments().getString("songAuthor"); songText = getArguments().getString("songText"); songsCount = getArguments().getInt("songsCount"); txtFairytales.setText(songTitle); txtAuthor.setText(songAuthor); Glide.with(getActivity()).load(Uri.parse("file:///android_asset/img/" + songImage + ".jpg")).into(imgFairytales); startPlayerService(); btnPrev.setOnClickListener(this); exo_pause.setOnClickListener(this); exo_play.setOnClickListener(this); btnNext.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnPrev: onPrev(); setScaleAnimation(view); break; case R.id.exo_pause: stopPlayerService(); setScaleAnimation(view); break; case R.id.exo_play: startPlayerService(); setScaleAnimation(view); break; case R.id.btnNext: onNext(); setScaleAnimation(view); break; } } private void setScaleAnimation(View v) { Animation scaleAnimation = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.scale); v.startAnimation(scaleAnimation); } private void startPlayerService() { Intent serviceIntent = new Intent(getActivity(), PlayerService.class); serviceIntent.putExtra(PlayerService.KEY_STREAM, songFile); serviceIntent.putExtra(PlayerService.KEY_TITLE, songTitle); System.out.println("songTitle in startPlayerService = " + songTitle); System.out.println("songFile in startPlayerService = " + songFile); serviceIntent.setAction(PlayerConstants.ACTION.STARTFOREGROUND_ACTION); getActivity().startService(serviceIntent); isPlay = true; if(exo_play.getVisibility() == View.VISIBLE) { exo_play.setVisibility(View.GONE); exo_pause.setVisibility(View.VISIBLE); } } private void stopPlayerService() { Intent serviceIntent = new Intent(getActivity(), PlayerService.class); serviceIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION); getActivity().stopService(serviceIntent); isPlay = false; if(exo_pause.getVisibility() == View.VISIBLE) { exo_pause.setVisibility(View.GONE); exo_play.setVisibility(View.VISIBLE); } } private void onNext() { currentID = id + 1; if (currentID < 1) { btnPrev.setEnabled(false); } else { btnPrev.setEnabled(true); btnNext.setEnabled(true); selectData(currentID); startPlayerService(); } } private void onPrev() { currentID = id - 1; if (currentID > songsCount) { btnNext.setEnabled(false); } else { btnPrev.setEnabled(true); btnNext.setEnabled(true); selectData(currentID); startPlayerService(); } } private void selectData(int idSong) { SongDao songDao = AppDatabase.createPersistentDatabase(getActivity()).songDao(); songDao.getById(idSong).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableMaybeObserver<Song>() { @Override public void onSuccess(Song song) { id = song.getId(); songFile = "file:///android_asset/music/" + song.getFilename() + ".mp3"; songTitle = song.getTitle(); songImage = song.getImage(); songAuthor = song.getAuthor(); songText = song.getText(); songText = songText.replace("\\n", "\n"); txtFairytales.setText(songTitle); txtAuthor.setText(songAuthor); Glide.with(getActivity()).load(Uri.parse("file:///android_asset/img/" + songImage + ".jpg")).into(imgFairytales); onDataSelectListener.onSelectData(songText); currentID = id; System.out.println("id onSuccess = " + id); System.out.println("currentID onSuccess = " + currentID); System.out.println("songTitle onSuccess = " + songTitle); System.out.println("songFile onSuccess = " + songFile); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); System.out.println("id exit onSuccess = " + id); System.out.println("currentID exit onSuccess = " + currentID); System.out.println("songTitle exit onSuccess = " + songTitle); System.out.println("songFile exit onSuccess = " + songFile); } } 
  • In the application there is no possibility to delete the song. BD is ready and cannot be changed, just take data and play audio - Kolhoznik
  • Id +1 so i get the next song. For example, if I got a song from id = 2 from activating, then when I clicked on next, I get currentid = 2 + 1 and play the next song. I have a breakdown in the fact that I cannot get the necessary data to the startPlayerService method, although I get it in onSuccess - April

1 answer 1

The problem is that you immediately after the start of the asynchronous code in selectData () execute startPlayerService (). That is, without waiting for the result, start startPlayerService () with the old data. The easiest solution is to move startPlayerService () to the end of onSuccess ().