2017-12-18 1 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

관련 문제