1、一开始以为只要传一个文件的路径就可以了:
String path = "/mnt/sdcard/movie/test.mp4";
mLibVLC.readMedia(pathUri);
查看了例程才发现,必须转换为它的URI格式
String path = "/mnt/sdcard/movie/test.mp4";
String pathUri = LibVLC.getInstance().nativeToURI(path);
mLibVLC.readMedia(pathUri, false);
这里nativeToURI是一个JNI接口,代码在Libvlcjni.c中:
jstring Java_org_videolan_vlc_LibVLC_nativeToURI(JNIEnv *env, jobject thiz, jstring path)
{
jboolean isCopy;
/* Get C string */
const char* psz_path = (*env)->GetStringUTFChars(env, path, &isCopy);
/* Convert the path to URI */
char* psz_location;
if(unlikely( strstr( psz_path, "://" ) ))
psz_location = strdup(psz_path);
else
psz_location = vlc_path2uri(psz_path, "file");
/* Box into jstring */
jstring t = (*env)->NewStringUTF(env, psz_location);
/* Clean up */
(*env)->ReleaseStringUTFChars(env, path, psz_path);
free(psz_location);
return t;
}
2、还是一直都没有出视频,调试例程后才发现,需要增加两个函数
public void setSurfaceSize(int width, int height);
private void changeSurfaceSize() ;
主要代码如下:
package com.lau.vlcdemo;
import org.videolan.vlc.EventManager;
import org.videolan.vlc.LibVLC;
import org.videolan.vlc.LibVlcException;
import org.videolan.vlc.WeakHandler;
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.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Toast;
public class VLCDemoActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {
public final static String TAG = "VLC/VideoPlayerActivity";
private SurfaceView surfaceView = null;
private SurfaceHolder surfaceHolder = null;
private LibVLC mLibVLC = null;
private Button button = null;
private int mVideoHeight;
private int mVideoWidth;
private static final int SURFACE_SIZE = 3;
private static final int SURFACE_BEST_FIT = 0;
private static final int SURFACE_FIT_HORIZONTAL = 1;
private static final int SURFACE_FIT_VERTICAL = 2;
private static final int SURFACE_FILL = 3;
private static final int SURFACE_16_9 = 4;
private static final int SURFACE_4_3 = 5;
private static final int SURFACE_ORIGINAL = 6;
private int mCurrentSize = SURFACE_BEST_FIT;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceView = (SurfaceView) findViewById(R.id.main_sfv);
button = (Button) findViewById(R.id.main_btn);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFormat(PixelFormat.RGBX_8888);
surfaceHolder.addCallback(this);
button.setOnClickListener(this);
try {
LibVLC.useIOMX(getApplicationContext());
mLibVLC = LibVLC.getInstance();
} catch (LibVlcException e) {
e.printStackTrace();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
public void setSurfaceSize(int width, int height) {
// store video size
mVideoHeight = height;
mVideoWidth = width;
Message msg = mHandler.obtainMessage(SURFACE_SIZE);
mHandler.sendMessage(msg);
}
private final Handler mHandler = new VideoPlayerHandler(this);
private static class VideoPlayerHandler extends WeakHandler<VLCDemoActivity> {
public VideoPlayerHandler(VLCDemoActivity owner) {
super(owner);
}
@Override
public void handleMessage(Message msg) {
VLCDemoActivity activity = getOwner();
if(activity == null) // WeakReference could be GC'ed early
return;
switch (msg.what) {
case SURFACE_SIZE:
activity.changeSurfaceSize();
break;
}
}
};
private void changeSurfaceSize() {
// get screen size
int dw = getWindow().getDecorView().getWidth();
int dh = getWindow().getDecorView().getHeight();
// getWindow().getDecorView() doesn't always take orientation into account, we have to correct the values
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
if (dw > dh && isPortrait || dw < dh && !isPortrait) {
int d = dw;
dw = dh;
dh = d;
}
if (dw * dh == 0)
return;
// calculate aspect ratio
double ar = (double) mVideoWidth / (double) mVideoHeight;
// calculate display aspect ratio
double dar = (double) dw / (double) dh;
switch (mCurrentSize) {
case SURFACE_BEST_FIT:
if (dar < ar)
dh = (int) (dw / ar);
else
dw = (int) (dh * ar);
break;
case SURFACE_FIT_HORIZONTAL:
dh = (int) (dw / ar);
break;
case SURFACE_FIT_VERTICAL:
dw = (int) (dh * ar);
break;
case SURFACE_FILL:
break;
case SURFACE_16_9:
ar = 16.0 / 9.0;
if (dar < ar)
dh = (int) (dw / ar);
else
dw = (int) (dh * ar);
break;
case SURFACE_4_3:
ar = 4.0 / 3.0;
if (dar < ar)
dh = (int) (dw / ar);
else
dw = (int) (dh * ar);
break;
case SURFACE_ORIGINAL:
dh = mVideoHeight;
dw = mVideoWidth;
break;
}
surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
LayoutParams lp = surfaceView.getLayoutParams();
lp.width = dw;
lp.height = dh;
surfaceView.setLayoutParams(lp);
surfaceView.invalidate();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mLibVLC.attachSurface(holder.getSurface(), VLCDemoActivity.this, width, height);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mLibVLC.detachSurface();
}
@Override
public void onClick(View v) {
if(mLibVLC != null) {
try {
String path = "/mnt/sdcard/movie/test.mp4";
String pathUri = LibVLC.getInstance().nativeToURI(path);
mLibVLC.readMedia(pathUri, false);
} catch (LibVlcException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
例程资源链接:(共3部分)
http://download.csdn.net/detail/laukaka/4824251