有人可以帮助我使用 Android RemoteControlClient 吗?

2024-04-18

我正在尝试获取RemoteControlClient设置以便我的应用程序的音乐可以通过锁定屏幕上弹出的小部件进行控制(例如 SoundCloud、Google Play 音乐和其他音乐/视频应用程序)。我不确定我的代码有什么问题以及为什么它没有正确挂钩,但这是我到目前为止所拥有的......

一个名为 MusicService 的类尝试处理 RemoteControlClient 的更新

public class MusicService extends Service
{
public static final String ACTION_PLAY = "com.stfi.music.action.PLAY";
private RemoteController controller = null;

@Override
public void onCreate()
{
    super.onCreate();

    System.out.println("Creating the service.");

    if(controller == null)
    {
        controller = new RemoteController();
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    String action = intent.getAction();
    System.out.println("Got an action of " + action);

           /* Logic to get my Song cur */
    controller.register(this);
    controller.updateMetaData(cur);

    return START_STICKY;
}

@Override
public void onDestroy()
{
    super.onDestroy();
    System.out.println("Destorying MusicService");
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

这使用了我调用的一个类RemoteController其中容纳了我的RemoteControlClient.

public class RemoteController { 
private RemoteControlClient remoteControlClient;
private Bitmap dummyAlbumArt;


public void register(Context context)
{
    if (remoteControlClient == null)
    {
        System.out.println("Trying to register it.");

        dummyAlbumArt = BitmapFactory.decodeResource(context.getResources(), R.drawable.dummy_album_art);

        AudioManager audioManager = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);

        ComponentName myEventReceiver = new ComponentName(context.getPackageName(), MediaButtonReceiver.class.getName());
        audioManager.registerMediaButtonEventReceiver(myEventReceiver);

        // build the PendingIntent for the remote control client 
        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(myEventReceiver);
        // create and register the remote control client 
        PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);
        remoteControlClient = new RemoteControlClient(mediaPendingIntent);
        remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
                | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                );
        audioManager.registerRemoteControlClient(remoteControlClient);


    }
} 

/** 
 * Update the state of the remote control. 
 */ 
public void updateState(boolean isPlaying)
{
    if(remoteControlClient != null)
    {
        if (isPlaying)
        {
            remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
        }

        else
        { 
            remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
        } 
    } 
} 

/** 
 * Updates the state of the remote control to "stopped". 
 */ 
public void stop()
{ 
    if (remoteControlClient != null)
    {
        remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
    } 
} 

public void updateMetaData(Song song)
{
    if (remoteControlClient != null && song != null)
    {
        System.out.println("Updating metadata");
        MetadataEditor editor = remoteControlClient.editMetadata(true);
        editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, dummyAlbumArt);
        editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, (long)1000);
        editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "Artist");
        editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, "Title");
        editor.apply();

        updateState(true);
    }
}

/** 
 * Release the remote control. 
 */ 
public void release() { 
    remoteControlClient = null;
} 
} 

每次我想更新小部件时,我都会调用startService(new Intent(MusicService.ACTION_PLAY));。看起来它正确地创建了服务,并且总是到达“更新元数据”的位置,但由于某种原因,当我锁定屏幕并解锁它时,我在锁定屏幕上看不到任何小部件。

以下也是我的清单的重要部分,因为这可能会以某种方式导致问题......

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stfi"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<application
    android:hardwareAccelerated="true"
    android:allowBackup="true"
    android:icon="@drawable/stfi"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/MyActionBarTheme" >
    <meta-data
        android:name="android.app.default_searchable"
        android:value=".activities.SearchActivity" />

    <activity
        android:name=".StartingToFeelIt"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    ...other activities listed

    <service
        android:name=".helpers.MyNotificationService"
        android:enabled="true"
        android:label="MyNotificationServiceLabel" >
    </service>
    <service
        android:name=".music.MusicService"
        android:exported="false" >
        <intent-filter>

            <action android:name="com.stfi.music.action.PLAY" />

        </intent-filter>
        <intent-filter>
            <action android:name="com.example.android.musicplayer.action.URL" />

            <data android:scheme="http" />
        </intent-filter>
    </service>

    <receiver
        android:name=".music.MediaButtonReceiver"
        android:exported="false" >
    </receiver>
</application>

现在我的 MediaButtonReceiver 并没有真正做任何事情。我只是想把钩子设置好。如果你愿意,这是我的 MediaButtonReceiver 类......

public class MediaButtonReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
    System.out.println("Receiving something.");
    if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON))
    {
        final KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

        if (event != null && event.getAction() == KeyEvent.ACTION_UP)
        {

            if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)
            {
                System.out.println("You clicked pause.");
            }

            else if(event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY)
            {
                System.out.println("You clicked play.");
            }

            else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_NEXT)
            {
                System.out.println("You clicked next.");
            }

            else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
            {
                System.out.println("You clicked previous.");
            }
        }
    }
}

}


如果您在锁定屏幕上看不到 RemoteControlClient,则必须实现音频焦点。你可以看here http://developer.android.com/training/managing-audio/audio-focus.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有人可以帮助我使用 Android RemoteControlClient 吗? 的相关文章

  • Flutter - 删除 ListView 中项目之间的空间

    我正在使用 ListView builder 函数来创建项目列表 然而 iOS 中每个项目之间的空间很大 截图 你知道如何删除项目吗 看来是默认的 因为我没有添加它 code 列表显示 return Scaffold body ListVi
  • Cucumber DataTable 错误 - io.cucumber.datatable.UndefinedDataTableTypeException:无法将 DataTable 转换为 cucumber.api.DataTable

    尝试使用 cucumber selenium java intelliJ 运行场景 但在其中一个步骤中出现有关 DataTable 的错误 在我开始使用测试运行程序并更改周围的一些内容之前 数据表工作正常并正确转换该步骤的参数 但我就是无法
  • 查找数组中的组合

    我在java中有一个像这样的二维数组 transmission communication tv television approach memorycode methodact 我需要获得所有组合 例如 transmission appr
  • 更改 JComboBox 中滚动条的大小

    有谁知道如何手动更改 jComboBox 中的滚动条大小 我已经尝试了一大堆东西 但没有任何效果 好吧 我明白了 您可以实现 PopUpMenuListener 并使用它 public void popupMenuWillBecomeVis
  • 如何使用 swagger-codegen-plugin (maven) 生成客户端代码?

    我需要使用 swagger codegen plugin for maven 在 eclipse 中生成服务器存根代码 你能帮忙怎么做吗 以及需要什么配置 在 pom xml 中 我找到了这个答案 您只需要像下面这样更改 pom xml 即
  • 如何使用共享首选项在两个 Android 应用程序之间共享数据?

    我有两个应用程序 App1 和 App2 我想使用共享首选项在 App1 中保存数据并在 App2 中访问 反之亦然 我可以在 App1 中保存数据并在 App2 中访问数据 但反之则不行 这就是我现在正在做的 在清单中 android s
  • 将触摸事件从 NestedScrollView 传递到父视图

    我在 NestedScrollView 下方有一个 ViewPager 宽度一些顶部填充 以及 ClipToPadding false 和透明背景 如图像 我的 ViewPager 无法获取触摸事件并且无法工作 我怎么解决这个问题 我无法更
  • java swing:向 JTree 项目添加自定义图形按钮

    我想在 JTree 中的项目右侧添加一个带有小图标的附加按钮 这可以做到吗 如果是这样 怎么办 thanks Clamp 你在这方面成功了吗 我想做同样的事情 但很难让 JButton 响应用户 设置渲染器以显示按钮的过程很顺利 但所有鼠标
  • Android 10 请求 ACTIVITY_RECOGNITION 权限

    我试图遵守 Google 的要求 为 Android 10 请求 ACTIVITY RECOGNITION 权限 但我似乎不明白为什么没有显示权限弹出窗口 就像其他权限 即位置 存储等 一样 我的代码是 if ContextCompat c
  • 通知操作而不启动新活动?

    我计划提供一个包含两个操作的提醒通知 一个用于批准登录请求 一个用于拒绝登录请求 通过单击这些操作中的任何一个 我希望向我的服务器发出 HTTP 请求 最重要的是 我不想启动新的 Activity 或根本不想将用户重定向到我的应用程序 Co
  • 在循环中按名称访问变量

    我正在开发一个 Android 项目 并且有很多可绘制对象 这些绘图的名称都类似于icon 0 png icon 1 png icon 100 png 我想将这些可绘制对象的所有资源 ID 添加到整数 ArrayList 中 对于那些不了解
  • Java和手动执行finalize

    如果我打电话finalize 在我的程序代码中的一个对象上 JVM当垃圾收集器处理这个对象时仍然再次运行该方法吗 这是一个大概的例子 MyObject m new MyObject m finalize m null System gc 是
  • Java 中处理异步响应的设计模式

    我读过类似问答的答案 如何在 JAVA 中创建异步 HTTP 请求 https stackoverflow com questions 3142915 how do you create an asynchronous http reque
  • 按“重置应用程序首选项”后,我的应用程序的所有权限都被撤销

    我开发了一个应用程序 支持Android 6 0 当我在 设置 gt 应用程序 gt 重置应用程序首选项 中重置应用程序首选项时 我的应用程序的所有权限都将被撤销 并且应用程序不会重新启动 撤销权限后未能重新启动应用程序可能会导致许多意外崩
  • android 中的 java.net.URL ..新手问题

    我是java新手 正在尝试android开发 以下代码生成 malformedURLException 有人可以帮助我识别异常吗 任何提示都会非常有帮助 package com example helloandroid import and
  • java中的预增量/后增量

    有人可以帮助我理解为什么 int i 1 int j 1 int k 1 int l 1 System out println i i System out println j j System out println k k System
  • Android Volley - 发布请求 - 无法在线工作

    我试图通过 Volley 发出 POST 请求 它在我的本地主机中工作得很好 但是当我将它移动到网络服务器时 响应为空 Java代码 RequestQueue queue Volley newRequestQueue this String
  • Jackson 反序列化相当于 @JsonUnwrapped 吗?

    假设我有以下课程 public class Parent public int age JsonUnwrapped public Name name 生成 JSON age 18 first Joey last Sixpack 我如何将其反
  • 如何使用注释处理 Hibernate 和 Spring 中的连接查询?

    我正在使用 Spring 和 Hibernate 以及 MySQL 开发应用程序 我是 Hibernate 新手 完成了基本任务 现在我需要在选择查询中应用联接以使用注释从多个表中获取数据 我已经搜索过但仍然没有任何想法 这是我的数据库表和
  • 你能快速告诉我这个伪代码是否有意义吗?

    我相信我的代码现在是万无一失的 我现在将写出伪代码 但我确实有一个问题 为什么 DRJava 要求我返回 if 语句之外的内容 正如你所看到的 我为 ex 写了 return 1 只是因为它问了 但是它永远不会返回该值 谁可以给我解释一下这

随机推荐