Android:如何在服务类中使用mediaController?
我有一个通过MediaPlayer作为服务播放广播的应用程序。可以在服务类中使用MediaController吗? 问题是我可以使用findviewbyd 我试着让我的线路布置在预先准备好的方法上Android:如何在服务类中使用mediaController?,android,service,media-player,mediacontroller,findviewbyid,Android,Service,Media Player,Mediacontroller,Findviewbyid,我有一个通过MediaPlayer作为服务播放广播的应用程序。可以在服务类中使用MediaController吗? 问题是我可以使用findviewbyd 我试着让我的线路布置在预先准备好的方法上 public class RadioPlayer extends Service implements OnPreparedListener, MediaController.MediaPlayerControl { @Override public void onCreate() { s
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);
}
}
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>
您不能在服务中使用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");
}
}
这是服务类的概述
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;
}
}
我也有同样的问题,我不能让它像你那样工作 解决方案是在活动中绑定服务,然后您可以从活动中访问服务中运行的MediaPlayer 请注意,要以这种方式运行,您的服务必须定义了所有必要的方法,如getCurrentPosition等 在活动中添加绑定工具:
/******************************************************************
*
* 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,我仅在用户第一次触摸屏幕时创建