Пример #1
0
func (this *musicManager) DownloadSmallCoverImage(musicId int, callback DownloadProgressCallback) {
	musicInfo, err := model.MusicModelInstance().QueryMusicById(musicId)
	if musicInfo == nil {
		var stop bool
		callback(nil, errors.New("对应的musicID无音乐"), &stop)
		return
	} else if err != nil {
		var stop bool
		callback(nil, err, &stop)
		return
	}
	fmt.Println("Download small cover image: " + musicInfo.MusicName)
	switch musicInfo.SourceType {
	case element.BaiduMusicSourceType:
		downloadInfo := &DownloadElement{}
		downloadInfo.DownloadType = DownloadTypeSingle
		downloadInfo.DownloadContent = musicInfo.SmallCoverImagePath
		downloadInfo.DownloadPath = cache.CacheManagerInstance().GenerateCacheFile()
		downloadInfo.Progress = func(content []byte, err error, stop *bool) {
			callback(content, err, stop)
		}
		downloadInfo.CompleteSignal = make(chan bool)
		DownloadManagerInstance().AddDownloadInfoIntoQueue(downloadInfo)
		<-downloadInfo.CompleteSignal
		cache.CacheManagerInstance().DeleteCacheFile(downloadInfo.DownloadPath)
	case element.LocalMusicSourceType:
		content, err := this.readLocalFile(musicInfo.SmallCoverImagePath)
		var stop bool
		callback(content, err, &stop)
	}
}
Пример #2
0
func (this *musicManager) DownloadMusic(musicId int, callback DownloadProgressCallback) {
	musicInfo, err := model.MusicModelInstance().QueryMusicById(musicId)
	if musicInfo == nil {
		var stop bool
		callback(nil, errors.New("对应的musicID无音乐"), &stop)
		return
	} else if err != nil {
		var stop bool
		callback(nil, err, &stop)
		return
	}
	fmt.Println("Download Music: " + musicInfo.MusicName)
	switch musicInfo.SourceType {
	case element.BaiduMusicSourceType:
		downloadInfo := &DownloadElement{}
		downloadInfo.Complete = func(musicInfo *element.MusicInfo) {
			fmt.Println("Download Success!")
			// 下载完成,将文件拷贝到本地目录,然后删除缓存目录
			// musicInfo.MusicPath = musicInfo.DownloadPath
			localPath := fmt.Sprintf("./resource/%s/", musicInfo.MusicUUID)
			cache.CacheManagerInstance().MoveCacheFile(downloadInfo.DownloadPath, localPath)
			this.WriteMusicInfoIntoDB(musicInfo)
		}
		downloadInfo.Failed = func(musicInfo *element.MusicInfo, err error) {
			cache.CacheManagerInstance().DeleteCacheFile(downloadInfo.DownloadPath)
		}
		musicInfo.MusicUUID = this.createUUID()
		downloadInfo.DownloadPath = fmt.Sprintf("/tmp/%s", musicInfo.MusicUUID)
		downloadInfo.DownloadType = DownloadTypeMusic
		downloadInfo.DownloadContent = musicInfo
		downloadInfo.Progress = func(content []byte, err error, stop *bool) {
			callback(content, err, stop)
		}
		downloadInfo.CompleteSignal = make(chan bool)
		DownloadManagerInstance().AddDownloadInfoIntoQueue(downloadInfo)
		<-downloadInfo.CompleteSignal
	case element.LocalMusicSourceType:
		content, err := this.readLocalFile(musicInfo.MusicPath)
		var stop bool
		callback(content, err, &stop)
	}
}