VLC SDK在VS2010中的配置及简单使用举例

1.        从http://www.videolan.org/vlc/download-windows.html下载vlc-2.2.0-win32.7z,解压缩;

2.        新建一个VLCtest控制台工程;

3.        将/vlc-2.2.0-win32/vlc-2.2.0/sdk/include添加到工程属性中,C/C++ -->General --> Additional Include Directories;

4.        将/vlc-2.2.0-win32/vlc-2.2.0/中的libvlc.lib、libvlc.dll、libvlccore.lib、libvlccore.dll及plugins目录拷贝到工程执行目录下;

5.        VLCtest工程中各个文件的代码为:

stdafx.h:

#pragma once

#include "targetver.h"

#include 
#include 
#include 

#include 

stdafx.cpp:

#include "stdafx.h"

#ifdef _DEBUG
	#pragma comment(lib, "../../../lib/dbg/x86_vc10/libvlc.lib")
	#pragma comment(lib, "../../../lib/dbg/x86_vc10/libvlccore.lib")
#else
	#pragma comment(lib, "../../../lib/rel/x86_vc10/libvlc.lib")
	#pragma comment(lib, "../../../lib/rel/x86_vc10/libvlccore.lib")
#endif

VLCtest.cpp:

#include "stdafx.h"
#include 

using namespace std;

void test1()
{	
	libvlc_instance_t* vlc_ins    = NULL;
	libvlc_media_player_t* vlc_player = NULL;
	libvlc_media_t* vlc_media  = NULL;

	const char* vlc_args[] = {"-I", "dummy", "--ignore-config", "--extraintf=logger", "--verbose=2"};  
	
	//创建一个VLC实例,release版本在此处会crash
	vlc_ins = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);

	if (vlc_ins != NULL) {
		//创建一个VLC播放器
		vlc_player = libvlc_media_player_new(vlc_ins);

		if (vlc_player != NULL) {
			//通过文件路径创建一个媒体实例
			vlc_media = libvlc_media_new_path(vlc_ins, "f:\\photo\\WP_20140405_002.mp4");

			if (vlc_media != NULL) {
				//解析媒体实例
				libvlc_media_parse(vlc_media);

				//获取媒体文件的播放长度,返回ms
				libvlc_time_t duration = libvlc_media_get_duration(vlc_media);

				//此处是获取媒体包含多个的视频和音频轨以及其它类型的轨道信息
				libvlc_media_track_info_t* media_tracks = NULL;
				int trackCount = libvlc_media_get_tracks_info(vlc_media, &media_tracks);

				//这里是释放内存,但我测试的时候会有问题,还没仔细研究是为何
				//free(media_tracks);  //crash?

				//把打开的媒体文件设置给播放器
				libvlc_media_player_set_media(vlc_player, vlc_media);

				// 因为是windows系统,所以需要设置一个HWND给播放器作为窗口,这里就直接使用桌面窗口,这里仅是测试
				libvlc_media_player_set_hwnd(vlc_player, ::GetDesktopWindow());

				//开始播放视频
				libvlc_media_player_play(vlc_player);

				//这里仅是为了打印一些信息出来,20秒后退出
				time_t last_time = time(NULL);

				while ((time(NULL) < (last_time + 20))) {
					Sleep(10);
					//获取当前播放位置
					libvlc_time_t play_time = libvlc_media_player_get_time(vlc_player);
					printf("playing time : %lld ms\r", (__int64)(play_time));

					//获取媒体的状态
					//libvlc_state_t media_state = libvlc_media_get_state(vlc_media);
					//printf("\nmedia state : %d\n", (int)(media_state));
				}	
				libvlc_media_player_stop(vlc_player);//停止
				libvlc_media_release(vlc_media);//释放
			}
			libvlc_media_player_release(vlc_player);//释放
		}
		libvlc_release(vlc_ins);//释放
	}
}

void test2()
{
	libvlc_instance_t* inst;
	libvlc_media_player_t* mp;
	libvlc_media_t* m;
	
	//init vlc modules, should be done only once
	inst = libvlc_new(0, NULL);

	//Create a new item
	m = libvlc_media_new_path(inst, "f:\\photo\\WP_20140405_002.mp4");

	//Create a media player playing environment
	mp = libvlc_media_player_new_from_media(m);
	libvlc_media_parse(m);
	libvlc_time_t duration = libvlc_media_get_duration(m);

	//No need to keep the media now
	libvlc_media_release(m);
#if 0
	//This is a non working code that show how to hooks into a window,
	//if we have a window around
	//libvlc_drawable_t drawable = xdrawable;
	//or on windows
	void *drawable = &g_hwnd;
	libvlc_media_player_set_hwnd(mp, drawable);
#endif
	// play the media_player
	libvlc_media_player_play(mp);

	Sleep((DWORD)duration); //Let it play a bit

	//Stop playing
	libvlc_media_player_stop(mp);

	// Free the media_player
	libvlc_media_player_release(mp);
	libvlc_release(inst);
}

int main(int argc, char* argv[])
{
	//test1();
	test2();

	cout<<"ok!"<

6.    经测试,test1()和test2()在Debug模式下均能正常运行,在Release下均会crash,均是执行到libvlc_new函数处有问题,具体原因还不清楚,后面会进一步分析。

说明:在Release下,将Linker->Optimization->References中的值由Yes (/OPT:REF)修改为No (/OPT:NOREF),就可以在Release下执行了。

以上测试代码来自于:

1.      http://blog.sina.com.cn/s/blog_62949ff40101edml.html 

2.      http://www.360doc.com/content/14/0309/10/2036337_358957305.shtml 


你可能感兴趣的:(VLC SDK在VS2010中的配置及简单使用举例)