Android Question Bitmap for bluetooth metadata not showing

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Using this modified version of the code posted by Erel

NOTE: My Samsung S7 is using the >= 21 code

B4X:
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaCodec;
import android.media.MediaDescription;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadata;
import android.media.MediaMetadataRetriever;
import android.media.RemoteControlClient.MetadataEditor;
import android.media.RemoteControlClient;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.os.Binder;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.ResultReceiver;
import android.os.SystemClock;
import android.service.media.MediaBrowserService.Result;
import android.support.v4.media.MediaBrowserCompat.MediaItem;
import android.support.v4.media.MediaBrowserServiceCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaButtonReceiver;
import android.support.v4.media.session.MediaSessionCompat.*;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v7.media.MediaItemMetadata;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.format.Time;
import android.util.Log;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.IntentWrapper;
import com.google.android.gms.cast.TextTrackStyle;
import java.util.ArrayList;
import java.util.List;


private AudioManager        mAudioManager;
private AudioTrack            mAudioTrack;
private RemoteControlClient mRemoteControlClient;
private MediaSession        mMediaSession;

 
    public void _onCreate() {
                 BA.Log("Running onCreate");
               IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
                 this.mAudioManager  = (AudioManager) getSystemService("audio");
               
               if (VERSION.SDK_INT < 21) {
                   if (this.mRemoteControlClient == null) {
                       Log.d("init()", "API " + VERSION.SDK_INT + " lower than " + 21);
                       Log.d("init()", "Using RemoteControlClient API.");
                       this.mRemoteControlClient = new RemoteControlClient(PendingIntent.getBroadcast(this, 0, new Intent("android.intent.action.MEDIA_BUTTON"), 0));
                       this.mAudioManager.registerRemoteControlClient(this.mRemoteControlClient);
                   }
               } else if (this.mMediaSession == null) {
                           Log.d("init()", "API " + VERSION.SDK_INT + " greater or equals " + 21);
                           Log.d("init()", "Using MediaSession API.");
                           this.mMediaSession = new MediaSession(this, "StreamPlayerServiceMediaSession");
                           this.mMediaSession.setFlags(2);
                           this.mMediaSession.setActive(true);
               }
    }
   
   public void mediaSessionSetCallBack(MediaSession.Callback callback)  {
                   BA.Log("mediaSessionSetCallBack");                                                 
   
                   if (this.mMediaSession != null)  {
                     BA.Log("mediaSessionSetCallBack - Setting callback");                                                 
               
                   this.mMediaSession.setCallback(callback);
               }
   }
       
   public void mediaSessionAllDone() {
                   BA.Log("mediaSessionAllDone");                                                 
   
               if (this.mMediaSession != null) {
                   BA.Log("mediaSessionAllDone  - release");                               
                   this.mMediaSession.setActive(false);
                   this.mMediaSession.release();
               }
   }
       
   public void sendTrackInfoToBluetoothDevice(Boolean IsPlaying, String Album, String AlbumArtist, String Artist, String Song, long Duration, long TrackNumber, Bitmap bitmap) {
   
                 if (VERSION.SDK_INT >= 21) {
//                         BA.Log("sendTrackInfoToBluetoothDevice - >21");               
                                   
                       if  (AlbumArtist.equalsIgnoreCase("Various Artists") == false) {
                            Artist = AlbumArtist;
                       }
                       //  STATE_PLAYING           =   3
                       //  STATE_PAUSED           =   2
                       //  STATE_STOPPED           =   1
                       
                       //   ACTION_STOP               =   1
                       //  ACTION_PAUSE           =   2       
                       //  ACTION_PLAY               =   4                   
                       //  ACTION_SKIP_TO_NEXT       =  32
                       //  ACTION_SKIP_TO_PREVIOUS =  16
                       //  ACTION_PLAY_PAUSE       = 512
                       MediaMetadata metadata = new MediaMetadata.Builder()                                                       
                                                       .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART,   bitmap)                                                                                                                               
                                                       .putBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON,   bitmap)                                                                                                                                                                                       
                                                       .putString(MediaMetadata.METADATA_KEY_TITLE,        Song)
                                                       .putString(MediaMetadata.METADATA_KEY_ARTIST,        Artist)
                                                       .putString(MediaMetadata.METADATA_KEY_ALBUM,        Album)
                                                       .putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER,    TrackNumber)   
                                                       .build();

//                                                       .putLong(MediaMetadata.METADATA_KEY_DURATION,        Duration)
//                                                       .putString(MediaMetadata.METADATA_KEY_DISPLAY_DESCRIPTION, "Display Discription")
//                                                       .putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, "SubTitle")
               
                     this.mMediaSession.setMetadata(metadata);
                     this.mMediaSession.setPlaybackState(new PlaybackState.Builder()
                                                                       .setActions(PlaybackState.ACTION_STOP | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
                                                                       .setState(IsPlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED, -1, TextTrackStyle.DEFAULT_FONT_SCALE, SystemClock.elapsedRealtime())
                                                                       .build());
                 } else if (VERSION.SDK_INT >= 18) {
                             BA.Log("sendTrackInfoToBluetoothDevice - >18");                                                 
               
                           MediaMetadataRetriever  metadata;
                           
                             android.media.RemoteControlClient.MetadataEditor ed = this.mRemoteControlClient.editMetadata(true);
                             ed.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM,        Album);
                             ed.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST,        Artist);
                             ed.putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST,    AlbumArtist);                           
                             ed.putString(MediaMetadataRetriever.METADATA_KEY_TITLE,        Song);
                           ed.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION,        Duration);
                           ed.putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, TrackNumber);
                           ed.putBitmap(100,                                                bitmap);                           
                             ed.apply();
                             this.mRemoteControlClient.setPlaybackState(3, -1, TextTrackStyle.DEFAULT_FONT_SCALE);
                 }
  }

Everything is showing up on my 2017 Honda Ridgeline display.

By everything I mean Album, Artist, Title and TrackNumber but not the bitmaps

Anyone have any success getting bitmaps to display?

I've used this program called Sanse Mp3 Art Sizer to resize all my bitmaps in my mp3 files to 250 x 250 and I load that bitmap using: cMediaData GetEmbeddedBitmap

I know the bitmaps are fine because I can display them on my phone but doesn't seem to be showing up on the car.

BobVal
 
Top