Iqraa
Iqraa

Reputation: 445

How to show notification with play/stop button?

I need some help as I am a bit new in android. In my android app that I am working on, I have implemented MediaPlayer service class (PlayService.java) which will play live radio audio stream from a remote source. My working code for MediaPlayer Service class is:

public class PlayService extends Service implements MediaPlayer.OnCompletionListener,MediaPlayer.OnPreparedListener,
        MediaPlayer.OnErrorListener,MediaPlayer.OnSeekCompleteListener,MediaPlayer.OnInfoListener,
        MediaPlayer.OnBufferingUpdateListener,AudioManager.OnAudioFocusChangeListener  {

    private AudioManager audioManager;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private String sentAudioLink;

    @Override
    public void onCreate() {
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnSeekCompleteListener(this);
        mediaPlayer.setOnInfoListener(this);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.reset();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        sentAudioLink = intent.getExtras().getString("sentAudioLink");
        mediaPlayer.reset();
        //setup the media player data source using the strAudioLink value
        if (!mediaPlayer.isPlaying()) {
            try {
                mediaPlayer.setDataSource(sentAudioLink);
                //prepare media player
                mediaPlayer.prepareAsync();
            } catch (IllegalStateException e) {
                Log.e("workingerror1", e.toString());
                stopSelf();
            } catch (IllegalArgumentException e) {
                Log.e("workingerror2", e.toString());
                stopSelf();
            } catch (IOException e) {
                Log.e("workingerror3", e.toString());
                stopSelf();
            }

            //buildNotification(PlaybackStatus.PLAYING);
        }

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        stopMedia();
        stopSelf();
    }

    public void stopMedia() {
        if (mediaPlayer.isPlaying())
            mediaPlayer.stop();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        //Invoked when there has been an error during an asynchronous operation
        return false;
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        playMedia();
    }

    public void playMedia() {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }

    @Override
    public void onSeekComplete(MediaPlayer mp) {

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
} 

The only problem that I face is I just want that music player will generate a custom notification when the audio stream is playing. And for that purpose, I have created custom_notification.xml class. I don't know that how should I create this custom notification from service and trigger call listeners for play and stop action button.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">
    <ImageView
        android:layout_alignParentLeft="true"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/uow_logo"
        android:id="@+id/imgAppIc" />
    <RelativeLayout
        android:layout_toRightOf="@id/imgAppIc"
        android:singleLine="true"
        android:id="@+id/textView1"
        android:ellipsize="marquee"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="5dp"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/selected_track_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title Text"
            android:textColor="#f7212121"
            android:textSize="15dp"/>
        <TextView
            android:id="@+id/selected_track_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FM Radio 100"
            android:textColor="#767676"
            android:layout_below="@+id/selected_track_title"
            android:layout_marginTop="1dp"
            android:textSize="15dp" />
    </RelativeLayout>
    <ImageButton
        android:id="@+id/play_pause"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:gravity="right"
        android:layout_alignParentRight="true"
        android:layout_marginTop="3dp" />
</RelativeLayout>

Upvotes: 1

Views: 515

Answers (1)

Ashvin solanki
Ashvin solanki

Reputation: 4809

This Answer May Helps You...

Ref: How to create a Custom Notification Layout in android?

To define a custom notification layout, start by instantiating a RemoteViews object that inflates an XML layout file. Then, instead of calling methods such as setContentTitle(), call setContent(). To set content details in the custom notification, use the methods in RemoteViews to set the values of the view's children:

Create an XML layout for the notification in a separate file. You can use any file name you wish, but you must use the extension .xml In your app, use RemoteViews methods to define your notification's icons and text. Put this RemoteViews object into your NotificationCompat.Builder by calling setContent(). Avoid setting a background Drawable on your RemoteViews object, because your text color may become unreadable.

custom_push.xml has my custom views R.id.image,R.id.text,R.id.title

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="64dp"
    android:padding="10dp" >
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView
        android:textSize="13dp"
        android:textColor="#000"
        android:text="Testing"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        />
    <TextView
        android:textSize="13dp"
        android:textColor="#000"
        android:text="Testing is awecome"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
         />
</RelativeLayout>

Instantiating a RemoteViews object and set it,

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContent(contentView);

Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(1, notification);

Notification Views Like

check : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle

Upvotes: 1

Related Questions