Android: How to use mediaController in service class?

前端 未结 2 1479
不知归路
不知归路 2020-12-18 12:13

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.

I try

相关标签:
2条回答
  • 2020-12-18 12:29

    I had the same problem and I couldn't get it working the way you have it.

    The solution is to bind the Service in the Activity and then you may access the MediaPlayer running in the Service from within your Activity.

    Please note that to run this way your Service must have defined all necessary methods like getCurrentPosition etc.

    In your Activity add the binding facility:

    /****************************************************************** 
     * 
     * 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;
        }
    };
    

    Then you must modify the onStart() and onStop() methods to Bind/unBind the service.

    @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;
        }
    }
    

    What I did later is to create the following methods

    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;
      }
    

    first is a boolean that is used to realize whether we have to create the mediacontroller or not, I create only on the first time the user touches the screen.

    0 讨论(0)
  • 2020-12-18 12:49

    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.

    This is an overview of activity class.

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

    This is an overview of service class.

    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;
    }
    }
    
    0 讨论(0)
提交回复
热议问题