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

我已经创建了一个流式音乐播放器,可以从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);

}

}

这里是我的那类,获取歌曲的URL和细节 这是一个是获取数据的类。

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

}

当你看到这首歌的细节应该是在应用程序中显示,但在通知栏不显示:

回答:

你需要做的是,当你设置自定义查看您的通知,您需要像这样同时填充数据。

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

以上是 如何在通知栏中显示歌曲名称和艺术家姓名 的全部内容, 来源链接: utcz.com/qa/263707.html

回到顶部