title: play radiostream in the background
body: <p>Hello i'm building a app for a radio station. Now i can the music in the app but when i go to the homescreen the audio stops. Does someone have some tips suggestions on how to use my code to keep on the audio playing in the background?</p>
  
  <p>My code:</p>
  
  <pre><code>package com.wemait.rtvhardenberg;
  
  import java.io.BufferedOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  
  import com.wemait.rtvhardenberg.Nieuws.GetJSONData;
  import com.wemait.rtvhardenberg.helper.AlertDialogManager;
  import com.wemait.rtvhardenberg.helper.BackgroundSoundService;
  import com.wemait.rtvhardenberg.helper.ConnectionDetector;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.graphics.drawable.AnimationDrawable;
  import android.media.MediaPlayer;
  import android.media.MediaPlayer.OnBufferingUpdateListener;
  import android.media.MediaPlayer.OnPreparedListener;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.ProgressBar;
  
  public class Muziek extends Activity implements OnClickListener {
  
      private final static String RADIO_STATION_URL = "http://stream.intronic.nl/rtvhardenberg";
  
      private ProgressBar playSeekBar;
  
      private Button buttonPlay;
  
      private Button buttonStopPlay;
  
      private MediaPlayer player;
  
      /**
       * Reference to the ImageView which will display the animation.
       */
      ImageView animation;
  
      // Connection detector
      ConnectionDetector cd;
  
      // Alert dialog manager
      AlertDialogManager alert = new AlertDialogManager();
  
      // Progress Dialog
      private ProgressDialog pDialog;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.muziek);
  
          cd = new ConnectionDetector(getApplicationContext());
  
          // Check for internet connection
          if (!cd.isConnectingToInternet()) {
              // Internet Connection is not present
              alert.showAlertDialog(Muziek.this, "Internet Connectie Error", "Zorg voor een werkende internet connectie", false);
              // stop executing code by return
              return;
          }
  
          animation = (ImageView)findViewById(R.id.imageAnimation);
  
          animation.setBackgroundResource(R.drawable.animation);
  
          //Intent svc=new Intent(this, BackgroundSoundService.class);
          //startService(svc);
  
          initializeUIElements();
  
          initializeMediaPlayer();
      }
  
      private void initializeUIElements() {
  
          playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
          playSeekBar.setMax(100);
          playSeekBar.setVisibility(View.INVISIBLE);
  
  
          animation.setVisibility(View.INVISIBLE);
  
          buttonPlay = (Button) findViewById(R.id.button_play);
          buttonPlay.setOnClickListener(this);
  
          buttonStopPlay = (Button) findViewById(R.id.button_stop);
          buttonStopPlay.setEnabled(false);
          buttonStopPlay.setOnClickListener(this);
      }
  
      public void onClick(View v) {
          if (v == buttonPlay) {
              startPlaying();
          } else if (v == buttonStopPlay) {
              stopPlaying();
          }
      }
  
      private void startPlaying() {
          //I tried this but didn't work thats why i've commented it away
          //new BackgroundSound().execute();
          //Intent svc=new Intent(this, BackgroundSoundService.class);
         // startService(svc);
          pDialog = new ProgressDialog(Muziek.this);
              pDialog.setMessage("Live radio laden ...");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(false);
              pDialog.show(); 
  
          buttonStopPlay.setEnabled(true);
          buttonPlay.setEnabled(false);
  
  
          playSeekBar.setVisibility(View.INVISIBLE); // plaatje
  
          AnimationDrawable frameAnimation = 
                  (AnimationDrawable) animation.getBackground();
          animation.setVisibility(View.INVISIBLE);
          frameAnimation.start();
  
          player.prepareAsync();
          frameAnimation.start();
          player.setOnPreparedListener(new OnPreparedListener() {
              public void onPrepared(MediaPlayer mp) {
                  player.start();
                 // buttonRecord.setEnabled(true);
                  //new BackgroundSound().execute();
  
              }
          });
      }
  
      private void stopPlaying() {
          if (player.isPlaying()) {
              player.stop();
              player.release();
              initializeMediaPlayer();
          }
  
          buttonPlay.setEnabled(true);
          buttonStopPlay.setEnabled(false);
          playSeekBar.setVisibility(View.INVISIBLE);
          animation.setVisibility(View.INVISIBLE);
          //buttonRecord.setEnabled(false);
          //buttonStopRecord.setEnabled(false);
          //stopRecording();
      }
  
  
      private void initializeMediaPlayer() {
          player = new MediaPlayer();
          try {
              player.setDataSource(RADIO_STATION_URL);
          } catch (IllegalArgumentException e) {
              e.printStackTrace();
          } catch (IllegalStateException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          Intent svc=new Intent(this, BackgroundSoundService.class);
          startService(svc);
  
          player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
  
              public void onBufferingUpdate(MediaPlayer mp, int percent) {
                  playSeekBar.setSecondaryProgress(percent);
                  pDialog.dismiss();
                  animation.setVisibility(View.VISIBLE);
                  Log.i("Buffering", "" + percent);
              }
          });
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          if (player.isPlaying()) {
              player.stop();
          }
      }
  
  }
  </code></pre>