gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:54:42 25 4
gpt4 key购买 nike

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

一个名为 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));。看起来它正确地创建了服务,并且它总是到达它说“更新元数据”的地步,但是由于某种原因,当我锁定屏幕并解锁时,我在锁定屏幕上看不到任何小部件。

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

<?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 并没有真正做任何事情。我只是想设置 Hook 。如果需要,这是我的 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

关于java - 有人可以帮助我使用 Android RemoteControlClient 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22105993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com