2016-08-22 2 views
1

Ich entwickle einen Musik-Player mit Service. Сurrently Fokussierung auf Hinweisen und Buttons ...Android: Benachrichtigungsfehler bei Musikplayer.?

Jetzt habe ich Tasten auf dem Notification Bar Play, Weiter, Zurück .. Ich will Pause-Taste, um anzuzeigen, wenn Medien & Play-Taste spielen, wenn nicht spielen ...

der Fehler ist, dass, wenn ich die If Bedingung innerhalb der Mitteilung .addction hinzugefügt es rote Linien wird angezeigt ..

ich den Code getan hatte es Fehler anzeigt ...

Der Service Code ist ...

Foreground.java (Klassenname) ..

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

Der Code I wird innerhalb der Linie Put hinzugefügt .....

Kann jemand Schlage mir den Exakten Code oder Displayig Play und Pause Button gleichzeitig vor ?? Danke

+0

Hallo, was genau Fehler hast du? Ihr Play/Pause-Button-Icon wird nicht aktualisiert? Oder addAction-Methode unterstreicht mit Api-Versionsfehler? –

+0

Es zeigt rote Linien, wenn ich hinzufügen, wenn Bedingung zwischen der .addAction. –

+0

Ich möchte Ihnen vorschlagen, vollständig benutzerdefinierte Ansicht zu verwenden. 1) Sie können jede Ansicht machen, was Sie wollen, 2) Sie werden <20 api-Versionen unterstützen –

Antwort

0

Ich schlage vor, Sie müssen benutzerdefinierte Ansicht für Media Player verwenden. Here Hier ist ein Tutorial, wie Sie das tun können. Kurz: Sie verwenden müssen:

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

individuelle und erweiterte benutzerdefinierte Ansichten bereitzustellen.

Als nächstes müssen Sie Ihre Anmeldung nicht nur aktualisieren, wenn Sie es zu zeigen beginnen, aber jederzeit, wenn Sie Spiel oder Pause Tasten drücken. So müssen Sie ändern:

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

Auf

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

So ähnlich. Hoffe, du hast die Hauptidee.

0

Setzen Sie ein Semikolon vor dem if und dann das Objekt Meldung verwenden, um die Aktion wie folgt einzustellen:

if (mp.isPlaying()) notification.addAction (android.R.drawable.ic_media_play, „Spiele ", pplayIntent); else Benachrichtigung .addAction (android.R.drawable.ic_media_pause, "Pause", pplayIntent);

Ich nehme an, das würde deine Arbeit tun

Verwandte Themen