2015.12.19
在此前完成的一个APP中有下载网络视频、音乐的客户需求,我自己封装了一个类。希望能够帮到大家。我使用的cocos2d-x的版本是3.4。
Downloader
这是cocos2d封装的一个用于下载网络数据的类,支持同步以及异步下载。更详细的类方法大家可以进去看。
使用时加上头文件。#include “extensions/assets-manager/Downloader.h”
实际调用:
第一个参数:音乐名的字符串,例如”song.mp3”
第二个参数:连接地址,例如”www.fffffffffffff.mp3”
HttpDownLoadingHelper::getInstance()->DownloadMusicToSandBox(“song.mp3”,“www.fffffffffffff.mp3”);
以下将我封装的HttpDownLoadingHelper类源码贴上,下载音乐和下载视频的用法是一样。
HttpDownLoadingHelper.h
#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 totalToDownload,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 totalToDownload,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;
};
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 totalToDownload,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/totalToDownload)*100),url.c_str());
XMLUIVideoPlayer::SetDownloadProgress({
MAX(1,static_cast<float>((int)((nowDownloaded/totalToDownload)*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 totalToDownload,const std::string& customId)
{
cocos2d::log("music(%s) download progress: %d%% - %s",url.c_str());
XMLUIMusicPlayer::MusicSetDownloadProgress({
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());
}