You don't need to control SDK_INT if you are using Compat version of components. Below code tested with many car bluetooth devices and works like charm. Some devices don't understand some KEYs so it's better to use possible KEY. Reference. Don't forget to .build() after putBitmap not before
#if JAVA
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadata;
import android.media.RemoteControlClient;
import android.media.RemoteControlClient.MetadataEditor;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.os.Binder;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat; // I added this line
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 android.content.Intent;
import android.content.IntentFilter;
import android.app.PendingIntent;
import android.app.Service;
import com.google.android.gms.cast.TextTrackStyle;
private AudioManager mAudioManager;
private AudioTrack mAudioTrack;
private RemoteControlClient mRemoteControlClient;
private MediaSession mMediaSession;
private MediaSessionCompat mMediaSessionCmp; // I added this line
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) {
BA.Log("Version Less then 21");
if (this.mRemoteControlClient == null) {
Log.d("init()", "API " + VERSION.SDK_INT + " lower than " + 21);
Log.d("init()", "Using RemoteControlClient API.");
//-----------------------------
// Tried with or with the lines below still crashes
// 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) {
BA.Log("Version greater then or equal 21");
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);
}
//-----------------------------------------------------------
// My new code
//-----------------------------------------------------------
if (this.mMediaSessionCmp == null) {
BA.Log("Creating MediaSessionCmp");
this.mMediaSessionCmp = new MediaSessionCompat(this, "PlayerServiceMediaSession"); <===== My Code crashes here
this.mMediaSessionCmp.setFlags(2);
this.mMediaSessionCmp.setActive(true);
BA.Log("Created");
}
}
public void sendTrackInfoToBluetoothDevice() {
BA.Log("sendTrackInfo");
if (VERSION.SDK_INT >= 21) {
BA.Log("API " + VERSION.SDK_INT + " greater than " + 21);
this.mMediaSession.setMetadata(new MediaMetadata.Builder().putString(MediaItemMetadata.KEY_TITLE, "Erel(A)-title").putString(MediaItemMetadata.KEY_ARTIST, "Erel(A)-artist").putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Erel(A)-album").build());
this.mMediaSession.setPlaybackState(new PlaybackState.Builder().setActions(4).setState(3, -1, TextTrackStyle.DEFAULT_FONT_SCALE, SystemClock.elapsedRealtime()).build());
} else if (VERSION.SDK_INT >= 18) {
BA.Log("API " + VERSION.SDK_INT + " greater than " + 18);
if (mAudioManager.isBluetoothA2dpOn() && mMediaSessionCmp != null) {
BA.Log("Creating MediaMetadataCompat");
String songTitle = "MMC-Title";
String artistTitle = "MMC-Artist";
long duration = 123;
final MediaMetadataCompat.Builder metadata = new MediaMetadataCompat.Builder();
metadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, songTitle);
metadata.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, songTitle);
metadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artistTitle);
metadata.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, artistTitle);
metadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
BA.Log("Sending MediaMetadataCompat");
this.mMediaSessionCmp.setMetadata(metadata.build());
BA.Log("Send MediaMetadataCompat");
}
}
}
#end if