How to prevent concurrent streaming

174 views Asked by At

I have written an app for a radio station, that is working well. If I click to go back to the desktop and click to activate the application again, it causes the app to stream again, resulting in 2 streams running concurrently.

Here is my code;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.setTitle("Standart FM");

    startButton = (Button) findViewById(R.id.buttonBasla);
    facebookButton = (Button) findViewById(R.id.buttonFacebook);
    twitterButton = (Button) findViewById(R.id.buttonTwitter);
    Intent myService = new Intent(this, MyMediaPlayerService.class);
    pd = new ProgressDialog(MainActivity.this);

    mediaPlayer = new MediaPlayer();

    if (isOnline()) {

        pd.setCancelable(false);
        pd.setMessage("Yükleniyor...");
        pd.show();

        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepareAsync();

        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaPlayer
                .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                        pd.dismiss();
                    }
                });
    } else {
        ....
    }

    startButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent myService = new Intent(MainActivity.this,
                    MyMediaPlayerService.class);
            if (mediaPlayer.isPlaying()) {
                startButton.setBackgroundResource(R.drawable.andplaybtn);
                mediaPlayer.stop();
            } else {
                startButton.setBackgroundResource(R.drawable.andstopbtn);

                mediaPlayer.reset();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mediaPlayer.setDataSource(path);
                    mediaPlayer.prepareAsync();
                    pd.setCancelable(false);
                    pd.setMessage("Yükleniyor...");
                    pd.show();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                mediaPlayer
                        .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                mp.start();
                                pd.dismiss();

                            }
                        });
            }
        }
    });

}

 }

How do I solve this problem? How to prevent concurrent streaming in this Activity? Please help me.

1

There are 1 answers

4
JanBo On

You should stop your media player in onPause method, and also say mediaPlayer=null;

@Override
protected void onPause() {
    super.onPause();

    mediaPlayer.stop();
    mediaPlayer=null;
}