Reputation: 850
I made an app it run perfectly in api 20+ but for android version 4.4 and less it is getting crashed with error NoClassDefFoundError: android.media.session.MediaSessionManager
this is the stack trace that i am getting in the developer console .
java.lang.NoClassDefFoundError: android.media.session.MediaSessionManager
at beatbox.neelay.beatbox.MediaService.initMediaSession(MediaService.java:634)
at beatbox.neelay.beatbox.MediaService.onStartCommand(MediaService.java:170)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2913)
at android.app.ActivityThread.access$2100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5339)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
All I am able to understand from this is the error is in the initMediaSession method .this is my initMediaSession method
private void initMediaSession() throws RemoteException {
if (mediaSessionManager != null) return; //mediaSessionManager exists
mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
// Create a new MediaSession
mediaSession = new MediaSessionCompat(getApplicationContext(), "AudioPlayer");
//Get MediaSessions transport controls
transportControls = mediaSession.getController().getTransportControls();
//set MediaSession -> ready to receive media commands
mediaSession.setActive(true);
//indicate that the MediaSession handles transport control commands
// through its MediaSessionCompat.Callback.
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
//Set mediaSession's MetaData
updateMetaData();
// passing the data
// Attach Callback to receive MediaSession updates
mediaSession.setCallback(new MediaSessionCompat.Callback() {
// Implement callbacks
@Override
public void onPlay() {
super.onPlay();
messagesent();
a = false;
resumeMedia();
buildNotification(PlaybackStatus.PLAYING);
}
@Override
public void onPause() {
super.onPause();
messagesent();
a = true;
pauseMedia();
buildNotification(PlaybackStatus.PAUSED);
}
@Override
public void onSkipToNext() {
super.onSkipToNext();
skipToNext();
updateMetaData();
buildNotification(PlaybackStatus.PLAYING);
}
@Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
skipToPrevious();
updateMetaData();
buildNotification(PlaybackStatus.PLAYING);
}
@Override
public void onStop() {
super.onStop();
removeNotification();
//Stop the service
pauseMedia();
messagesent();
stopSelf();
}
@Override
public void onSeekTo(long position) {
super.onSeekTo(position);
}
});
}
I dont understand why it is getting crashend for 4.4 and less devices and how i can fix this .I googled and got this but this post dont tell how tofix this.
Upvotes: 2
Views: 1320
Reputation: 51
I am also following the same tutorial and had same issue. I found the solution to this. Simply check if your SDK >21 then only use method initMediaSession();
Upvotes: 1
Reputation: 629
MediaSessionManager
was only added in api 21 (5.0)
If it's absolutely necessary to use it then you can set your min sdk to 21 or check your build number with:
android.os.Build.VERSION.SDK
and not call this service with devices with lower sdks
Upvotes: 2
Reputation: 491
It seems that the reason may be due to multidex. Check your apk method count at Get Method Count
You can enable multidex by adding dependency
compile 'com.android.support:multidex:1.0.1'
then enable it in config
defaultConfig {
multiDexEnabled true
}
add following snippet in android section of your
dexOptions {
javaMaxHeapSize "4g"
preDexLibraries false
}
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = []
}
dx.additionalParameters += '--multi-dex'
// this is optional
dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()
}
}
compileOptions {
incremental false
}
Upvotes: 1