2013/05/22

Android 監聽原生音樂播放器

這個專案可以去控制到Android原生的音樂播放器,所謂的原生就是系統內建的音樂播放器
透過監聽廣播的方式去得知目前的動作,在透過發送廣播的方式去控制到音樂播放器





package com.example.listenmusic;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ListenMusic extends Activity {
 private Button btn_TogglePause;
 private Button btn_Pause;
 private Button btn_Next;
 private Button btn_Previous;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_listen_music);
  init();
  filterMusic();
 }

 private void init() {
  btn_Next = (Button) findViewById(R.id.buttonNext);
  btn_Next.setOnClickListener(clickListenerNext);
  btn_Pause = (Button) findViewById(R.id.buttonPause);
  btn_Pause.setOnClickListener(clickListenerPause);
  btn_Previous = (Button) findViewById(R.id.buttonPrevious);
  btn_Previous.setOnClickListener(clickListenerPrevious);
  btn_TogglePause = (Button) findViewById(R.id.buttonTogglePause);
  btn_TogglePause.setOnClickListener(clickListenerStop);
 }

 private void filterMusic() {
  IntentFilter filter = new IntentFilter();
  filter.addAction(MusicBroadcastReceiver.SERVICECMD);
  filter.addAction(MusicBroadcastReceiver.TOGGLEPAUSE_ACTION);
  filter.addAction(MusicBroadcastReceiver.PAUSE_ACTION);
  filter.addAction(MusicBroadcastReceiver.NEXT_ACTION);
  filter.addAction(MusicBroadcastReceiver.PREVIOUS_ACTION);
  registerReceiver(new MusicBroadcastReceiver(), filter);
 }

 private Button.OnClickListener clickListenerStop = new OnClickListener() {
  @Override
  public void onClick(View view) {
   Intent intent = new Intent(
     MusicBroadcastReceiver.TOGGLEPAUSE_ACTION);
   sendBroadcast(intent);
  }
 };
 private Button.OnClickListener clickListenerPause = new OnClickListener() {
  @Override
  public void onClick(View view) {
   Intent intent = new Intent(MusicBroadcastReceiver.PAUSE_ACTION);
   sendBroadcast(intent);
  }
 };
 private Button.OnClickListener clickListenerNext = new OnClickListener() {
  @Override
  public void onClick(View view) {
   Intent intent = new Intent(MusicBroadcastReceiver.NEXT_ACTION);
   sendBroadcast(intent);
  }
 };
 private Button.OnClickListener clickListenerPrevious = new OnClickListener() {
  @Override
  public void onClick(View view) {
   Intent intent = new Intent(MusicBroadcastReceiver.PREVIOUS_ACTION);
   sendBroadcast(intent);
  }
 };

}

package com.example.listenmusic;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MusicBroadcastReceiver extends BroadcastReceiver {
 public static final String SERVICECMD = "com.android.music.musicservicecommand";
 public static final String CMDNAME = "command";
 public static final String CMDTOGGLEPAUSE = "togglepause";
 public static final String CMDSTOP = "stop";
 public static final String CMDPAUSE = "pause";
 public static final String CMDPREVIOUS = "previous";
 public static final String CMDNEXT = "next";
 public static final String TOGGLEPAUSE_ACTION = "com.android.music.musicservicecommand.togglepause";
 public static final String PAUSE_ACTION = "com.android.music.musicservicecommand.pause";
 public static final String PREVIOUS_ACTION = "com.android.music.musicservicecommand.previous";
 public static final String NEXT_ACTION = "com.android.music.musicservicecommand.next";

 @Override
 public void onReceive(Context context, Intent intent) {
  
  String action = intent.getAction();
 
  String cmd = intent.getStringExtra("command");

  if (CMDNEXT.equals(cmd) || NEXT_ACTION.equals(action)) {
   Log.i("NEXT", action);
  } else if (CMDPREVIOUS.equals(cmd) || PREVIOUS_ACTION.equals(action)) {
   Log.i("PREVIOUS", action);
  } else if (CMDTOGGLEPAUSE.equals(cmd)
    || TOGGLEPAUSE_ACTION.equals(action)) {
   Log.i("PAUSE", action);
  } else if (CMDPAUSE.equals(cmd) || PAUSE_ACTION.equals(action)) {
   Log.i("PAUSE", action);
  } else if (CMDSTOP.equals(cmd)) {
   Log.i("STOP", action);
  }
 }
}