@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (mVlcPlayer != null) { mVlcPlayer.getVLCVout().detachViews(); mVlcPlayer.getVLCVout().setVideoView(mSurfaceView); if (hasSubtitlesSurface) mVlcPlayer.getVLCVout().setSubtitlesView(mSubtitlesSurface); mVlcPlayer.getVLCVout().attachViews(); TvApp.getApplication().getLogger().Debug("Surface attached"); mSurfaceReady = true; } }
private void releasePlayer() { if (mVlcPlayer == null) return; mVlcPlayer.setEventListener(null); mVlcPlayer.stop(); mVlcPlayer.getVLCVout().detachViews(); mVlcPlayer.release(); mLibVLC = null; mVlcPlayer = null; mSurfaceView.setKeepScreenOn(false); }
public void setAudioMode() { if (!nativeMode) { 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"); } }
public void play() { if (nativeMode) { mVideoView.start(); mVideoView.setKeepScreenOn(true); } else { mVlcPlayer.play(); mSurfaceView.setKeepScreenOn(true); // work around losing audio when pausing bug int sav = mVlcPlayer.getAudioTrack(); mVlcPlayer.setAudioTrack(-1); mVlcPlayer.setAudioTrack(sav); // } }
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 void stopPlayback() { if (nativeMode) { mVideoView.stopPlayback(); } else { mVlcPlayer.stop(); } }
public void start() { if (nativeMode) { mVideoView.start(); mVideoView.setKeepScreenOn(true); normalWidth = mVideoView.getLayoutParams().width; normalHeight = mVideoView.getLayoutParams().height; } else { if (!mSurfaceReady) { TvApp.getApplication().getLogger().Error("Attempt to play before surface ready"); return; } if (!mVlcPlayer.isPlaying()) { mVlcPlayer.play(); } } }
public void setAudioDelay(long value) { if (!nativeMode && mVlcPlayer != null) { if (!mVlcPlayer.setAudioDelay(value * 1000)) { TvApp.getApplication().getLogger().Error("Error setting audio delay"); } else { TvApp.getApplication().getLogger().Info("Audio delay set to " + value); } } }
public void pause() { if (nativeMode) { mVideoView.pause(); mVideoView.setKeepScreenOn(false); } else { mVlcPlayer.pause(); mSurfaceView.setKeepScreenOn(false); } }
public long seekTo(long pos) { if (nativeMode) { Long intPos = pos; mVideoView.seekTo(intPos.intValue()); return pos; } else { if (mVlcPlayer == null || !mVlcPlayer.isSeekable()) return -1; mForcedTime = pos; mLastTime = mVlcPlayer.getTime(); TvApp.getApplication().getLogger().Info("VLC length in seek is: " + mVlcPlayer.getLength()); try { if (getDuration() > 0) mVlcPlayer.setPosition((float) pos / getDuration()); else mVlcPlayer.setTime(pos); return pos; } catch (Exception e) { TvApp.getApplication().getLogger().ErrorException("Error seeking in VLC", e); Utils.showToast(mActivity, "Unable to seek"); return -1; } } }
public long getCurrentPosition() { if (nativeMode) return mVideoView.getCurrentPosition(); long time = mVlcPlayer.getTime(); if (mForcedTime != -1 && mLastTime != -1) { /* XXX: After a seek, mLibVLC.getTime can return the position before or after * the seek position. Therefore we return mForcedTime in order to avoid the seekBar * to move between seek position and the actual position. * We have to wait for a valid position (that is after the seek position). * to re-init mLastTime and mForcedTime to -1 and return the actual position. */ if (mLastTime > mForcedTime) { if (time <= mLastTime && time > mForcedTime) mLastTime = mForcedTime = -1; } else { if (time > mForcedTime) mLastTime = mForcedTime = -1; } } return mForcedTime == -1 ? time : mForcedTime; }
public void setVideoPath(String path) { mCurrentVideoPath = path; if (nativeMode) { try { mVideoView.setVideoPath(path); } catch (IllegalStateException e) { TvApp.getApplication() .getLogger() .ErrorException("Unable to set video path. Probably backing out.", e); } } else { mSurfaceHolder.setKeepScreenOn(true); mCurrentMedia = new Media(mLibVLC, Uri.parse(path)); mCurrentMedia.parse(); mVlcPlayer.setMedia(mCurrentMedia); mCurrentMedia.release(); } }
public boolean setSubtitleTrack(int index, List<MediaStream> allStreams) { if (!nativeMode) { // find the relative order of our sub index within the sub tracks in VLC int vlcIndex = 1; // start at 1 to account for "disabled" for (MediaStream stream : allStreams) { if (stream.getType() == MediaStreamType.Subtitle && !stream.getIsExternal()) { if (stream.getIndex() == index) { break; } vlcIndex++; } } org.videolan.libvlc.MediaPlayer.TrackDescription vlcSub; try { vlcSub = getSubtitleTracks()[vlcIndex]; } catch (IndexOutOfBoundsException e) { TvApp.getApplication() .getLogger() .Error("Could not locate subtitle with index %s in vlc track info", index); return false; } catch (NullPointerException e) { TvApp.getApplication() .getLogger() .Error( "No subtitle tracks found in player trying to set subtitle with index %s in vlc track info", index); return false; } TvApp.getApplication().getLogger().Info("Setting Vlc sub to " + vlcSub.name); return mVlcPlayer.setSpuTrack(vlcSub.id); } return false; }
public boolean isPlaying() { return nativeMode ? mVideoView.isPlaying() : mVlcPlayer != null && mVlcPlayer.isPlaying(); }
public void disableSubs() { if (!nativeMode && mVlcPlayer != null) mVlcPlayer.setSpuTrack(-1); }
public boolean canSeek() { return nativeMode || mVlcPlayer.isSeekable(); }
public org.videolan.libvlc.MediaPlayer.TrackDescription[] getSubtitleTracks() { return nativeMode ? null : mVlcPlayer.getSpuTracks(); }
public long getAudioDelay() { return mVlcPlayer != null ? mVlcPlayer.getAudioDelay() / 1000 : 0; }
public void setCompatibleAudio() { if (!nativeMode) { mVlcPlayer.setAudioOutput("opensles_android"); mVlcPlayer.setAudioOutputDevice("hdmi"); } }
public void setPlaySpeed(float speed) { if (!nativeMode) mVlcPlayer.setRate(speed); }
public void setAudioTrack(int id) { if (!nativeMode) mVlcPlayer.setAudioTrack(id); }
public int getAudioTrack() { return nativeMode ? -1 : mVlcPlayer.getAudioTrack(); }
public boolean addSubtitleTrack(String path) { return !nativeMode && mVlcPlayer.setSubtitleFile(path); }
public long getDuration() { return nativeMode ? mVideoView.getDuration() : mVlcPlayer.getLength() > 0 ? mVlcPlayer.getLength() : mMetaDuration; }
@Override public void surfaceDestroyed(SurfaceHolder holder) { if (mVlcPlayer != null) mVlcPlayer.getVLCVout().detachViews(); mSurfaceReady = false; }
public void updateMeta(MediaPlayer mediaPlayer) { final Media media = mediaPlayer.getMedia(); if (media == null) return; updateMeta(media); media.release(); }