Android:如何在服务类中使用 mediaController? [英] Android: How to use mediaController in service class?

查看:28
本文介绍了Android:如何在服务类中使用 mediaController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 MediaPlayer 作为服务播放广播的应用.是否可以在 Service 类中使用 MediaController?问题是我可以使用 findViewById.

I have a app that plays radio via MediaPlayer as a service. Is it possible to use MediaController in Service class? The problem is that I can use findViewById.

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

I try to get my LinearLayout in onPrepeared methiod ...

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 文件

main.xml file

<?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,活动中有控制器.

You cannot use mediacontroller class in your service. A service does not contain a GUI so findViewById is not recognized by your service. Instead create an activity which implements mediaController then create your service and bind it to your activity. So you have mediaPlayer in your service and controller in your 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;
}
}

这篇关于Android:如何在服务类中使用 mediaController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆