2017-12-18 11 views
0

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

ソングの詳細はアプリ内のディスプレイでなければなりませんが、通知バーに表示されません。見ての通り:カスタムを設定しているとき

enter image description here

+0

通知にトラック名とアーティスト名を入力しますか? –

+0

はいいいえ通知のトラック名とアーティスト名を入力したい – Reality

答えて

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

私の歌の名前とアーティスト名はFirebaseにアップロードされます – Reality

+0

@実際のコードは既にデータを取得したことを示しています –

+0

しかし、タイトルと名前が通知バーに表示されません。あなたが写真で見るように – Reality

関連する問題