예제 #1
0
  // Play file
  public void play(int pos) {
    if (tracks == null) return;
    startUpdateProgress();

    this.position = pos;

    // Play File
    String path = "";
    if (tracks != null && tracks.size() > position) {
      currentTrack = tracks.get(position);
      path = currentTrack.getPath();
    }
    if (path.equals("")) {
      onPlayError("empty");
      return;
    }

    BASS.BASS_StreamFree(chan);
    if ((chan = BASS.BASS_StreamCreateFile(path, 0, 0, 0)) == 0) {
      onPlayError(path);

      // Stop Foreground
      stopForeground(true);

      return;
    }
    // Play File
    int result = BASS.BASS_ChannelSetSync(chan, BASS.BASS_SYNC_END, 0, EndSync, 0);
    // ByteBuffer byteBuffer = (ByteBuffer)BASS.BASS_ChannelGetTags(chan, BASS.BASS_TAG_ID3V2);

    BASS.BASS_ChannelPlay(chan, false);

    // Update Properties
    this.duration =
        BASS.BASS_ChannelBytes2Seconds(chan, BASS.BASS_ChannelGetLength(chan, BASS.BASS_POS_BYTE));
    this.progress = 0.0;

    // Notify Activity
    if (activity != null) {
      activity.onFileLoaded(
          currentTrack,
          this.duration,
          currentTrack.getArtist(),
          currentTrack.getTitle(),
          position,
          currentTrack.getAlbumId());
      activity.onProgressChanged(progress);
      activity.onUpdatePlayPause();
    }

    // Start foreground
    fireNotification();

    // Remote control
    if (Build.VERSION.SDK_INT >= 14 && remoteControlClient != null) {
      updateRemoteControl();
    }
  }
예제 #2
0
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  void updateRemoteControl() {
    updateRemoteControlState(RemoteControlClient.PLAYSTATE_PLAYING);
    remoteControlClient.setTransportControlFlags(
        RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);

    // Update the remote controls
    Bitmap bitmap =
        MediaUtils.getArtworkQuick(this, currentTrack, screenWidth / 2, screenWidth / 2);
    int redTop = 0, greenTop = 0, blueTop = 0, pixelsTop = 0;
    int redBtm = 0, greenBtm = 0, blueBtm = 0, pixelsBtm = 0;
    int colorTop = 0, colorBtm = 0;

    if (bitmap != null) {
      int w = bitmap.getWidth();
      int h = bitmap.getHeight();
      for (int i = 0; i < w; i++) {
        try {
          colorTop = bitmap.getPixel(i, 0);
          redTop += Color.red(colorTop);
          greenTop += Color.green(colorTop);
          blueTop += Color.blue(colorTop);
          pixelsTop += 1;

          colorBtm = bitmap.getPixel(i, h - 1);
          redBtm += Color.red(colorBtm);
          greenBtm += Color.green(colorBtm);
          blueBtm += Color.blue(colorBtm);
          pixelsBtm += 1;
        } catch (Exception e) {
        }
      }
      if (pixelsTop > 0 && pixelsBtm > 0) {
        colorTop =
            Color.rgb(redTop / pixelsTop, greenTop / pixelsTop, blueTop / pixelsTop); // EDE7E9
        colorBtm = Color.rgb(redBtm / pixelsBtm, greenBtm / pixelsBtm, blueBtm / pixelsBtm);
        Shader shader =
            new LinearGradient(w / 2, 0, w / 2, h, colorTop, colorBtm, Shader.TileMode.CLAMP);
        Bitmap bitmapBgr = Bitmap.createBitmap(w, screenHeight / 2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapBgr);
        Paint paint = new Paint();
        paint.setShader(shader);
        canvas.drawRect(0, 0, w, screenHeight / 2, paint);
        canvas.drawBitmap(bitmap, 0, (screenHeight / 2 - screenWidth / 2) / 2, null);
        bitmap.recycle();
        bitmap = bitmapBgr;
      }
    } else {
      // create random color
      bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
      Random rnd = new Random();
      bitmap.eraseColor(Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
    }
    remoteControlClient
        .editMetadata(true)
        .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, currentTrack.getArtist())
        .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, currentTrack.getAlbum())
        .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, currentTrack.getTitle())
        .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, currentTrack.getDuration())
        .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, bitmap)
        .apply();
  }