How to pause an audio and play another one in the same app

0

I have combined two projects, an online radio and an online music player. But when playing the radio and then the music on line, they sound at the same time. I need help on how I can make that when playing one, the other pause stops. I was inquiring about Managing Audio Focus but I do not know if it is the right thing or if you can give me a tip to use the same service for the two projects. Thank you in advance.
This is the MusicPLayActivity.java code of the music player code online.

btnPlay.setOnClickListener(new View.OnClickListener() {
@Override 
        public void onClick(View arg0) {

            // check for already playing
            if(mp.isPlaying()){
                if(mp!=null){
                    mp.pause();
                    // Changing button image to play button
                    btnPlay.setImageResource(R.drawable.btn_play);

                }
            }else{

                // Resume song
                if(mp!=null){
                    mp.start();

                    // Changing button image to pause button
                    btnPlay.setImageResource(R.drawable.btn_paush);
                }
            }

        }
    });

This is the code of the online radio: FragmentRadio.java

 public void onClick(View view) {
    if (view == buttonPlay) {

        if (urlToPlay != null) {
            startPlaying();

            buttonPlay.setVisibility(View.GONE);
            buttonStopPlay.setVisibility(View.VISIBLE);

            AudioManager am = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
            int volume_level = am.getStreamVolume(AudioManager.STREAM_MUSIC);
            if (volume_level < 1) {
                Toast.makeText(activity, getResources().getString(R.string.volume_low), Toast.LENGTH_SHORT).show();
            }

            if (counter == Config.INTERSTITIAL_COUNTER_NUMBER) {
                interstitialAd = new InterstitialAd(getActivity());
                interstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
                AdRequest adRequest = new AdRequest.Builder().build();
                interstitialAd.loadAd(adRequest);
                interstitialAd.setAdListener(new AdListener() {
                    public void onAdLoaded() {
                        if (interstitialAd.isLoaded()) {
                            interstitialAd.show();
                        }
                    }
                });
                counter = 1;
            } else {
                counter++;
            }


        } else {
            Log.d("INFO", "The loading of urlToPlay should happen almost instantly, so this code should never be reached");
        }

    } else if (view == buttonStopPlay) {
        stopPlaying();

        buttonPlay.setVisibility(View.VISIBLE);
        buttonStopPlay.setVisibility(View.GONE);
    }
}

private void startPlaying() {

    progressBar.setVisibility(View.VISIBLE);
    textView.setVisibility(View.VISIBLE);
    radioManager.startRadio(urlToPlay);
    radioManager.updateNotification(
            activity.getResources().getString(R.string.notification_title),
            activity.getResources().getString(R.string.notification_subtitle),
            R.mipmap.ic_launcher,
            BitmapFactory.decodeResource(activity.getResources(), R.mipmap.ic_launcher)
    );
    updateButtons();
}

private void stopPlaying() {

    radioManager.stopRadio();
    progressBar.setVisibility(View.INVISIBLE);
    textView.setVisibility(View.INVISIBLE);
    updateButtons();
    if (runningOnOldConnection) {
        resetRadioManager();
    }
}

The latter uses the mobiwise library link

I do not know if this data is sufficient. Thanks.

    
asked by Edwin Ramirez 18.04.2017 в 22:30
source

0 answers