LVC 播放器提供buffer自定义读写的方法,但是并没有开放到Java层,并且关于此类的应用也并不多;不过用到的也很少;下面就简单介绍应用的方法:
在LVC 中的lvc\lib\media.c中已经提供其应用接口,接口如下:
libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
libvlc_media_open_cb open_cb,
libvlc_media_read_cb read_cb,
libvlc_media_seek_cb seek_cb,
libvlc_media_close_cb close_cb,
void *opaque);
简单的使用方法就是在lvclibjni_media.c 实现其jni的接口;方法如下:
static int opaque;
static bool isFifoReadStop = false;
int media_open_cb(void *opaque, void **datap,uint64_t *sizep)
{
<pre name="code" class="objc">printf("%s", __FUNCTION__);
return 0;}ssize_t media_read_cb(void *opaque, unsigned char *buf, size_t len){ int readLen = 0; int oneLen = 0; void *p_read_buffer = NULL; readLen = 0;while(isFifoReadStop == false){ oneLen = getFifobuf(&p_read_buffer,len - readLen); if(oneLen > 0) { memcpy(buf+readLen,p_read_buffer,oneLen); readLen += oneLen; } if(readLen >= len) { break; }} if(isFifoReadStop != false) {
printf("media_read_cb stop read data\n");
return -1; } //printf("read:%ld byte ~ %ld byte\n",readLen,len); if((readLen <= 0) ||(readLen > len)) { printf("media_read_cb no data or stop read len = %d get len = %d\n",readLen,len); readLen = 0; return -1; } return (ssize_t)readLen;}void media_close_cb(void *opaque){
//printf("%s", __FUNCTION__);
}int media_seek_cb(void *opaque, uint64_t offset){ return 0;}voidJava_org_videolan_libvlc_Media_nativeNewFromBufferCallBack(JNIEnv *env, jobject thiz, jobject libVlc){ vlcjni_object *p_obj; int fd = (*env)->GetIntField(env, jfd, fields.FileDescriptor.descriptorID); if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); fd = -1; } if (fd == -1) { throw_IllegalArgumentException(env, "fd invalid"); return; } p_obj = VLCJniObject_newFromJavaLibVlc(env, thiz, libVlc); if (!p_obj) return; p_obj->u.p_m = libvlc_media_new_callbacks(p_obj->p_libvlc, media_open_cb, media_read_cb, media_close_cb, media_seek_cb, &opaque); Media_nativeNewCommon(env, thiz, p_obj);}
在media.java中实现调用
private native void nativeNewFromBufferCallBack(LibVLC libVLC);
public Media(LibVLC libVLC) {
nativeNewFromBufferCallBack(libVLC);
}
如果,不希望在jni中实现数据的读取接口,则可以在Java中实现,在回调函数中调用Java类中实现的接口即可,此次略过;
实现的另外一种方法,是不通过更改media的jni,而通过更改mediaplay jni来更改media的参数即可;
方法如下:
#include <android/log.h>
#include <vlc/libvlc.h>
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
#include <vlc/libvlc_events.h>
#include <vlc_common.h>
#include <vlc_input.h>
#include <vlc_meta.h>
#include <vlc_playlist.h> /* For the preparser */
#include <vlc_url.h>
#include "libvlc_internal.h"
#include "media_internal.h"
#include "Fifobuf.h"
....
void
Java_org_videolan_libvlc_MediaPlayer_nativeSetMediaBufferCallback(JNIEnv *env,
jobject thiz,
jobject jmedia)
{
libvlc_media_t *p_m = NULL;
vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
if (!p_obj)
return;
if (jmedia)
{
vlcjni_object *p_m_obj = VLCJniObject_getInstance(env, jmedia);
if (!p_m_obj)
return;
p_m = p_m_obj->u.p_m;
LOGI("register media buffer callback\n");
input_item_AddOpaque(p_m->p_input_item, "imem-data", opaque);
input_item_AddOpaque(p_m->p_input_item, "imem-open", media_open_cb);
input_item_AddOpaque(p_m->p_input_item, "imem-read", media_read_cb);
input_item_AddOpaque(p_m->p_input_item, "imem-seek", media_seek_cb);
input_item_AddOpaque(p_m->p_input_item, "imem-close", media_close_cb);
}
}
Java实现如下:
private native void nativeSetMediaBufferCallback(Media media);
public void setSetMediaBufferCallback(Media media)
{
nativeSetMediaBufferCallback(media);
}
调用方法不变,但是参数需要更改为:
String playUrl = "imem://";
Uri uri = Uri.parse(playUrl);
if(mMedia != null)
{
mMedia.release();
}
mMedia = new Media(libvlc, uri);
mVlcPlayer.setSetMediaBufferCallback(mMedia);
mVlcPlayer.setMedia(mMedia);