Android: 在服务类中如何使用mediaController?

3

我有一个通过MediaPlayer作为服务播放广播的应用程序。在Service类中使用MediaController是否可行?问题是我无法使用findViewById。

我尝试在onPrepared方法中获取我的LinearLayout ...

public class RadioPlayer extends Service implements OnPreparedListener, MediaController.MediaPlayerControl {
  @Override
  public void onCreate() {
  super.onCreate();
  mediaPlayer = new MediaPlayer();
  mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  mediaPlayer.setOnPreparedListener(this);
  mediaController = new MediaController(this, false); mediaPlayer.setDataSource(url);
  mediaPlayer.prepareAsync(); 
}

@Override
  public void onPrepared(MediaPlayer mediaPlayer) {
  mediaPlayer.start();
  mediaController.setMediaPlayer(this);
  LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  View layout = inflater.inflate(R.layout.main, (ViewGroup) findViewById(R.id.main_program_view));
  mediaController.setAnchorView(layout);
}
}

主要的xml文件

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_program_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:orientation="vertical" >  

        <TextView
            android:id="@+id/now_playing_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:text="@string/na_antenie"
            android:textColor="#333333" />    
</LinearLayout>
2个回答

7

您不能在服务中使用mediacontroller类。服务不包含GUI,因此findViewById无法被服务识别。 相反,创建一个实现mediaController的活动,然后创建您的服务并将其绑定到您的活动。这样您就可以在服务中拥有mediaPlayer,并在活动中拥有控制器。

这是activity类的概述。

public class MusicPlayerActivity extends Activity implements MediaController.MediaPlayerControl{
//Do all the stuff
void initMusic(){
    mMediaController = new MediaController(this,false);
    //Note do not create any variable of mediaplayer
    mMediaController.setMediaPlayer(this);       
    mMediaController.setAnchorView(findViewById(R.id.anchorText));

    new Thread(new Runnable() {

        @Override
        public void run() {
        startService(new Intent("PLAY_MUSIC"));
        }
    }).start();
        MusicService.setPathOfSong("Provide the path");

}
}

这是服务类的概述。
public class MusicService extends Service implements MediaPlayer.OnPreparedListener {
private MediaPlayer mMediaPlayer;
private static String pathOfSong;
public int onStartCommand(Intent intent, int flags, int startId) {
        mMediaPlayer=new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
        //Other stuff
}
public static void setPathOfSong(String path)
{
    pathOfSong=path;
}
}

4

我遇到了同样的问题,但是我没能按照你的方式解决它。

解决方法是在Activity中绑定Service,然后你可以从Activity内部访问运行在Service中的MediaPlayer。

请注意,为了以这种方式运行,您的Service必须定义了所有必要的方法,比如getCurrentPosition等。

在您的Activity中添加绑定设施:

/****************************************************************** 
 * 
 * Defines callbacks for service binding, passed to bindService() 
 * 
 * ************************************************************** */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};

然后你必须修改onStart()和onStop()方法来绑定/解绑服务。

@Override
protected void onStart() {
    super.onStart();
    // Bind to LocalService
    Intent intent = new Intent(this, MusicService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    // Unbind from the service
    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
}

我后来做的是创建以下方法。
public void showMediaControllerHere(){
    if (mBound){
        mc = new MediaController(mpContext);
        mc.setAnchorView(findViewById(R.id.anchorText));
        mc.setMediaPlayer(mService);
        mc.setEnabled(true);
        mc.show(0);
    }
}
@Override
  public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    if (first){
          if (mBound){
              showMediaControllerHere();
              first = false;
          }
      }
      else {
          if (mBound){
              mc.show(0);
          }
      }
    return false;
  }

首先是一个布尔值,用于判断是否需要创建mediacontroller。只有在用户第一次触摸屏幕时才创建。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接