android - How to use MediaSessianCompat for Media playback? -


i have spent literally whole day trying understand how mediasessioncompat works , does? how different mediaplayer or audiomanager class? honest did not find explanation. info got android docs , codes found on web which, according me not sufficient in case. helpful thing this video ian lake found on youtube. still not able understand how can use in our app.

this code found ian lake, , spent quite time on still have lots of doubts.

package com.example.remotecontrolclient;  import android.app.service; import android.content.componentname; import android.content.context; import android.content.intent; import android.graphics.bitmapfactory; import android.media.audiomanager; import android.os.ibinder; import android.support.v4.media.mediametadatacompat; import android.support.v4.media.session.mediasessioncompat; import android.support.v4.media.session.playbackstatecompat;  public class playerservice extends service {     private mediasessioncompat mediasession;      @override     public void oncreate() {         super.oncreate();         componentname receiver = new componentname(getpackagename(), remotereceiver.class.getname());         mediasession = new mediasessioncompat(this, "playerservice", receiver, null);         mediasession.setflags(mediasessioncompat.flag_handles_media_buttons |                 mediasessioncompat.flag_handles_transport_controls);         mediasession.setplaybackstate(new playbackstatecompat.builder()                 .setstate(playbackstatecompat.state_paused, 0, 0)                 .setactions(playbackstatecompat.action_play_pause)                 .build());         mediasession.setmetadata(new mediametadatacompat.builder()                 .putstring(mediametadatacompat.metadata_key_artist, "test artist")                 .putstring(mediametadatacompat.metadata_key_album, "test album")                 .putstring(mediametadatacompat.metadata_key_title, "test track name")                 .putlong(mediametadatacompat.metadata_key_duration, 10000)                 .putbitmap(mediametadatacompat.metadata_key_album_art,                     bitmapfactory.decoderesource(getresources(), r.mipmap.ic_launcher))                 .build());          audiomanager audiomanager = (audiomanager) getsystemservice(context.audio_service);         audiomanager.requestaudiofocus(new audiomanager.onaudiofocuschangelistener() {             @override             public void onaudiofocuschange(int focuschange) {                 // ignore             }         }, audiomanager.stream_music, audiomanager.audiofocus_gain);         mediasession.setactive(true);     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         if (mediasession.getcontroller().getplaybackstate().getstate() == playbackstatecompat.state_playing) {             mediasession.setplaybackstate(new playbackstatecompat.builder()                     .setstate(playbackstatecompat.state_paused, 0, 0.0f)                     .setactions(playbackstatecompat.action_play_pause).build());         } else {             mediasession.setplaybackstate(new playbackstatecompat.builder()                     .setstate(playbackstatecompat.state_playing, 0, 1.0f)                     .setactions(playbackstatecompat.action_play_pause).build());         }         return super.onstartcommand(intent, flags, startid);     }      @override     public ibinder onbind(intent intent) {         return null;     }      @override     public void ondestroy() {         super.ondestroy();         mediasession.release();     } } 

can please provide link tutorial or blog explains mediasessioncompat , how used media playback. if not can provide simple example play, pause , stop media say, mp3 filefrom activity using mediasessioncompat. appreciated. !!

you can watch


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -