0

我对 android 还是很陌生,但是我在开发收音机应用程序时遵循了在线教程,现在这是我遇到的问题,我希望这台收音机继续播放,以便应用程序用户可以在收音机时继续使用该应用程序或其他应用程序在后台播放。当用户离开流媒体页面时,收音机停止播放。

  private void startPlaying() {
  buttonStopPlay.setEnabled(true);
  buttonPlay.setEnabled(false);
  player.start();


   }


  private void initializeMediaPlayer() {
  player = new MediaPlayer();
  try {
  player.setDataSource(" radio URL");

  player.setOnPreparedListener(new OnPreparedListener() {

  public void onPrepared(MediaPlayer mp) {
  player.start();


  }
  });
  player.prepareAsync();
  buttonStopPlay.setEnabled(true);
  buttonPlay.setEnabled(true);


  } catch (IllegalArgumentException e) {
  e.printStackTrace();
  } catch (IllegalStateException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }

  player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

  public void onBufferingUpdate(MediaPlayer mp, int percent) {
  playSeekBar.setSecondaryProgress(percent);
  Log.i("Buffering", "" + percent);
  }
  });
  }

 @Override
 protected void onPause() {
 super.onPause();
 if (player.isPlaying()) {
 player.pause();
 buttonStopPlay.setEnabled(false);
 buttonPlay.setEnabled(true);
 }

 }
4

1 回答 1

0

参考这个链接

Intent svc=new Intent(this, BackgroundSoundService.class);
    startService(svc);



public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.idil);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);

    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
    }
    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}
于 2013-01-31T05:21:20.867 回答