/** Start playing the previously prepared {@link org.tomahawk.libtomahawk.collection.Track} */
 @Override
 public void start() throws IllegalStateException {
   Log.d(TAG, "start()");
   if (mLibVLC != null) {
     if (!mLibVLC.isPlaying()) {
       mLibVLC.play();
     }
   }
 }
 /** Pause playing the current {@link org.tomahawk.libtomahawk.collection.Track} */
 @Override
 public void pause() throws IllegalStateException {
   Log.d(TAG, "pause()");
   if (mLibVLC != null) {
     if (mLibVLC.isPlaying()) {
       mLibVLC.pause();
     }
   }
 }
  public static LibVLC getLibVlcInstance() throws LibVlcException {
    LibVLC instance = LibVLC.getExistingInstance();
    if (instance == null) {
      Thread.setDefaultUncaughtExceptionHandler(new VlcCrashHandler());

      instance = LibVLC.getInstance();
      Context context = VLCApplication.getAppContext();
      SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
      updateLibVlcSettings(pref);
      instance.init(context);
    }
    return instance;
  }
 public VLCRendererBuilder(Context context, Uri uri, Properties props)
     throws ExoPlaybackException {
   this.context = context;
   this.uri = LibVLC.PathToURI(uri.toString());
   this.vlc = ExoVlcUtil.getVLC(context);
   this.props = props;
 }
  private VLCMediaPlayer() {
    // Initialize and register Receiver
    if (mVLCMediaPlayerReceiver == null) {
      mVLCMediaPlayerReceiver = new VLCMediaPlayerReceiver();
      IntentFilter intentFilter = new IntentFilter(PipeLine.PIPELINE_URLTRANSLATIONREPORTED);
      TomahawkApp.getContext().registerReceiver(mVLCMediaPlayerReceiver, intentFilter);
    }

    try {
      mLibVLC = LibVLC.getInstance();
      mLibVLC.init(TomahawkApp.getContext());
      mLibVLC.setHardwareAcceleration(LibVLC.HW_ACCELERATION_DISABLED);
    } catch (LibVlcException e) {
      Log.e(TAG, "<init>: Failed to initialize LibVLC: " + e.getLocalizedMessage());
    }
  }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
          if (!fromUser) return;

          equalizer[0] = progress - 20;
          if (libVlc != null && button.isChecked()) libVlc.setEqualizer(equalizer);
        }
 /** @return the current track position */
 @Override
 public int getPosition() {
   if (mLibVLC != null && mPreparedQuery != null) {
     return (int) mLibVLC.getTime();
   } else {
     return 0;
   }
 }
 /** Seek to the given playback position (in ms) */
 @Override
 public void seekTo(int msec) throws IllegalStateException {
   Log.d(TAG, "seekTo()");
   if (mLibVLC != null
       && mPreparedQuery != null
       && !PipeLine.PLUGINNAME_BEATSMUSIC.equals(
           mPreparedQuery.getPreferredTrackResult().getResolvedBy().getId())) {
     mLibVLC.setTime(msec);
   }
 }
 /** Prepare the given url */
 private MediaPlayerInterface prepare(Query query) {
   mPreparedQuery = null;
   mPreparingQuery = query;
   release();
   if (mLibVLC == null) {
     return null;
   }
   Result result = query.getPreferredTrackResult();
   if (PipeLine.PLUGINNAME_BEATSMUSIC.equals(result.getResolvedBy().getId())
       || PipeLine.PLUGINNAME_GMUSIC.equals(result.getResolvedBy().getId())) {
     if (mTranslatedUrls.get(result) == null) {
       ((ScriptResolver) result.getResolvedBy()).getStreamUrl(result);
     } else {
       mLibVLC.playMRL(LibVLC.PathToURI(mTranslatedUrls.get(result)));
       onPrepared(null);
     }
   } else {
     mLibVLC.playMRL(LibVLC.PathToURI(result.getPath()));
     onPrepared(null);
   }
   return this;
 }
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
          if (libVlc == null) return;
          float[] preset = libVlc.getPreset(pos);
          if (preset == null) return;

          equalizer = preset;
          preamp.setProgress((int) equalizer[0] + 20);
          for (int i = 0; i < equalizer.length - 1; ++i) {
            EqualizerBar bar = (EqualizerBar) bands_layout.getChildAt(i);
            bar.setValue(equalizer[i + 1]);
          }
        }
  private void createPlayer(int buffer) {
    try {

      // Create a new media player
      ArrayList<String> options = new ArrayList<>(20);
      options.add("--network-caching=" + buffer);
      options.add("--no-audio-time-stretch");
      options.add("--androidwindow-chroma");
      options.add("RV32");
      //            options.add("--subsdec-encoding");
      //            options.add("Universal (UTF-8)");
      options.add("-v");

      mLibVLC = new LibVLC(options);
      TvApp.getApplication().getLogger().Info("Network buffer set to " + buffer);
      LibVLC.setOnNativeCrashListener(
          new LibVLC.OnNativeCrashListener() {
            @Override
            public void onNativeCrash() {
              new Exception().printStackTrace();
              Utils.PutCustomAcraData();
              ACRA.getErrorReporter().handleException(new Exception("Error in LibVLC"), false);
              mActivity.finish();
              android.os.Process.killProcess(android.os.Process.myPid());
              System.exit(10);
            }
          });

      mVlcPlayer = new org.videolan.libvlc.MediaPlayer(mLibVLC);
      SharedPreferences prefs = TvApp.getApplication().getPrefs();
      String audioOption =
          Utils.isFireTv() && !Utils.is50()
              ? "1"
              : prefs.getString("pref_audio_option", "0"); // force compatible audio on Fire 4.2
      mVlcPlayer.setAudioOutput(
          "0".equals(audioOption) ? "android_audiotrack" : "opensles_android");
      mVlcPlayer.setAudioOutputDevice("hdmi");

      mSurfaceHolder.addCallback(mSurfaceCallback);
      mVlcPlayer.setEventListener(mVlcHandler);
      mVlcPlayer.getVLCVout().addCallback(this);

    } catch (Exception e) {
      TvApp.getApplication().getLogger().ErrorException("Error creating VLC player", e);
      Utils.showToast(
          TvApp.getApplication(),
          TvApp.getApplication().getString(R.string.msg_video_playback_error));
    }
  }
  public static void updateLibVlcSettings(SharedPreferences pref) {
    LibVLC instance = LibVLC.getExistingInstance();
    if (instance == null) return;

    instance.setSubtitlesEncoding(pref.getString("subtitle_text_encoding", ""));
    instance.setTimeStretching(pref.getBoolean("enable_time_stretching_audio", false));
    instance.setFrameSkip(pref.getBoolean("enable_frame_skip", true));
    instance.setChroma(pref.getString("chroma_format", ""));
    instance.setVerboseMode(pref.getBoolean("enable_verbose_mode", true));

    if (pref.getBoolean("equalizer_enabled", false))
      instance.setEqualizer(getFloatArray(pref, "equalizer_values"));

    int aout;
    try {
      aout = Integer.parseInt(pref.getString("aout", "-1"));
    } catch (NumberFormatException nfe) {
      aout = -1;
    }
    int vout;
    try {
      vout = Integer.parseInt(pref.getString("vout", "-1"));
    } catch (NumberFormatException nfe) {
      vout = -1;
    }
    int deblocking;
    try {
      deblocking = Integer.parseInt(pref.getString("deblocking", "-1"));
    } catch (NumberFormatException nfe) {
      deblocking = -1;
    }
    int hardwareAcceleration;
    try {
      hardwareAcceleration = Integer.parseInt(pref.getString("hardware_acceleration", "-1"));
    } catch (NumberFormatException nfe) {
      hardwareAcceleration = -1;
    }
    int networkCaching = pref.getInt("network_caching_value", 0);
    if (networkCaching > 60000) networkCaching = 60000;
    else if (networkCaching < 0) networkCaching = 0;
    instance.setAout(aout);
    instance.setVout(vout);
    instance.setDeblocking(deblocking);
    instance.setNetworkCaching(networkCaching);
    instance.setHardwareAcceleration(hardwareAcceleration);
  }
 @Override
 public boolean isPlaying(Query query) {
   return mLibVLC != null && isPrepared(query) && mLibVLC.isPlaying();
 }
 @Override
 public void setVolume(float leftVolume, float rightVolume) {
   if (mLibVLC != null) {
     mLibVLC.setVolume((int) (leftVolume + rightVolume * 50));
   }
 }
  private void fillViews() {
    SharedPreferences preferences =
        PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
    float[] bands = null;
    String[] presets = null;
    try {
      libVlc = VLCInstance.getLibVlcInstance();
      bands = libVlc.getBands();
      presets = libVlc.getPresets();
      if (equalizer == null) equalizer = Preferences.getFloatArray(preferences, "equalizer_values");
      if (equalizer == null) equalizer = new float[bands.length + 1];
    } catch (LibVlcException e) {
      e.printStackTrace();
      return;
    }

    // on/off
    button.setChecked(libVlc.getEqualizer() != null);
    button.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (libVlc == null) return;
            libVlc.setEqualizer(isChecked ? equalizer : null);
          }
        });

    // presets
    equalizer_presets.setAdapter(
        new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_spinner_dropdown_item, presets));

    // Set the default selection asynchronously to prevent a layout initialization bug.
    final int equalizer_preset_pref = preferences.getInt("equalizer_preset", 0);
    equalizer_presets.post(
        new Runnable() {
          @Override
          public void run() {
            equalizer_presets.setSelection(equalizer_preset_pref, false);
            equalizer_presets.setOnItemSelectedListener(mPresetListener);
          }
        });

    // preamp
    preamp.setMax(40);
    preamp.setProgress((int) equalizer[0] + 20);
    preamp.setOnSeekBarChangeListener(mPreampListener);

    // bands
    for (int i = 0; i < bands.length; i++) {
      float band = bands[i];

      EqualizerBar bar = new EqualizerBar(getActivity(), band);
      bar.setValue(equalizer[i + 1]);
      bar.setListener(new BandListener(i + 1));

      bands_layout.addView(bar);
      LinearLayout.LayoutParams params =
          new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1);
      bar.setLayoutParams(params);
    }
  }
 @Override
 public Object getItem(int position) {
   return LibVLC.PathToURI(
       Environment.getExternalStorageDirectory().getAbsolutePath() + '/' + mFiles.get(position));
 }