2016-08-22 30 views
1

我正在開發一個帶有服務的音樂播放器。 Сurrently重點 聲明和按鈕...Android:音樂播放器的通知錯誤。?

現在我對通知欄播放,下一首,上..我想 顯示暫停按鈕,如果媒體播放&播放按鈕,如果不打 ...

按鈕

的錯誤是,當我增加了如果條件的通知.addction它顯示紅線..

我做了裏面的代碼是顯示錯誤...

服務代碼是...

Foreground.java(類名)..

public class ForegroundService extends Service { 
    private static final String LOG_TAG = "ForegroundService"; 
    public static boolean IS_SERVICE_RUNNING = false; 
    final MediaPlayer mp=new MediaPlayer(); 

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) { 
      Log.i(LOG_TAG, "Received Start Foreground Intent "); 
      showNotification(); 
      //------------------------------------------- 

      try{ 
       //you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3 
       mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/downloadedfile.mp3"); 

       mp.prepare(); 
      }catch(Exception e){e.printStackTrace();} 


      mp.start(); 


      //------------------------------------- 
      Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show(); 

     } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) { 
      Log.i(LOG_TAG, "Clicked Previous"); 

      Toast.makeText(this, "Clicked Previous!", Toast.LENGTH_SHORT) 
        .show(); 
     } else if (intent.getAction().equals(Constants.ACTION.PAUSE_ACTION)) { 
      Log.i(LOG_TAG, "Clicked Play"); 


      Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show(); 
     } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) { 
      Log.i(LOG_TAG, "Clicked Next"); 

      Toast.makeText(this, "Clicked Next!", Toast.LENGTH_SHORT).show(); 
     } else if (intent.getAction().equals(
       Constants.ACTION.STOPFOREGROUND_ACTION)) { 
      Log.i(LOG_TAG, "Received Stop Foreground Intent"); 
      stopForeground(true); 
      stopSelf(); 
     } 
     return START_STICKY; 
    } 

    private void showNotification() { 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     notificationIntent.setAction(Constants.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, ForegroundService.class); 
     previousIntent.setAction(Constants.ACTION.PREV_ACTION); 
     PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, 
       previousIntent, 0); 

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


//--------------------------------------------------------------------------------- 
     Intent pauseIntent = new Intent(this, ForegroundService.class); 
     pauseIntent.setAction(Constants.ACTION.PAUSE_ACTION); 
     PendingIntent ppauseIntent = PendingIntent.getService(this, 0, 
       playIntent, 0); 
//------------------------------------------------------------------------ 


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

     Bitmap icon = BitmapFactory.decodeResource(getResources(), 
       R.drawable.aa); 

     Notification notification = new NotificationCompat.Builder(this) 
       .setContentTitle("TutorialsFace Music Player") 
       .setTicker("TutorialsFace Music Player") 
       .setContentText("My song") 
       .setSmallIcon(R.drawable.aa) 
       .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) 
       .setContentIntent(pendingIntent) 
       .setOngoing(true) 
       .addAction(android.R.drawable.ic_media_previous, "Previous", 
         ppreviousIntent) 
//--------------------------------------------------------------------------- 
       if(mp.isPlaying()){ 
        .addAction(android.R.drawable.ic_media_play, "Play", 
          pplayIntent) 
       } 
     else{ 
        .addAction(android.R.drawable.ic_media_pause, "Pause", 
          pplayIntent) 
       } 

//------------------------------------------------------------------------------------------------ 

       .addAction(android.R.drawable.ic_media_next, "Next", 
         pnextIntent).build(); 
     startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, 
       notification); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(LOG_TAG, "In onDestroy"); 
     Toast.makeText(this, "Service Detroyed!", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // Used only in case if services are bound (Bound Services). 
     return null; 
    } 
} 

我添加的代碼是將線內.....

任何人都可以建議我使用Exact代碼或Displayig播放和暫停按鈕同時進行?謝謝

+0

你好,你有什麼確切的錯誤?您的播放/暫停按鈕圖標未更新?或者addAction方法強調Api版本錯誤? –

+0

當我添加條件在.addAction之間時顯示紅線。 –

+0

我想建議你使用完全自定義的視圖。 1)你可以做任何視圖,你想要什麼,2)你會支持<20 api版本 –

回答

0

我建議你需要使用媒體播放器的自定義視圖。 Here這裏是一個教程,你如何做到這一點。 總之:你需要使用:

notification = new Notification.Builder(this).build(); 
notification.contentView = views; 
notification.bigContentView = bigViews; 

提供定製和擴展的自定義視圖

接下來你需要,當你開始展示它來更新您的通知不僅有,而且任何時候,當你按下發揮暫停按鈕。所以,你需要改變:

} else if (intent.getAction().equals(Constants.ACTION.PAUSE_ACTION)) { 
    Log.i(LOG_TAG, "Clicked Play"); 
    Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show(); 
} 

} else if (intent.getAction().equals(Constants.ACTION.PAUSE_ACTION)) { 
    Log.i(LOG_TAG, "Clicked Pause"); 
    mp.pause(); 
    showNotification(); 
    Toast.makeText(this, "Clicked Pause!", Toast.LENGTH_SHORT).show(); 
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) { 
    Log.i(LOG_TAG, "Clicked Play"); 
    mp.start(); 
    showNotification(); 
    Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show(); 
} 

類似的東西。希望你有主意。

0

將分號if和然後之前使用通知對象來設置的動作如下:

如果(mp.isPlaying())notification.addAction(android.R.drawable.ic_media_play,「播放「, pplayIntent); else notification .addAction(android.R.drawable.ic_media_pause,「Pause」, pplayIntent);

我想這會做你的工作