首页 > 解决方案 > 从服务模拟 YouTube 等应用的暂停/播放

问题描述

我正在尝试全局模拟播放/暂停操作,以便在播放 YouTube 视频时,它会暂停或恢复。

我不确定如何在服务中专门实现这一点。

所以我有这个功能

public static void sendMediaButton(Context context, 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, keyEvent);
    context.sendOrderedBroadcast(intent, null);
}

我称之为

sendMediaButton(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PAUSE);

但是在我的服务中,这没有任何作用,YouTube 视频仍在播放。

FWIW,我的应用程序同时具有ACTION_MANAGE_OVERLAY_PERMISSION权限SYSTEM_ALERT_WINDOW,因此它仍然可以以最小的限制启动活动等等。

我的问题是:如何在我的服务中模拟播放和暂停操作?

谢谢!

标签: javaandroidandroid-studioserviceandroid-audiomanager

解决方案


推荐阅读