Cocos2d-x VLC Player_cocos vlc-程序员宅基地

技术标签: cocos2d-x  Cocos2d-x  windows  

        由于项目需要用到动态的背景,测试发现,如果用帧序列动画将占用超过1G的内存,而CPU的利用率则一直保持在5%左右,所以想到将动态效果做成视频,循环播放,以作为背景之用。查询之下发现cocos2d-x本身带有一个叫做VideoPlayer的类,但是很可惜,它们只能用在移动平台上,而笔者的项目是基于Windows平台的,所以便在网上搜索实现方法,最终利用VLC Player实现了所需的效果。
        首先需要下载VLC Player播放器,安装之后可以在安装目录下的SDK文件夹内找到头文件、lib、dll。
        其实原理也很简单,就是利用VLC从视频中提取数据流,然后打入buffer中,然后以buffer作为纹理绘制整个画面,代码如下:
#include "VLCPlayer.h"

USING_NS_CC;

void *lock(void *data, void **p_pixels)
{
    auto player = static_cast
    
    
     
     (data);
    *p_pixels = player->m_videobuf;
    return NULL;
}

void unlock(void *data, void *id, void *const *p_pixels)
{
    assert(id == NULL);
}

void display(void *data, void *id)
{
    auto player = static_cast
     
     
      
      (data);
    player->m_readyToShow = true;
    assert(id == NULL);
}

void endReached(const struct libvlc_event_t *event, void *data)
{
    if (libvlc_MediaPlayerEndReached == event->type)
    {
        VLCPlayer *self = (VLCPlayer *)data;
        self->m_isEndReached = true;
    }
}

VLCPlayer::~VLCPlayer()
{
    libvlc_media_player_stop(vlc_player);
    libvlc_media_player_release(vlc_player);
    libvlc_release(vlc);
    free(m_videobuf);
}

VLCPlayer* VLCPlayer::create(Size size)
{
    auto player = new VLCPlayer;
    if (player && player->init(size)) {
        player->autorelease();
    } else {
        CC_SAFE_DELETE(player);
    }
    return player;
}

bool VLCPlayer::init(Size &size)
{
    vlc = libvlc_new(0, NULL);
    vlc_player = libvlc_media_player_new(vlc);
    width = size.width;
    height = size.height;
    m_videobuf = (char *)malloc((width * height) << 2);
    memset(m_videobuf, 0, (width * height) << 2);
    libvlc_video_set_callbacks(vlc_player, lock, unlock, display, this);
    libvlc_video_set_format(vlc_player, "RGBA", width, height, width << 2);
    libvlc_event_attach(libvlc_media_player_event_manager(vlc_player), 
                        libvlc_MediaPlayerEndReached, 
                        endReached,
                        (void *)this);
    Texture2D *texture = new Texture2D();
    texture->initWithData(m_videobuf, 
                          (width * height) << 2, 
                          Texture2D::PixelFormat::RGBA8888, 
                          width, height, size);
    texture->autorelease();
    initWithTexture(texture);
    scheduleUpdate();
    return true;
}

void VLCPlayer::o_play(std::string &path, bool repeat)
{
    m_isEndReached = false;
    m_curMedia = path;
    m_repeat = repeat;
    m_readyToShow = false;
    libvlc_media_t *media = libvlc_media_new_path(vlc, path.c_str());
    libvlc_media_player_set_media(vlc_player, media);
    libvlc_media_release(media);
    libvlc_media_player_play(vlc_player);
}
void VLCPlayer::o_stop(void)    { libvlc_media_player_stop(vlc_player); }
void VLCPlayer::o_resume(void)  { libvlc_media_player_set_pause(vlc_player, 0); }
void VLCPlayer::o_pause(void)   { libvlc_media_player_pause(vlc_player); }
bool VLCPlayer::isPlaying(void) { return (1 == libvlc_media_player_is_playing(vlc_player)) ? true : false; }
bool VLCPlayer::isEndReached()  { return m_isEndReached; }

void VLCPlayer::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    _insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
    if(_insideBounds)
    {
        if (m_readyToShow) {
            m_readyToShow = false;
            Texture2D *texture = new Texture2D();
            texture->initWithData(m_videobuf, 
                                  (width * height) << 2, 
                                  Texture2D::PixelFormat::RGBA8888, 
                                  width, height, Size(width, height));
            texture->autorelease();
            setTexture(texture);
        }
        _quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transform);
        renderer->addCommand(&_quadCommand);
#if CC_SPRITE_DEBUG_DRAW
        _debugDrawNode->clear();
        Vec2 vertices[4] = {
            Vec2( _quad.bl.vertices.x, _quad.bl.vertices.y ),
            Vec2( _quad.br.vertices.x, _quad.br.vertices.y ),
            Vec2( _quad.tr.vertices.x, _quad.tr.vertices.y ),
            Vec2( _quad.tl.vertices.x, _quad.tl.vertices.y ),
        };
        _debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
#endif //CC_SPRITE_DEBUG_DRAW
    }
}

void VLCPlayer::update(float dt)
{
    if (m_repeat && isEndReached()) {
        o_play(m_curMedia);
    }
}
#ifndef __MOVIEPLAYER_H__
#define __MOVIEPLAYER_H__

#include "vlc/vlc.h"
#include "cocos2d.h"

class VLCPlayer : public cocos2d::Sprite
{
public:
    ~VLCPlayer();
    static VLCPlayer* create(cocos2d::Size size);
    bool init(cocos2d::Size &size);

    void o_play(std::string &path, bool repeat = true);
    inline void o_stop(void);
    inline void o_pause(void);
    inline void o_resume(void);
    inline bool isPlaying(void);
    inline bool isEndReached();
    
    virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override;
    virtual void update(float dt) override;

    friend void endReached(const struct libvlc_event_t *event, void *data);
    friend void *lock(void *data, void **p_pixels);
    friend void unlock(void *data, void *id, void *const *p_pixels);
    friend void display(void *data, void *id);

private:
    libvlc_instance_t     *vlc;
    libvlc_media_player_t *vlc_player;
    unsigned int          width;
    unsigned int          height
    char                  *m_videobuf;
    bool                  m_isEndReached;
    std::string           m_curMedia;
    bool                  m_repeat;
    bool                  m_readyToShow;
};
#endif
     
     
    
    
        最后说明一下,可以看到有一些函数以"o_"为开头,怎么会这么奇怪呢~~笔者之前没有加这个前缀,结果resume函数跟Node类的resume函数重名了,根据类的继承规则,Node的resume函数被"遮挡"住了,导致该节点没办法正常的恢复动作,比如,笔者在init方法里调用了scheduleUpdate方法,结果update方法却一直不被调用,查来查去,才发现是Node的resume方法被"遮挡"了,节点没有被正确的恢复。
        当然,还有一种办法就是在VLC类的resume方法里调用Node的resume方法,不过这样做可能导致一些不灵活的情况,所以笔者最终选择了改变函数名称。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/llong568/article/details/43484731

智能推荐

编程人员的不二之选 LEGION Y9000X正式发布_y9000x写代码够用吗-程序员宅基地

文章浏览阅读4.6k次。10月11日,联想集团在全球总部未来中心举行了主题为“解密X空间”的新品发布会,正式发布了LEGIONY9000X笔记本电脑,并公布了“联想个人云存储核心测试用户招募”计划。高性能标压轻薄本 LEGION Y9000X赋能内容创造者“你正在用的笔记本是游戏本还是轻薄本?”通过这样的一个问题,联想中国区消费业务笔记本产品规划总监林林,拉开了“解密X空间”的序幕,带来重磅新品——高性能标..._y9000x写代码够用吗

强化学习(reinforcement learning)教程_强化学习教程-程序员宅基地

文章浏览阅读3.4w次,点赞2次,收藏29次。前一阵研究强化学习,发现中文的资料非常少,实例就更少。于是翻译一篇q学习算法的教程,供需要的人学习。原文链接:http://mnemstudio.org/path-finding-q-learning-tutorial.htm正文:Q学习算法是一种用来解决马尔可夫决策过程中最优化问题的方法。Q学习算法最大的特点是它具有选择瞬时奖励和延迟奖励的能力。在每一步中,agent通过观察状态_强化学习教程

SpringBoot+Vue校园二手书交易平台(源码+论文)_基于vue+springboot的校园二手商品交易网站论文-程序员宅基地

文章浏览阅读81次。后端:Java+SpringBoot前端:Vue数据库:MySQL开发软件:Eclipse、MyEclipse、IDEA都可以运行。_基于vue+springboot的校园二手商品交易网站论文

Chrome 开发者工具各种骚技巧-程序员宅基地

文章浏览阅读231次。对于每个前端从业者来说,除了F5键之外,用的最多的另外一个键就是F12了。今天,大神(@小鱼二)推荐我一个网站,才知道chrome还有各种骚姿势。网站是:umaar.com/dev-tip...

【jeecg-boot】jeecg-boot的一些功能扩展:-程序员宅基地

文章浏览阅读2k次。【jeecg-boot】jeecg-boot的一些功能扩展:_jeecg-boot

gitlab上克隆远程分支到本地(报错-error: RPC failed; curl 18 transfer closed with outstanding read data remaining)_gitlab 18: transfer closed with outstanding read d-程序员宅基地

文章浏览阅读2.7k次。首先确保你的电脑有安装git环境,本人使用的是windows下的git环境。双击桌面图标 的Git Bash 打开窗口修改配置git config --global user.namegit config --global user.email如:git config --global user.name "muzidigbig"git config --glo..._gitlab 18: transfer closed with outstanding read data remaining

随便推点

小帅的七个男友 第一章 未恋先失-程序员宅基地

文章浏览阅读164次。第一章 未恋先失&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;初中时代的我,还是一个单纯的女孩,对于爱情,以为是眼泪制造出来的。我的圈子并不大,只有几个要好的女生,彭老二,周薇,秋毛。彭老二是个大嘴,校园里发生了什么事情她总是最先知道,通过她的大嘴,什么八卦新闻都逃..._操小帅

MATLAB db4小波分解与重构,语音降噪-程序员宅基地

文章浏览阅读4.4k次,点赞2次,收藏23次。小波变换3级分解Mallat图:将带噪语音作为输入信号进行逐级DWT小波分解,并将分解出的低频成分cA3cA_3cA3​与强制置0后的高频成分cD3cD_3cD3​,cD2cD_2cD2​,cD1cD_1cD1​进行小波重构。Demo:clc,clear[x,Fs]= audioread('MUsic_Test.wav');snr = 20; %设定信噪比,单位dbnoise = randn(size(x)); % 用randn函数产生高斯白噪声Nx = length(x_db4小波

安装和配置SNMP(windows10和Linux)--附SNMP客户端工具_snmp工具-程序员宅基地

文章浏览阅读8.3k次,点赞5次,收藏34次。首先需要安装 snmp ,使用下面的命令进行安装安装完毕之后,使用下面的命令查看是否安装成功当命令行显示如图即为安装成功。_snmp工具

如何正确的敲键盘(打字习惯改正)_怎么敲键盘-程序员宅基地

文章浏览阅读6.4k次,点赞5次,收藏40次。练习打字的官网:http://dazi.kukuw.com/关于打字的详细介绍:一个过来人的打字指法纠正之路_怎么敲键盘

网络安全解决方案-程序员宅基地

文章浏览阅读9.6k次,点赞3次,收藏68次。一,网络安全体系结构网络安全体系结构是对网络信息安全基本问题的应对措施的集合,通常由保护,检测,响应和恢复等手段构成。1,网络信息安全的基本问题研究信息安全的困难在于:边界模糊数据安全与平台安全相交叉;存储安全与传输安全相制约;网络安全,应用安全与系统安全共存;集中的安全模式与分权制约安全模式相互竞争等。评估困难安全结构非常复杂,网络层,系统层,应用层的安全设备,安全协议和安全程序构成一个有机的整体,加上安全机制与人的互动性,网络的动态运行带来的易变性,使得评价网络安全性成为极_网络安全解决方案

QGIS在Windows下的编译——QGIS3.28.15 + Qt5.15.3 +CMake3.28.0 + VS2022 ---64位版本_qgis windows编译-程序员宅基地

文章浏览阅读1.2k次,点赞22次,收藏29次。QGIS在Windows下的编译——QGIS3.28.15 + Qt5.15.3 +CMake3.28.0 + VS2022 ---64位版本_qgis windows编译

推荐文章

热门文章

相关标签