2016-08-22 51 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);

我想这会做你的工作