public static void togglePartyShuffle() {
   if (sService != null) {
     int shuffle = getCurrentShuffleMode();
     try {
       if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
         sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
       } else {
         sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
       }
     } catch (RemoteException ex) {
     }
   }
 }
 /*
  * Returns true if a file is currently opened for playback (regardless
  * of whether it's playing or paused).
  */
 public static boolean isMusicLoaded() {
   if (MusicUtils.sService != null) {
     try {
       return sService.getPath() != null;
     } catch (RemoteException ex) {
     }
   }
   return false;
 }
 public static long getCurrentAudioId() {
   if (MusicUtils.sService != null) {
     try {
       return sService.getAudioId();
     } catch (RemoteException ex) {
     }
   }
   return -1;
 }
 public static long getCurrentAlbumId() {
   if (sService != null) {
     try {
       return sService.getAlbumId();
     } catch (RemoteException ex) {
     }
   }
   return -1;
 }
 public static int getCurrentShuffleMode() {
   int mode = MediaPlaybackService.SHUFFLE_NONE;
   if (sService != null) {
     try {
       mode = sService.getShuffleMode();
     } catch (RemoteException ex) {
     }
   }
   return mode;
 }
 public static void initAlbumArtCache() {
   try {
     int id = sService.getMediaMountedCount();
     if (id != sArtCacheId) {
       clearAlbumArtCache();
       sArtCacheId = id;
     }
   } catch (RemoteException e) {
     e.printStackTrace();
   }
 }
 public static void addToCurrentPlaylist(Context context, long[] list) {
   if (sService == null) {
     return;
   }
   try {
     sService.enqueue(list, MediaPlaybackService.LAST);
     String message =
         context
             .getResources()
             .getQuantityString(
                 R.plurals.NNNtrackstoplaylist, list.length, Integer.valueOf(list.length));
     Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
   } catch (RemoteException ex) {
   }
 }
 private static void playAll(Context context, long[] list, int position, boolean force_shuffle) {
   if (list.length == 0 || sService == null) {
     Log.d("MusicUtils", "attempt to play empty song list");
     // Don't try to play empty playlists. Nothing good will come of it.
     String message = context.getString(R.string.emptyplaylist, list.length);
     Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
     return;
   }
   try {
     if (force_shuffle) {
       sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NORMAL);
     }
     long curid = sService.getAudioId();
     int curpos = sService.getQueuePosition();
     if (position != -1 && curpos == position && curid == list[position]) {
       // The selected file is the file that's currently playing;
       // figure out if we need to restart with a new playlist,
       // or just launch the playback activity.
       long[] playlist = sService.getQueue();
       if (Arrays.equals(list, playlist)) {
         // we don't need to set a new list, but we should resume playback if needed
         sService.play();
         return; // the 'finally' block will still run
       }
     }
     if (position < 0) {
       position = 0;
     }
     sService.open(list, force_shuffle ? -1 : position);
     sService.play();
   } catch (RemoteException ex) {
   } finally {
     Intent intent =
         new Intent("com.android.music.PLAYBACK_VIEWER").setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     context.startActivity(intent);
   }
 }
 public static void clearQueue() {
   try {
     sService.removeTracks(0, Integer.MAX_VALUE);
   } catch (RemoteException ex) {
   }
 }
  public static void deleteTracks(Context context, long[] list) {

    String[] cols =
        new String[] {
          MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID
        };
    StringBuilder where = new StringBuilder();
    where.append(MediaStore.Audio.Media._ID + " IN (");
    for (int i = 0; i < list.length; i++) {
      where.append(list[i]);
      if (i < list.length - 1) {
        where.append(",");
      }
    }
    where.append(")");
    Cursor c =
        query(
            context,
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            cols,
            where.toString(),
            null,
            null);

    if (c != null) {

      // step 1: remove selected tracks from the current playlist, as well
      // as from the album art cache
      try {
        c.moveToFirst();
        while (!c.isAfterLast()) {
          // remove from current playlist
          long id = c.getLong(0);
          sService.removeTrack(id);
          // remove from album art cache
          long artIndex = c.getLong(2);
          synchronized (sArtCache) {
            sArtCache.remove(artIndex);
          }
          c.moveToNext();
        }
      } catch (RemoteException ex) {
      }

      // step 2: remove selected tracks from the database
      context
          .getContentResolver()
          .delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where.toString(), null);

      // step 3: remove files from card
      c.moveToFirst();
      while (!c.isAfterLast()) {
        String name = c.getString(1);
        File f = new File(name);
        try { // File.delete can throw a security exception
          if (!f.delete()) {
            // I'm not sure if we'd ever get here (deletion would
            // have to fail, but no exception thrown)
            Log.e("MusicUtils", "Failed to delete file " + name);
          }
          c.moveToNext();
        } catch (SecurityException ex) {
          c.moveToNext();
        }
      }
      c.close();
    }

    String message =
        context
            .getResources()
            .getQuantityString(
                R.plurals.NNNtracksdeleted, list.length, Integer.valueOf(list.length));

    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    // We deleted a number of tracks, which could affect any number of things
    // in the media content domain, so update everything.
    context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
  }