Android Open Source - audioplayer Notification Player






From Project

Back to project page audioplayer.

License

The source code is released under:

GNU General Public License

If you think the Android project audioplayer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fr.julien_dumortier.simplemusicplayer.service.notificationplayer;
/* w  w w .j av a  2s. c o m*/
import fr.julien_dumortier.simplemusicplayer.R;

import fr.julien_dumortier.simplemusicplayer.MainActivity;
import fr.julien_dumortier.simplemusicplayer.playerview.IPlayerViewController;
import fr.julien_dumortier.simplemusicplayer.service.MusicPlayerService;
import fr.julien_dumortier.simplemusicplayer.service.playlist.MusicMediaPlayer;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

@SuppressWarnings("deprecation")
public class NotificationPlayer implements IPlayerViewController {

  public static final String LAST = "last", NEXT = "next", PLAY = "play", DELETE = "delete";
  private MusicPlayerService mContext;
  private RemoteViews mRemoteViews;
  private NotificationManager mNotificationManager;
  private boolean isPlayingMode;
  
  public NotificationPlayer(MusicPlayerService context) {
    try {
    mContext = context;
    
    mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_layout); 
    
    Intent intent = new Intent(context, NotificationManagerService.class); 
    
    intent.setAction(LAST);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 1);
    mRemoteViews.setOnClickPendingIntent(R.id.button_last, pendingIntent);

    Intent intent2 = new Intent(context, NotificationManagerService.class); 
    intent2.setAction(NEXT);
    pendingIntent = PendingIntent.getService(context, 1, intent2, 1);
    mRemoteViews.setOnClickPendingIntent(R.id.button_next, pendingIntent);

    Intent intent3 = new Intent(context, NotificationManagerService.class); 
    intent3.setAction(PLAY);
    pendingIntent = PendingIntent.getService(context, 2, intent3, 1);
    mRemoteViews.setOnClickPendingIntent(R.id.button_play, pendingIntent);
    
    BitmapDrawable dr = (BitmapDrawable) mContext.getMusicMediaPlayer().getCurrentTrack().getPicture();
        
    mRemoteViews.setImageViewBitmap(R.id.image, dr.getBitmap());
    mRemoteViews.setTextViewText(R.id.mini_track_name, mContext.getMusicMediaPlayer().getCurrentTrack().getTrackName());
    mRemoteViews.setTextViewText(R.id.mini_artist_name, mContext.getMusicMediaPlayer().getCurrentTrack().getArtistName());

    
  //  Intent intentResumeActivity = new Intent(context, MainActivity.class);
    PendingIntent resumePlayerIntent = PendingIntent.getActivity(context, 3, new Intent(context, MainActivity.class)
        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_CANCEL_CURRENT);// PendingIntent.getActivity(mContext, 0, intentResumeActivity, 0);
    
    Intent intentDeleteActivity = new Intent(context, NotificationManagerService.class);
    intentDeleteActivity.setAction(DELETE);
    PendingIntent deletePlayerIntent = PendingIntent.getActivity(mContext, 4, intentDeleteActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_CANCEL_CURRENT);
    
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setContent(mRemoteViews).setSmallIcon(R.drawable.ic_casque_mini).setContentIntent(resumePlayerIntent).setDeleteIntent(deletePlayerIntent);
    
    mNotificationManager = 
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    mNotificationManager.notify(20, mBuilder.getNotification());
    NotificationManagerService.mMusicMediaPlayer = mContext.getMusicMediaPlayer();
    mContext.getMusicMediaPlayer().addViewControler(this);
    }catch(Exception e){}
  }

  public void initView(MusicMediaPlayer mediaPlayer) {
    try{
      if(mediaPlayer.getCurrentTrack()!=null) {
        mRemoteViews.setTextViewText(R.id.mini_track_name, mediaPlayer.getCurrentTrack().getTrackName());
        mRemoteViews.setTextViewText(R.id.mini_artist_name, mediaPlayer.getCurrentTrack().getArtistName());
        BitmapDrawable dr = (BitmapDrawable) mediaPlayer.getCurrentTrack().getPicture();
        mRemoteViews.setImageViewBitmap(R.id.image, dr.getBitmap());
        if(mediaPlayer.isPlaying()) 
          mRemoteViews.setImageViewResource(R.id.button_play, R.drawable.pause_button);
        else
          mRemoteViews.setImageViewResource(R.id.button_play, R.drawable.play_button);
        isPlayingMode = mediaPlayer.isPlaying();
        
        Intent intentResumeActivity = new Intent(mContext, MainActivity.class);
        PendingIntent resumePlayerIntent = PendingIntent.getActivity(mContext, 5, intentResumeActivity, 0);
        
        Intent intent = new Intent(mContext, NotificationManagerService.class);       
        intent.setAction(DELETE);
        PendingIntent pendingIntent = PendingIntent.getService(mContext, 6, intent, 1);
        
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
        .setContent(mRemoteViews).setSmallIcon(R.drawable.ic_casque_mini)
        .setContentIntent(resumePlayerIntent).setDeleteIntent(pendingIntent);
        
        mNotificationManager = 
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
        mNotificationManager.notify(20, mBuilder.getNotification());
        NotificationManagerService.mMusicMediaPlayer = mediaPlayer;
      }
    }catch(Exception e){}
  }

  public void play() {
    try {
      if(!isPlayingMode && mContext.getMusicMediaPlayer().getCurrentTrack()!=null)
      {
        isPlayingMode = true;
        mContext.getMusicMediaPlayer().resume();
        mRemoteViews.setImageViewResource(R.id.button_play, R.drawable.pause_button);
  
        Intent intentResumeActivity = new Intent(mContext, MainActivity.class);
        PendingIntent resumePlayerIntent = PendingIntent.getActivity(mContext, 7, intentResumeActivity, 0);
        
        Intent intent = new Intent(mContext, NotificationManagerService.class);       
        intent.setAction(DELETE);
        PendingIntent pendingIntent = PendingIntent.getService(mContext, 8, intent, 1);
        
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
        .setContent(mRemoteViews).setSmallIcon(R.drawable.ic_casque_mini)
        .setContentIntent(resumePlayerIntent).setDeleteIntent(pendingIntent);
        
        mNotificationManager = 
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
        mNotificationManager.notify(20, mBuilder.getNotification());
      }
    }catch(Exception e){}
  }

  public void pause() {
    try {
      if(isPlayingMode)
      {
        isPlayingMode = false;
        mContext.getMusicMediaPlayer().pause();
        mRemoteViews.setImageViewResource(R.id.button_play, R.drawable.play_button);
  
        Intent intentResumeActivity = new Intent(mContext, MainActivity.class);
        PendingIntent resumePlayerIntent = PendingIntent.getActivity(mContext, 9, intentResumeActivity, 0);
        
        Intent intent = new Intent(mContext, NotificationManagerService.class);       
        intent.setAction(DELETE);
        PendingIntent pendingIntent = PendingIntent.getService(mContext, 10, intent, 1);
        
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
        .setContent(mRemoteViews).setSmallIcon(R.drawable.ic_casque_mini)
        .setContentIntent(resumePlayerIntent).setDeleteIntent(pendingIntent);
        
        mNotificationManager = 
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
        mNotificationManager.notify(20, mBuilder.getNotification());
      }
    }catch(Exception e){}
  }

  public void clearView() {
    dismiss();
  }

  public void stop() {}

  public void setPercentTime(int percentPlayed) {}

  public void setTimePlayed(long timePlayed, long restTimePlayed) {}

  public void setRepeat(int repeatMode) {}

  public void setRandom(boolean b) {}

  public void dismiss() {
    try {
      mContext.getMusicMediaPlayer().removeViewController(this);
      mNotificationManager.cancel(20);
    }catch(Exception e){}
  }
}




Java Source Code List

fr.julien_dumortier.simplemusicplayer.DevInfo.java
fr.julien_dumortier.simplemusicplayer.DrawerListAdapter.java
fr.julien_dumortier.simplemusicplayer.IMusicMediaControllerGetter.java
fr.julien_dumortier.simplemusicplayer.MainActivity.java
fr.julien_dumortier.simplemusicplayer.browsemanager.BrowseCover.java
fr.julien_dumortier.simplemusicplayer.browsemanager.BrowseManager.java
fr.julien_dumortier.simplemusicplayer.coveralbumeditor.CoverAlbumEditor.java
fr.julien_dumortier.simplemusicplayer.coveralbumeditor.CoverAlbumGetter.java
fr.julien_dumortier.simplemusicplayer.coveralbumeditor.PromptCovers.java
fr.julien_dumortier.simplemusicplayer.equalizer.EqualizerActivity.java
fr.julien_dumortier.simplemusicplayer.equalizer.EqualizerConfigurationDataBase.java
fr.julien_dumortier.simplemusicplayer.equalizer.VerticalSeekBar.java
fr.julien_dumortier.simplemusicplayer.equalizer.preset.Preset.java
fr.julien_dumortier.simplemusicplayer.item.Album.java
fr.julien_dumortier.simplemusicplayer.item.Artist.java
fr.julien_dumortier.simplemusicplayer.item.Genre.java
fr.julien_dumortier.simplemusicplayer.item.Item.java
fr.julien_dumortier.simplemusicplayer.item.Playlist.java
fr.julien_dumortier.simplemusicplayer.item.Track.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.AlbumAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.ArtistAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.CurrentPlayAnimationManager.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.GenreAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.ItemAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.ItemClicManager.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.OrganisableTrackAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.PlayListAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.TrackAdapter.java
fr.julien_dumortier.simplemusicplayer.notifynews.NotifyNews.java
fr.julien_dumortier.simplemusicplayer.playerview.FullScreenPlayerViewController.java
fr.julien_dumortier.simplemusicplayer.playerview.IPlayerViewController.java
fr.julien_dumortier.simplemusicplayer.playerview.MiniPlayerViewController.java
fr.julien_dumortier.simplemusicplayer.playerview.TimeFormater.java
fr.julien_dumortier.simplemusicplayer.playlistview.MultiSelectManager.java
fr.julien_dumortier.simplemusicplayer.playlistview.PlayListManagerActivity.java
fr.julien_dumortier.simplemusicplayer.playlistview.SaveManagerView.java
fr.julien_dumortier.simplemusicplayer.playlistview.SavedPlaylistManager.java
fr.julien_dumortier.simplemusicplayer.service.BackgroundBinder.java
fr.julien_dumortier.simplemusicplayer.service.MusicPlayerService.java
fr.julien_dumortier.simplemusicplayer.service.bluetoohactionmanager.BluetoothReceiver.java
fr.julien_dumortier.simplemusicplayer.service.notificationplayer.NotificationManagerService.java
fr.julien_dumortier.simplemusicplayer.service.notificationplayer.NotificationPlayer.java
fr.julien_dumortier.simplemusicplayer.service.phonestatemanager.PhoneCallStateManager.java
fr.julien_dumortier.simplemusicplayer.service.playlist.EndOfPlayListException.java
fr.julien_dumortier.simplemusicplayer.service.playlist.IMediaPlayerController.java
fr.julien_dumortier.simplemusicplayer.service.playlist.MusicMediaPlayer.java
fr.julien_dumortier.simplemusicplayer.service.playlist.PlayListManager.java
fr.julien_dumortier.simplemusicplayer.thememanager.ColorPreferences.java
fr.julien_dumortier.simplemusicplayer.thememanager.DisplayManager2.java
fr.julien_dumortier.simplemusicplayer.thememanager.ManageDisplayView.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptActionByType.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptActionPlaylist.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmAddInNew.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmDeletePlaylist.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmExit.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmReplacePlaylist.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptDefineCoverAlbum.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptGetTextName.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptMoveTrack.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptPlaylistName.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptShowText.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptSuppressConfirm.java
fr.julien_dumortier.simplemusicplayer.widget.AudioPlayerApplication.java
fr.julien_dumortier.simplemusicplayer.widget.AudioPlayerWidgetProvider.java
fr.julien_dumortier.simplemusicplayer.widget.AudioPlayerWidgetViewController.java
fr.julien_dumortier.simplemusicplayer.widget.WidgetView.java