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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在通知栏中显示歌曲名称和艺术家姓名相关的知识,希望对你有一定的参考价值。

我创建了一个流音乐播放器,可以播放来自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

答案

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

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);

以上是关于如何在通知栏中显示歌曲名称和艺术家姓名的主要内容,如果未能解决你的问题,请参考以下文章

如何在不启动 Activity 的情况下使用通知中的操作

使用 youtube-dl 将元数据(艺术家姓名、歌曲名称、年份、专辑、时长、流派)写入 mp3/m4a 音频文件(随后的 AtomicParsely 错误)

单击另一个活动时如何发送自定义arraylist的列表项的数据?

如何使用引用路径查询文档?

如何在具有固定高度的 flex 容器中缩小响应式图像

通过 API 从 Youtube 视频中获取艺术家和歌曲名称