-仿迅雷播放器教程 -- 封装VLC (5)

    虽然上个教程中10多行代码便做出了一个播放器,但如果加上快进快退等功能的话,代码都会挤在一团,阅读性很差,所以这个版本将对VLC进行封装,由于第一个教程已经进行了ffmpeg的封装,所以这里将利用它的框架CAVPlayer,由于这些封装代码都很简洁明了,所以将不再对细节一一介绍,直接给出封装好的代码。
#include <string>
#include "AVPlayer.h"
 
int main(int argc, char* argv[])
{
    std::string strCMD;
    CAVPlayer   cAVPlayer;
 
    cAVPlayer.Play("G:\\media test\\music\\amani.wma");
 
    while(true)
    {
        std::cin >> strCMD;
 
        if (! strCMD.compare("Quit"))
        {
            break;
        }
    }
 
    return 0;
}

    运行之后,输入Quit即可退出。这里有效代码仅仅一行,是不是比上一个教程又简洁很多呢~O(∩_∩)O~

    其中CAVPlayer的部分封装如下:
class CAVPlayer
{
public:
    CAVPlayer(void);
    ~CAVPlayer(void);
 
    void Init();                            // 初始化
    void Close();                           // 关闭文件及清理
 
    bool Play(const std::string &strPath);  // 播放路径为strPath的文件
    void PlayNext();                        // 播放下一个文件
    void PlayPre();                         // 播放上一个文件
 
    void Stop ();                           // 停止
    void Pause();                           // 暂停
 
    void Volume(int nVol);                  // 音量设置为nVol
    void VolumeIncrease();                  // 音量增大
    void VolumeReduce();                    // 音量减小
 
    void SeekTo(int nPos);                  // 跳到指定位置nPos
    void SeekForward();                     // 快进
    void SeekBackward();                    // 快退
......
}

    CAVPlayer目前只实现了Play函数,其他函数将会在后面的教程一次性实现,届时不再一一列出,这里给出一个框架,加快大家对VLC的认识。 

(代码在win7系统的VC6、VS2008、VS2010、VS2012下均编译通过;在XP系统的VS2008、VS2010下均编译通过;在gcc下应该也能编译通过,不过没有用gcc测试。
由于第一个教程给出的是VS2010的代码,有些小伙伴没安装这个编译器,导致很多问题,所以这个教程给出的代码是VC6的,可以升级到任何编译器)
  (由于界面等工作还没开始,所以下一个教程可能会隔得久一点,Alberl会尽快直播~)
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//创建初始化播放器资源 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] extern public static IntPtr libvlc_new(int argc, IntPtr argv); //创建播放器实例 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance); // 释放libvlc实例 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_release(IntPtr libvlc_instance); //获取库版本信息 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern String libvlc_get_version(); // 从视频来源(例如Url)构建一个libvlc_meida RTSP [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path); // 从本地文件路径构建一个libvlc_media rtsp串流不适合调用此接口 // [MarshalAs(UnmanagedType.LPStr)] string path [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path); /// <summary> /// 影片长度 /// </summary> /// <param name="libvlc_instance"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr libvlc_media_player_get_length(IntPtr libvlc_media_player); //释放对象 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_release(IntPtr libvlc_media_inst); // 将视频(libvlc_media)绑定到播放器上 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media); //创建(libvlc_media)播放窗口 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_new_from_media(IntPtr libvlc_media_player); // 设置图像输出的窗口 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable); //播放 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer); //暂停 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer); //停止 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer); // 解析视频资源的媒体信息(如时长等) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_parse(IntPtr libvlc_media); // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media); // 当前播放的时间 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer); // 设置播放位置(拖动) [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time); /// <summary> /// 抓图 /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <param name="num">经典0</param> /// <param name="filePath">完整路径,文件名英文或下划线开头</param> /// <param name="i_width"></param> /// <param name="i_height"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_video_take_snapshot(IntPtr libvlc_mediaplayer, uint num, IntPtr filePath, uint i_width, uint i_height); //media player release [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer); // 获取音量 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player); //设置音量 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume); // 设置全屏 [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen); /// <summary> ///判断是否可以录像 /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <returns></returns> //Can the media player record the current media? [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern bool libvlc_media_player_is_recordable(IntPtr libvlc_media_player); /// <summary> ///判断是否在录像 /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern bool libvlc_media_player_is_recording(IntPtr libvlc_media_player); /// <summary> /// 录像开始 /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <param name="psz_PathFilename">保存路径+文件名(d:\\record (将在D盘根目录保存为record.mp4))</param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_media_player_record_start(IntPtr libvlc_media_player, IntPtr psz_PathFilename); /// <summary> /// 录像停止 /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)] public static extern int libvlc_media_player_record_stop(IntPtr libvlc_media_player);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值