大佬教程收集整理的这篇文章主要介绍了cocos2d-x 下载网络视频、音乐保存到沙盒,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>
#include "extensions/assets-manager/Downloader.h"
#include "cocos2d.h"
USING_NS_Cc;
typedef std::function<void(int progress)> myProgressCallBACk;
class httpDownLoadingHelper : public Node
{
public:
virtual bool init();
CREATE_FUNC(httpDownLoadingHelper);
static httpDownLoadingHelper *geTinstance();
public:
//video
void DownloadVideoToSandBox(std::String videoNameStirng,std::String urlLink);
//music
void DownloadMusicToSandBox(std::String musicNameStirng,std::String urlLink);
private:
//video
void errorCallBACk(const cocos2d::extension::Downloader::Error& error);
void VideoprogressCallBACk(double @R_552_10586@lToDownload,double nowDownloaded,const std::String& url,const std::String& customId);
void VideosuccessCallBACk(const std::String& url,const std::String& path,const std::String& customId);
//music
void MusicprogressCallBACk(double @R_552_10586@lToDownload,const std::String& customId);
void MusicerrorCallBACk(const cocos2d::extension::Downloader::Error& error);
void MusicsuccessCallBACk(const std::String& url,const std::String& customId);
private:
std::shared_ptr<cocos2d::extension::Downloader> _myVideoDownloader;
std::shared_ptr<cocos2d::extension::Downloader> _mymusicDownloader;
};
@H_696_1@httpDownLoadingHelper.cpp
#include "httpDownLoadingHelper.hpp"
#include "XMLUIVideoPlayer.hpp"
#include "XMLUIMusicPlayer.hpp"
static httpDownLoadingHelper *g_instance = NULL;
//static int g_imageCount = 1;
httpDownLoadingHelper *httpDownLoadingHelper::geTinstance()
{
if (g_instance == NULL) {
g_instance = httpDownLoadingHelper::create();
g_instance->retain();
}
return g_instance;
}
bool httpDownLoadingHelper::init()
{
if ( !Node::init() )
{
return false;
}
return true;
}
void httpDownLoadingHelper::DownloadVideoToSandBox(std::string videoNameStirng,std::string urlLink)
{
_myVideoDownloader = std::shared_ptr<cocos2d::extension::Downloader>(new cocos2d::extension::Downloader);
// std::shared_ptr<cocos2d::extension::Downloader> myDownloader(new cocos2d::extension::Downloader);
_myVideoDownloader->setErrorCallBACk(std::bind(&httpDownLoadingHelper::errorCallBACk,g_instance,std::placeholders::_1));
_myVideoDownloader->setProgressCallBACk(std::bind(&httpDownLoadingHelper::VideoprogressCallBACk,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4));
_myVideoDownloader->setsuccessCallBACk(std::bind(&httpDownLoadingHelper::VideosuccessCallBACk,std::placeholders::_3));
_myVideoDownloader->setConnectionTimeout(5);
std::string path = FileUtils::geTinstance()->getWritablePath() + videoNameStirng;
std::string remote = urlLink;
cocos2d::log("Downloading '%s' into '%s'",remote.c_str(),path.c_str());
_myVideoDownloader->downloadAsync(remote,path,videoNameStirng);
}
void httpDownLoadingHelper::errorCallBACk(const cocos2d::extension::Downloader::Error& error)
{
cocos2d::log("error downloading: %s - %s",error.url.c_str(),error.message.c_str());
}
void httpDownLoadingHelper::VideoprogressCallBACk(double @R_552_10586@lToDownload,double nowDownloaded,const std::string& url,const std::string& customId)
{
cocos2d::log("%s",customId.c_str());
cocos2d::log("video(%s) download progress: %d%% - %s",customId.c_str(),(int)((nowDownloaded/@R_552_10586@lToDownload)*100),url.c_str());
XMLUIVideoPlayer::SetDownloadProgress({
@H_549_26@mAX(1,static_cast<float>((int)((nowDownloaded/@R_552_10586@lToDownload)*100))),customId
});
}
void httpDownLoadingHelper::VideosuccessCallBACk(const std::string& url,const std::string& path,const std::string& customId)
{
cocos2d::log("download finished: %s",path.c_str());
}
//music
void httpDownLoadingHelper::DownloadMusicToSandBox(std::string musicNameStirng,std::string urlLink)
{
_mymusicDownloader = std::shared_ptr<cocos2d::extension::Downloader>(new cocos2d::extension::Downloader);
_mymusicDownloader->setErrorCallBACk(std::bind(&httpDownLoadingHelper::MusicerrorCallBACk,std::placeholders::_1));
_mymusicDownloader->setProgressCallBACk(std::bind(&httpDownLoadingHelper::MusicprogressCallBACk,std::placeholders::_4));
_mymusicDownloader->setsuccessCallBACk(std::bind(&httpDownLoadingHelper::MusicsuccessCallBACk,std::placeholders::_3));
_mymusicDownloader->setConnectionTimeout(5);
std::string path = FileUtils::geTinstance()->getWritablePath() + musicNameStirng;
std::string remote = urlLink;
cocos2d::log("Downloading '%s' into '%s'",path.c_str());
_mymusicDownloader->downloadAsync(remote,musicNameStirng);
}
void httpDownLoadingHelper::MusicprogressCallBACk(double @R_552_10586@lToDownload,const std::string& customId)
{
cocos2d::log("music(%s) download progress: %d%% - %s",url.c_str());
XMLUIMusicPlayer::MusicSetDownloadProgress({
@H_549_26@mAX(1,customId
});
}
void httpDownLoadingHelper::MusicerrorCallBACk(const cocos2d::extension::Downloader::Error& error)
{
cocos2d::log("error downloading: %s - %s",error.message.c_str());
}
void httpDownLoadingHelper::MusicsuccessCallBACk(const std::string& url,path.c_str());
}
以上是大佬教程为你收集整理的cocos2d-x 下载网络视频、音乐保存到沙盒全部内容,希望文章能够帮你解决cocos2d-x 下载网络视频、音乐保存到沙盒所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。