Android uses AIDL to realize music playing case

  • 2021-08-31 09:25:36
  • OfStack

This article example for everyone to share Android using AIDL way to play music specific code, for your reference, the specific content is as follows

Thoughts:

① Create two new APP projects or Module, one as the server and one as the client, and create service on the server

2. Create two new aidl files (interfaces) in the main files of the two module files, which define the method of processing music

After defining the method in the two AIDL files, give them makeproject in the taskbar and compile them into Java files before they can be used in service and acvitity
interface. Stub requires instantiation to implement remote methods

④ The onbind method in Service returns interface. Stub.

sevice needs to set action, otherwise the client service will report null pointer exception when running
Handle the server first:

In the Mainfest file, add action for the intent implicit call


 <service
   android:name=".MusicService"
   android:enabled="true"
   android:exported="true">
   <intent-filter>
   <action android:name="com.work.MusicService"></action>
 </intent-filter>

MusicService. Java


public class MusicService extends Service {

  private MediaPlayer player = null;

  public MusicService() {
  }

  // ① The realization has been makeproject Pass adil Interface , Override the custom definition inside 3 Methods 
  MusicAidlInterface.Stub stub = new MusicAidlInterface.Stub() {
    @Override
    public void paly() throws RemoteException {
      if (player == null) {
        player = MediaPlayer.create(MusicService.this, R.raw.hckz);
      }
      if (player != null && !player.isPlaying()){
        player.start();
      }

    }

    @Override
    public void paus() throws RemoteException {
      if(player!=null&&player.isPlaying()){
        player.pause();
      }

    }

    @Override
    public void stop() throws RemoteException {
      if(player!=null){
        player.stop();
      }
      try {
        player.prepare();// Prepare for the next play 
      } catch (IOException e) {
        e.printStackTrace();
      }

    }
  };

  @Override
  public IBinder onBind(Intent intent) {
    return stub;
  }
  // ②   Rewrite service Method of destruction 

  @Override
  public void onDestroy() {
    super.onDestroy();
    if(player!=null){
      player.stop();
      player.release();// Release resources to prevent loss of support overflow and anomaly 
    }
  }

Processing client


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private Button btn_play, btn_pause, btn_stop, btn_stopservice, btn_stopacvitity;
   MusicAidlInterface service = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    intiView();
    connect();

  }

  private void intiView() {
    btn_play = (Button) findViewById(R.id.btn_play);
    btn_pause = (Button) findViewById(R.id.btn_pause);
    btn_stopservice = (Button) findViewById(R.id.btn_stopservice);
    btn_stopacvitity = (Button) findViewById(R.id.btn_exitacvitity);
    btn_stop = (Button) findViewById(R.id.btn_stop);

    btn_play.setOnClickListener(this);
    btn_pause.setOnClickListener(this);
    btn_stop.setOnClickListener(this);
    btn_stopservice.setOnClickListener(this);
    btn_stopacvitity.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    try {
      switch (v.getId()) {
        case R.id.btn_play:
          service.paly();
          break;
        case R.id.btn_pause:
          service.paus();
          break;
        case R.id.btn_stop:
          service.stop();
          break;
        case R.id.btn_stopservice:
          if (conn != null) {
            unbindService(conn);
          }
          break;
        case R.id.btn_exitacvitity:
          finish();
          break;
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    }

  }

  // Connect service
  ServiceConnection conn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder iBinder) {
      service = MusicAidlInterface.Stub.asInterface(iBinder);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
      service = null;
    }
  };

  // Get a connection BindService Method of 
  private void connect() {
    // Use intent Implicitly invoke the method of 
    Intent intent = new Intent("com.work.MusicService");
    // Set the target service Package name of 
    intent.setPackage("com.work.musicservice");
    bindService(intent, conn, BIND_AUTO_CREATE);
  }
  // Rewrite acvitity How to unbind when destroying 

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (conn != null) {// Unbind 
      unbindService(conn);
    }
  }

Related articles: