0

你好,我正在为一个电台建立一个应用程序。现在我可以在应用程序中播放音乐,但是当我进入主屏幕时音频停止。有人有关于如何使用我的代码继续在后台播放音频的提示建议吗?在后台播放radioostream

我的代码:

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

} 

回答

0

看起来你已经在使用一个Service为您的应用程序,这对长期运行的后台活动的正确方法的一部分。

为了让事情在后台运行,您应该在Service的内部初始化MediaPlayer,而不是在Activity的内部。您可以通过发送额外的Intent或通过绑定来与Service进行通信(请参阅Bound Services)。