我有一个通过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>