如何在通知栏中显示歌曲名称和艺术家姓名

时间:2017-12-18 13:26:14

标签: java android service

我创建了一个流音乐播放器,可以从Firebase中播放歌曲。现在我创建了一个自定义通知,以在通知栏上显示歌曲详细信息。歌曲详细信息应显示在应用内部,但不会向通知栏显示详细信息。我怎么能采取行动,停止音乐。任何人都可以告诉我如何修复现有的解决方案。

这是我的通知栏代码。

public class NotificationService extends Service {

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(NConstants.ACTION.STARTFOREGROUND_ACTION)) {
        showNotification();
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();

    } else if (intent.getAction().equals(NConstants.ACTION.PREV_ACTION)) {
        Toast.makeText(this, "Clicked Previous", Toast.LENGTH_SHORT).show();
        Log.i(LOG_TAG, "Clicked Previous");
    } else if (intent.getAction().equals(NConstants.ACTION.PLAY_ACTION)) {
        Toast.makeText(this, "Clicked Play", Toast.LENGTH_SHORT).show();
        Log.i(LOG_TAG, "Clicked Play");
    } else if (intent.getAction().equals(NConstants.ACTION.NEXT_ACTION)) {
        Toast.makeText(this, "Clicked Next", Toast.LENGTH_SHORT).show();
        Log.i(LOG_TAG, "Clicked Next");
    } else if (intent.getAction().equals(
            NConstants.ACTION.STOPFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Stop Foreground Intent");
       // Toast.makeText(this, "Service Stoped", Toast.LENGTH_SHORT).show();
        stopForeground(true);
        stopSelf();
    }
    return START_STICKY;
}
Notification status;
private final String LOG_TAG = "NotificationService";

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
// Using RemoteViews to bind custom layouts into Notification
    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.status_bar);
    RemoteViews bigViews = new RemoteViews(getPackageName(),
            R.layout.status_bar_expanded);

 // showing default album image
    views.setViewVisibility(R.id.status_bar_icon, View.VISIBLE);
    views.setViewVisibility(R.id.status_bar_album_art, View.GONE);
    bigViews.setImageViewBitmap(R.id.status_bar_album_art,
            NConstants.getDefaultAlbumArt(this));

    Intent notificationIntent = new Intent(this, ViewUploadsActivity.class);
    notificationIntent.setAction(NConstants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Intent previousIntent = new Intent(this, NotificationService.class);
    previousIntent.setAction(NConstants.ACTION.PREV_ACTION);
    PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
            previousIntent, 0);

    Intent playIntent = new Intent(this, NotificationService.class);
    playIntent.setAction(NConstants.ACTION.PLAY_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);

    Intent nextIntent = new Intent(this, NotificationService.class);
    nextIntent.setAction(NConstants.ACTION.NEXT_ACTION);
    PendingIntent pnextIntent = PendingIntent.getService(this, 0,
            nextIntent, 0);

    Intent closeIntent = new Intent(this, NotificationService.class);
    closeIntent.setAction(NConstants.ACTION.STOPFOREGROUND_ACTION);
    PendingIntent pcloseIntent = PendingIntent.getService(this, 0,
            closeIntent, 0);

    views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);

    views.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);

    views.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);

    views.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);

    views.setImageViewResource(R.id.status_bar_play,
            R.drawable.ic_pause);
    bigViews.setImageViewResource(R.id.status_bar_play,
            R.drawable.ic_play);


    views.setTextViewText(R.id.status_bar_track_name,"Name"); // How i can 
      displpay the song name here 
    bigViews.setTextViewText(R.id.status_bar_track_name,"Name");

    views.setTextViewText(R.id.status_bar_artist_name, "Artist");
    bigViews.setTextViewText(R.id.status_bar_artist_name,"Artist");


    status = new Notification.Builder(this).build();
    status.contentView = views;
    status.bigContentView = bigViews;
    status.flags = Notification.FLAG_ONGOING_EVENT;
    status.icon = R.drawable.images;
    status.contentIntent = pendingIntent;
    startForeground(NConstants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);
}
}

这是我的那个获取歌曲网址和细节的类 这是另一个获取数据的类。

   public void get()
        {
       seekBarProgress.setMax(99); // It means 100% .0-99
    seekBarProgress.setOnTouchListener(this);
    mMediaPlayer.setOnBufferingUpdateListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //getting the upload
            Upload upload = uploadList.get(i);
            mSelectedTrackTitle.setText(upload.getName());
            selected_track_ar.setText(upload.getAr());
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
                mMediaPlayer.reset();
                seekBarProgress.setProgress(0);
            }

            try {
                mMediaPlayer.setDataSource(upload.getUrl());
                mMediaPlayer.prepare();




            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });



}
    @Override
public boolean onTouch(View v, MotionEvent event) {
    if(v.getId() == R.id.length){
        /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
       if(mMediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
           mMediaPlayer.seekTo(playPositionInMillisecconds);
        }
    }
    return false;


}

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

    seekBarProgress.setSecondaryProgress(percent);
}

@Override
public void onCompletion(MediaPlayer mp) {
  }

如您所见,歌曲细节应显示在应用内,但不会显示在通知栏中:

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要做的是,在为通知设置自定义视图时,您需要同时填充数据,如下所示。

RemoteViews customView = new RemoteViews(getPackageName(), R.layout.custom_layout);
customView.setTextViewText(R.id.track_name, "My fav music");
customView.setTextViewText(R.id.artist_name, "My fav artist");

Notification notification = new NotificationCompat.Builder(this).setContent(customView).allOtherMethods().build();

notificationManager.notify(1, notification);