Control media playback

This commit is contained in:
Jens 2022-01-26 22:53:02 +01:00
parent 5f0eab5b30
commit abaa961d3a
4 changed files with 45 additions and 50 deletions

View File

@ -333,9 +333,12 @@ public class Action
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.pause)); returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.pause));
break; break;
case "3": case "3":
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.previous)); returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.stop));
break; break;
case "4": case "4":
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.previous));
break;
case "5":
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.next)); returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.next));
break; break;
default: default:

View File

@ -1483,70 +1483,54 @@ public class Actions
keyCode = KeyEvent.KEYCODE_MEDIA_PAUSE; keyCode = KeyEvent.KEYCODE_MEDIA_PAUSE;
break; break;
case 3: case 3:
keyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS; keyCode = KeyEvent.KEYCODE_MEDIA_STOP;
break; break;
case 4: case 4:
keyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS;
break;
case 5:
keyCode = KeyEvent.KEYCODE_MEDIA_NEXT; keyCode = KeyEvent.KEYCODE_MEDIA_NEXT;
break; break;
} }
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
mAudioManager.dispatchMediaKeyEvent(event);
// if (mAudioManager.isMusicActive()) // if (mAudioManager.isMusicActive())
// { // {
// keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
// intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
/*switch (choice)
{
case play_pause:
return KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
case play:
return KeyEvent.KEYCODE_MEDIA_PLAY;
case pause:
return KeyEvent.KEYCODE_MEDIA_PAUSE;
case previous:
return KeyEvent.KEYCODE_MEDIA_PREVIOUS;
case next:
return KeyEvent.KEYCODE_MEDIA_NEXT;
default:
throw new IllegalAccessError();*/
// }
// Intent.CATEGORY_APP_MUSIC
// Intent i = new Intent("com.android.music.musicservicecommand");
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) // if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
// return controlMediaPlaybackFromApi21(keyCode); // return controlMediaPlaybackFromApi21(keyCode);
// else // else
// { // {
// KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); return controlMediaByDispatch(keyCode);
// Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); // return controlMediaPlaybackByBroadcast(keyCode);
// intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
// context.sendOrderedBroadcast(intent, null);
//
// keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
// intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
// intent.putExtra(Intent.EXTRA_KEY_EVENT, keyCode);
//
// context.sendOrderedBroadcast(intent, null);
// // AutomationService.getInstance().sendBroadcast(i);
//
//
// return true;
// } // }
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
static boolean controlMediaByDispatch(int keyCode)
{
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
mAudioManager.dispatchMediaKeyEvent(event);
return true; return true;
} }
// return false; static boolean controlMediaPlaybackByBroadcast(int keyCode)
// } {
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent, null);
keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyCode);
context.sendOrderedBroadcast(intent, null);
AutomationService.getInstance().sendBroadcast(intent);
return true;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static boolean controlMediaPlaybackFromApi21(int keyCode) static boolean controlMediaPlaybackFromApi21(int keyCode)

View File

@ -2034,7 +2034,14 @@ public class ActivityManageRule extends Activity
{ {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle(myContext.getResources().getString(R.string.selectCommand)); alertDialogBuilder.setTitle(myContext.getResources().getString(R.string.selectCommand));
final String choices[] = { myContext.getString(R.string.playPause), myContext.getString(R.string.play), myContext.getString(R.string.pause), myContext.getString(R.string.previous), myContext.getString(R.string.next) }; final String choices[] = {
myContext.getString(R.string.playPause),
myContext.getString(R.string.play),
myContext.getString(R.string.pause),
myContext.getString(R.string.stop),
myContext.getString(R.string.previous),
myContext.getString(R.string.next)
};
alertDialogBuilder.setItems(choices, new DialogInterface.OnClickListener() alertDialogBuilder.setItems(choices, new DialogInterface.OnClickListener()
{ {
@Override @Override

View File

@ -783,4 +783,5 @@
<string name="previous">previous</string> <string name="previous">previous</string>
<string name="next">next</string> <string name="next">next</string>
<string name="android.permission.MEDIA_CONTENT_CONTROL">Control media</string> <string name="android.permission.MEDIA_CONTENT_CONTROL">Control media</string>
<string name="stop">stop</string>
</resources> </resources>