媒体服务活动 [英] Media Service Activity

查看:91
本文介绍了媒体服务活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动就是使用媒体播放器,播放音频的服务。这是服务

I have an activity that is a service which plays audio using a media player. This is the service

class MusicService extends Service implements OnCompletionListener {
 MediaPlayer mediaPlayer;

 @Override
public IBinder onBind(Intent intent) {
return null;
}

 @Override
 public void onCreate() {
  mediaPlayer = MediaPlayer.create(this, R.raw.s);// raw/s.mp3
  mediaPlayer.setOnCompletionListener(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
  mediaPlayer.start();
}
return START_STICKY;
 }

  public void onDestroy() {
   if (mediaPlayer.isPlaying()) {
  mediaPlayer.stop();
   }
   mediaPlayer.release();
 }

 public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
 }

}

而不是从原始文件,我想它从那就是来自于SD卡中的String路径玩游戏。该路径在调用我如何将字符串传递给服务类,并把它的媒体播放器创建方法的服务类举行。这里就是我所说的服务

Instead of playing from that raw file i want it to play from a path that is a String that comes from the sd card. the path is held in the class that calls the service how do i pass that string to the service class and put it in the mediaplayer create method. here is where i call the service

   private void playAudio(String url) throws Exception{
Intent music = new Intent(this,MusicService.class);
startService(music);

}

推荐答案

使用的<一个href=\"http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.lang.CharSequence%29\"相对=nofollow>起始意向putExtra 命令:

music.putExtra(key, value);

然后获取传递的信息<一个href=\"http://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29\"相对=nofollow> onStartCommand 您的媒体播放器的服务:

Then get that passed info in the onStartCommand of your mediaplayer service:

intent.getExtras().getString(key);

要注意的是,当该服务是由系统重新启动这一意图可能的情况下空。

Beware that this intent can be null in cases when the service is restarted by the system.

这篇关于媒体服务活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆