How to Put Media Controller Button on Notification Bar

How to put media controller button on notification bar?

For getting media player controller in your app simply follow this:

Call this method in your MainActivity

public void showNotification(View view){
new MyNotification(this);
finish();
}

Create a new MynotificationClass

public class MyNotification extends Notification {

private Context ctx;
private NotificationManager mNotificationManager;

@SuppressLint("NewApi")
public MyNotification(Context ctx){
super();
this.ctx=ctx;
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
CharSequence tickerText = "Shortcuts";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(ctx);
@SuppressWarnings("deprecation")
Notification notification=builder.getNotification();
notification.when=when;
notification.tickerText=tickerText;
notification.icon=R.drawable.ic_launcher;

RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.messageview);

//set the button listeners
setListeners(contentView);

notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
CharSequence contentTitle = "From Shortcuts";
mNotificationManager.notify(548853, notification);
}

public void setListeners(RemoteViews view){
//radio listener
Intent radio=new Intent(ctx,HelperActivity.class);
radio.putExtra("DO", "radio");
PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
view.setOnClickPendingIntent(R.id.radio, pRadio);

//volume listener
Intent volume=new Intent(ctx, HelperActivity.class);
volume.putExtra("DO", "volume");
PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
view.setOnClickPendingIntent(R.id.volume, pVolume);

//reboot listener
Intent reboot=new Intent(ctx, HelperActivity.class);
reboot.putExtra("DO", "reboot");
PendingIntent pReboot = PendingIntent.getActivity(ctx, 5, reboot, 0);
view.setOnClickPendingIntent(R.id.reboot, pReboot);

//top listener
Intent top=new Intent(ctx, HelperActivity.class);
top.putExtra("DO", "top");
PendingIntent pTop = PendingIntent.getActivity(ctx, 3, top, 0);
view.setOnClickPendingIntent(R.id.top, pTop);*/

//app listener
Intent app=new Intent(ctx, com.example.demo.HelperActivity.class);
app.putExtra("DO", "app");
PendingIntent pApp = PendingIntent.getActivity(ctx, 4, app, 0);
view.setOnClickPendingIntent(R.id.btn1, pApp);
}

}

Create a HelperActivity class

public class HelperActivity extends Activity {

private HelperActivity ctx;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ctx = this;
String action = (String) getIntent().getExtras().get("DO");
if (action.equals("radio")) {
//Your code
} else if (action.equals("volume")) {
//Your code
} else if (action.equals("reboot")) {
//Your code
} else if (action.equals("top")) {
//Your code
} else if (action.equals("app")) {
//Your code
}

if (!action.equals("reboot"))
finish();
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}

XML layout for Notificationlayout.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/msglbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test" />

<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/msglbl" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play" android:layout_margin="10dp"/>

</RelativeLayout>

how can i show media controls for my music player website in the notification tray using javascript/Jquery?

I think the API you are looking for is the Media Session API. If you register the handlers for the appropriate actions the browser should display buttons which trigger those handlers.

navigator.mediaSession.setActionHandler('previoustrack', () => {
// Whatever you need to do to play the previous track.
});
navigator.mediaSession.setActionHandler('nexttrack', () => {
// Whatever you need to do to play the next track.
});

How to build music controller in NotificationBar/LockScreen that supporting from api version 10?

You have to use NotificationCompat with MediaStyle: https://developer.android.com/reference/android/support/v7/app/NotificationCompat.MediaStyle.html

Notification noti = new NotificationCompat.Builder()
.setSmallIcon(R.drawable.ic_stat_player)
.setContentTitle("Track title")
.setContentText("Artist - Album")
.setLargeIcon(albumArtBitmap))
.setStyle(new NotificationCompat.MediaStyle()
.setMediaSession(mySession))
.build();

A full example of implementing mediaSessionCompat with NotificationCompat can be found here: https://bitbucket.org/nonameden/android-universalmusicplayer-compat/src/412465d6605fee26f197cc5615f7da15cc859d4a/mobile/src/main/java/com/example/android/uamp/MediaNotificationManager.java?at=master

How can i use MediaController in the notification bar when app is sent to background?

In my project I do it like this:

  • I define a manual layout for the notification
  • In the layout, I add ImageButtons
  • I define a selector for background of the ImageButtons (would be possible for the source, too)
  • Finally, I manually set the layout of the Notification as given here
  • Of course, I also add a PendingIntent but as you already do that, I don't detail it here

The xml-layout-file contains ImageButtons:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp" <!-- This is where I manually define the height -->
android:orientation="horizontal" >

<ImageButton
android:id="@+id/big_notification_cancel_button"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="@drawable/notification_button_background_selector"
android:src="@drawable/some_image_with_transparent_background"
/>
<!-- some more elements.. -->
</LinearLayout>

The notification_button_background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="@drawable/white_semi_opaque_background"/>

<item
android:drawable="@drawable/transparent_background"/>

</selector>

I'm not sure if this is really the answer you're looking for. Hope it helps, though.

Edit:
I have worked with the change of Image for ImageView, too. You have to update the View after you receive a click.
In the class where I update the notification, I do this:

RemoteViews smallViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout_big);

if(playing){
smallViews.setImageViewResource(R.id.big_notification_button_play_pause, R.drawable.notification_small_pause_button_selector);
}else{
smallViews.setImageViewResource(R.id.big_notification_button_play_pause, R.drawable.notification_small_play_button_selector);
}
// and some more stuff


Related Topics



Leave a reply



Submit