1

我正在尝试制作一种可以播放所有主要视频文件格式的小型视频播放器。起初我尝试了 Android VideoView,但我发现它只能播放 .3gp 和 .mp4 文件。所以我实现了 LibVLC 库来播放视频文件。我使用这个链接来学习如何在我的代码中实现 libVLC。

它正在播放所有视频格式,但主要问题是播放几秒钟后视频开始滞后并在屏幕上显示像素化框。

虽然当我播放 .mp4 文件或 SD(标准清晰度)视频时,它工作得非常好。问题在于播放 720p 1080p 或更高的高清视频有时甚至 480p 视频滞后。特别是在 .mkv 文件的情况下。

这是我的代码:

player.xml ~ 布局文件

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:background="@android:color/black">

        <SurfaceView
            android:id="@+id/surface_view1"
            android:layout_width="match_parent"
            android:layout_centerInParent="true"
            android:visibility="visible"
            android:layout_height="match_parent" />

    </RelativeLayout>

Player.java ~ Java 文件

    package com.example.admin.myapplication;


import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import org.videolan.libvlc.EventHandler;
import org.videolan.libvlc.IVideoPlayer;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaList;

import java.lang.ref.WeakReference;


/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 */
public class Player extends AppCompatActivity implements IVideoPlayer {

    String path = null;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    Surface surface;

    LibVLC mLibVLC;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        // path is the path of the video file
        path = getIntent().getStringExtra("PATH");
        path = "file:///" + path;
        surfaceView = (SurfaceView) findViewById(R.id.surface_view1);
        surfaceHolder = surfaceView.getHolder();

        try{
            mLibVLC = new LibVLC();
            mLibVLC.setAout(mLibVLC.AOUT_AUDIOTRACK);
            mLibVLC.setVout(mLibVLC.VOUT_ANDROID_SURFACE);
            mLibVLC.setHardwareAcceleration(LibVLC.HW_ACCELERATION_AUTOMATIC);

            mLibVLC.init(getApplicationContext());
        }
        catch(Exception e){e.printStackTrace();}

        surface = surfaceHolder.getSurface();

        mLibVLC.attachSurface(surface, Player.this);
        mLibVLC.playMRL(path);

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLibVLC.stop();
    }

    @Override
    public void setSurfaceLayout(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {

    }
    @Override
    public int configureSurface(Surface surface, int width, int height, int hal) {
        return -1;
    }
    @Override
    public void eventHardwareAccelerationError() {

    }
}
4

0 回答 0