我有一个通过 MediaPlayer 作为服务播放广播的应用程序。是否可以在 Service 类中使用 MediaController?问题是我可以使用 findViewById。
我尝试在 onPrepeared 方法中获取我的 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);
}
}
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>