0

Bonjour, je construis une application pour une station de radio. Maintenant, je peux la musique dans l'application, mais quand je vais à l'écran d'accueil, l'audio s'arrête. Est-ce que quelqu'un a quelques suggestions de conseils sur la façon d'utiliser mon code pour continuer à jouer de l'audio en arrière-plan?jouer radiostream en arrière-plan

Mon 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(); 
     } 
    } 

} 

Répondre

0

On dirait que vous utilisez déjà une Service pour la partie de votre application, ce qui est la bonne approche pour l'activité de fond de longue durée.

Pour que les choses tournent en arrière-plan, vous devez initialiser le MediaPlayer à l'intérieur du Service plutôt qu'à l'intérieur de votre Activity. Vous pouvez communiquer avec le Service une fois qu'il est en cours d'exécution en lui envoyant des informations supplémentaires Intent ou en établissant une liaison avec celui-ci (voir Bound Services).