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

Krz*_*cki 3 service android media-player mediacontroller findviewbyid

我有一个应用程序,通过MediaPlayer播放无线电作为服务.是否可以在Service类中使用MediaController?问题是我可以使用findViewById.

我试着让我的LinearLayout进入onPrepeared方法......

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);
}
}
Run Code Online (Sandbox Code Playgroud)

main.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>
Run Code Online (Sandbox Code Playgroud)

小智 6

您不能在服务中使用mediacontroller类.服务不包含GUI,因此您的服务无法识别findViewById.而是创建一个实现mediaController的活动,然后创建您的服务并将其绑定到您的活动.因此,您的活动中的服务和控制器中都有mediaPlayer.

这是活动类的概述.

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");

}
}
Run Code Online (Sandbox Code Playgroud)

这是服务类的概述.

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;
}
}
Run Code Online (Sandbox Code Playgroud)