Create a videoplayer with the LibVLC for android

19,850

Solution 1

Please see the below sample repo I created. It doesn't have all the bells and whistles, but it simply plays any valid video url. It uses a VLC SDK kindly provided by mrmaffen:

https://github.com/gareoke/VLCPlayer

Solution 2

2019 sees introduction of a VLCVideoLayout component that greatly simplifies the code required to embed VLC into android.

https://code.videolan.org/videolan/libvlc-android-samples

The libVLC is provided by the official VideoLAN project hosted on BinTray. See the build.gradle files for the link to the Maven repo and the package name/version.

https://code.videolan.org/videolan/libvlc-android-samples/blob/master/build.gradle#L18 https://code.videolan.org/videolan/libvlc-android-samples/blob/master/java_sample/build.gradle#L34

Solution 3

if someone is still looking for simple vlc player example Please have look at this MyVlcPlayer. It uses minimum code to play a video. To keep this project simple I have not added any video controller.

Share:
19,850
leykan
Author by

leykan

Updated on June 15, 2022

Comments

  • leykan
    leykan about 2 years

    I am trying to creat a video player for an android app with the last LibVLC.

    The problem is that I don't know how this lib works and I can't find sample to help me (as it is say here https://bitbucket.org/edwardcw/libvlc-android-sample)

    So I try on my own to create the video player :

    public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlayer, GestureDetector.OnDoubleTapListener, IDelayController {
    
    private static LibVLC LibVLC() {
        return VLCInstance.get();
    }
    
    private static MediaPlayer MediaPlayer() {
        return VLCInstance.getMainMediaPlayer();
    }
    
    @Override
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Toast.makeText(getApplicationContext(), "Ca start VideoPlayerActivity !!", Toast.LENGTH_SHORT).show();
        if (!VLCInstance.testCompatibleCPU(this)) {
           // exit(RESULT_CANCELED);
            return;
        }
        extras = getIntent().getExtras();
        mUri = extras.getParcelable(PLAY_EXTRA_ITEM_LOCATION);
        Toast.makeText(getApplicationContext(), "Oui ça start le VideoPlayer", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.player_test);
    }
    
    @Override
    public  void onResume() {
        super.onResume();
    
        mSurfaceView = (SurfaceView) findViewById(R.id.player_surface_test);
        setSurfaceLayout(100, 100, 100, 100, 100, 100);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceFrame = (FrameLayout) findViewById(R.id.player_surface_frame_test);
        mSurfaceHolder.addCallback(mSurfaceCallback);
    }
    
    private static class ConfigureSurfaceHolder {
        private final Surface surface;
        private boolean configured;
    
        private ConfigureSurfaceHolder(Surface surface) {
            this.surface = surface;
        }
    }
    
    @Override
    public void setSurfaceLayout(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {
    
        /*if (width * height == 0)
            return;*/
    
        // store video size
        mVideoHeight = height;
        mVideoWidth = width;
        mVideoVisibleHeight = visible_height;
        mVideoVisibleWidth  = visible_width;
        mSarNum = sar_num;
        mSarDen = sar_den;
       Toast.makeText(this, "mVideoHeight = " + mVideoHeight, Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public int configureSurface(Surface surface, final int width, final int height, final int hal) {
        if (AndroidUtil.isICSOrLater() || surface == null)
            return -1;
        if (width * height == 0)
            return 0;
        Log.i(TAG, "configureSurface: " + width +"x"+height);
    
        final ConfigureSurfaceHolder holder = new ConfigureSurfaceHolder(surface);
    
        final Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (mSurface == holder.surface && mSurfaceHolder != null) {
                    if (hal != 0)
                        mSurfaceHolder.setFormat(hal);
                    mSurfaceHolder.setFixedSize(width, height);
                }
                synchronized (holder) {
                    holder.configured = true;
                    holder.notifyAll();
                }
            }
        });
        try {
            synchronized (holder) {
                while (!holder.configured)
                    holder.wait();
            }
        } catch (InterruptedException e) {
            return 0;
        }
        return 1;
    }
    
    @Override
    public void eventHardwareAccelerationError() {
    }
    
    private void startVideo() {
        // LibVLC lib = new LibVLC();
        mMediaPlayer = VLCInstance.getMainMediaPlayer();
        Media media = new Media(VLCInstance.get(), mUri.getPath());
        media.parse();
        Toast.makeText(this, "le media dure : "+media.getDuration(), Toast.LENGTH_SHORT).show();
       // Toast.makeText(this, "le media dure : "+media., Toast.LENGTH_SHORT).show();
        mMediaPlayer.setMedia(media);
        //mMediaPlayer.setVideoTrackEnabled(true);
        //media.release();
       // mMediaPlayer.setEqualizer(VLCOptions.getEqualizer());
       // mMediaPlayer.setVideoTitleDisplay(MediaPlayer.Position.Disable, 0);
        int sw = getWindow().getDecorView().getWidth();
        int sh = getWindow().getDecorView().getHeight();
        VLCInstance.get().setWindowSize(sw, sh);
        mMediaPlayer.play();
        Toast.makeText(this, "le player a une valeur de : "+mMediaPlayer.isPlaying(), Toast.LENGTH_SHORT).show();
       // media.parse();
       // media.release();
       // mMediaPlayer.setMedia(media);
       // mMediaPlayer.play();
    }
    
    private final SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            if(MediaPlayer() != null) {
                width = 100;
                height =100;
                Toast.makeText(getApplicationContext(), "surface width = "+width, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "surface height = "+height, Toast.LENGTH_SHORT).show();
                final Surface newSurface = holder.getSurface();
                if (mSurface != newSurface) {
                    mSurface = newSurface;
                    Toast.makeText(getApplicationContext(), "surfaceChanged: " + mSurface, Toast.LENGTH_SHORT).show();
                    LibVLC().attachSurface(mSurface, VideoPlayerActivity.this);
                    mSurfaceReady = true;
                    startVideo();
                    //mHandler.sendEmptyMessage(1);
                }
            }
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.i(TAG, "surfaceDestroyed");
            if(MediaPlayer() != null) {
                mSurface = null;
                LibVLC().detachSurface();
                mSurfaceReady = false;
            }
        }
    };
    
    private final Handler mHandler = new VideoPlayerHandler(this);
    
    private static class VideoPlayerHandler extends WeakHandler<VideoPlayerActivity> {
        public VideoPlayerHandler(VideoPlayerActivity owner) {
            super(owner);
        }
    
        @Override
        public void handleMessage(Message msg) {
            VideoPlayerActivity activity = getOwner();
            if(activity == null) // WeakReference could be GC'ed early
                return;
    
            switch (msg.what) {
                case 1:
                    activity.startVideo();
                    break;
                default:
                    break;
            }
        }
    };
    
    public static void start(Context context, Uri uri) {
        start(context, uri, null, false, -1);
    }
    
    public static void start(Context context, Uri uri, boolean fromStart) {
        start(context, uri, null, fromStart, -1);
    }
    
    public static void start(Context context, Uri uri, String title) {
        start(context, uri, title, false, -1);
    }
    
    private static void start(Context context, Uri uri, String title, boolean fromStart, int openedPosition) {
        Intent intent = new Intent(context, VideoPlayerActivity.class);
    
    
        intent.setAction(PLAY_FROM_VIDEOGRID);
        intent.putExtra(PLAY_EXTRA_ITEM_LOCATION, uri);
        intent.putExtra(PLAY_EXTRA_ITEM_TITLE, title);
        intent.putExtra(PLAY_EXTRA_FROM_START, fromStart);
        intent.putExtra(PLAY_EXTRA_OPENED_POSITION, openedPosition);
    
        /*if (openedPosition != -1)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);*/
        Toast.makeText(context, "uri = "+uri.toString(), Toast.LENGTH_SHORT).show();
        context.startActivity(intent);  // /!\ start the activity /!\ !!!
    }
    
    @Override
    public void showAudioDelaySetting() {
    
    }
    
    @Override
    public void showSubsDelaySetting() {
    
    }
    
    @Override
    public void endDelaySetting() {
    
    }
    
    @Override
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        return false;
    }
    
    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        return false;
    }
    
    @Override
    public boolean onDoubleTapEvent(MotionEvent motionEvent) {
        return false;
    }
    }
    

    To start the videoPlayerActivity I call start(Context context, Uri uri) and it will creat the activity.

    To resum this code :

    After the oncreat() I call onResum() that will call a mSurfaceHolder.addCallback(mSurfaceCallback); and this callback call startVideo() that should start the video, but nothing start..

    So if someone have a sample of how to create a simple video player with the last LibVLC or an idea where I fail, it would be helpful

  • Manny265
    Manny265 over 8 years
    nice. but how do I access an IP camera from this and pass it authentication details? @gareoke
  • Kishore Jethava
    Kishore Jethava over 7 years
    need video controller, could you please help me
  • Om Infowave Developers
    Om Infowave Developers over 7 years
    @gareoke Can it play video from local storage?