【LOL盒子】英雄详情->配音->【下载】与【播放】模块

在英雄详情->配音页面中,需要实现未下载时,按钮显示下载图标,点击后下载并存储到对应页面,下载完成后,显示播放按钮,点击后可播放。如图:


直接写下载过程的代码吧:

#pragma mark点击下载按钮获取配音
- (void)downloadVoice:(UIButton *)sender{

    
    if (self.currentBtn.tag == 1009)
    {
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //self.commonVoiceList为当前配音列表
        //fileName为当前行对应的音频文件名(fileName ===Ashe_我们必须向前推进.mp3)
        NSString *fileName = [self.commonVoiceList objectAtIndex:sender.tag];
        
        //(pathName ===Ashe)
        NSString *pathName = self.enName;
        
        //文件路径字符串拼接,拼接后自动添加"/"
        //[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]为当前app的document路径
        //filePath为在当前路径下添加一个名为当前英雄的子文件夹
        //(filePath ===/Users/yanyue/Library/Developer/CoreSimulator/Devices/E170452C-72D8-428E-9062-AF61A14758C8/data/Containers/Data/Application/B22FE59F-1B29-42DD-B8AF-ACAD9B9578F1/Documents/Ashe)
        NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject] stringByAppendingPathComponent:pathName];
        
        //当前英雄文件夹里再添加对应配音文件
        NSString *file  = [filePath stringByAppendingPathComponent:fileName];
        
        //判断所要下载文件是否已存在
        if ([fileManager fileExistsAtPath:file])
        {
            //存在,不做任何操作
            NSLog(@"文件存在");
        }else
        {
            //不存在,创建
            NSLog(@"文件不存在");
            //创建对应文件夹
            [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
            
            //URL为配音的对应网址,并转码
            NSURL *URL = [NSURL URLWithString:[[NSString stringWithFormat:@"http://box.dwstatic.com/sounds/%@/%@",self.enName,[self.commonVoiceList objectAtIndex:sender.tag]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            
            //网络请求(网址为URL)
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            
            //创建网络了操作
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            
            //网络操作从URL获取内容
            operation.inputStream  = [NSInputStream inputStreamWithURL:URL];
            
            //网络操作下载内容存在路径file里
            operation.outputStream = [NSOutputStream outputStreamToFileAtPath:file append:NO];
            
            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
            {
               
                //网络获取成功,则重载对应行
                NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag inSection:0];
                [self.voiceTableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationNone];
            }
            failure:^(AFHTTPRequestOperation *operation, NSError *error)
            {
                
            }];
            //发出请求
            [operation start];

            

            
            
        }
        
    }else{
        //其他配音
    }
}



每步的意义都写在注释里了,其他配音类似。

各个路径打印结果如下:





self.commonVoiceList代表了当前TableView显示的是英雄配音列表还是其他配音列表。

对应网址和JSON解析后的内容如下:

工具->英雄->英雄详情->配音

配音列表:
英雄配音:
http://box.dwstatic.com/apiHeroSound.php?hero=Ashe&v=64&OSType=iOS8.1.1&versionName=2.1.2

response:
[
    "Ashe.mp3", 
    "Ashe_你要来几发吗.mp3", 
    "Ashe_好好看着可别到时候不知道自己是怎么死的.mp3", 
    "Ashe_我们必须向前推进.mp3", 
    "Ashe_我瞄的很稳.mp3", 
    "Ashe_明智之选.mp3", 
    "Ashe_正对眉心.mp3"
]

其他配音:
http://box.dwstatic.com/apiHeroSound.php?hero=Common&v=64&OSType=iOS8.1.1&versionName=2.1.2

response:
[
    "Common_一血.mp3", 
    "Common_三杀.mp3", 
    "Common_主宰比赛(六连杀).mp3", 
    "Common_五杀.mp3", 
    "Common_全军出击.mp3", 
    "Common_双杀.mp3", 
    "Common_啦啦啦德玛西亚1.mp3", 
    "Common_啦啦啦德玛西亚2.mp3", 
    "Common_啦啦啦德玛西亚3.mp3", 
    "Common_四杀.mp3", 
    "Common_团灭.mp3", 
    "Common_多杀合音.mp3", 
    "Common_大杀特杀(三连杀).mp3", 
    "Common_失败.mp3", 
    "Common_排位赛背景音乐.mp3", 
    "Common_接近神(七连杀).mp3", 
    "Common_敌军还有30秒到达战场.mp3", 
    "Common_无人能挡(五连杀).mp3", 
    "Common_暴走(四连杀).mp3", 
    "Common_欢迎来到英雄联盟.mp3", 
    "Common_终结击杀.mp3", 
    "Common_胜利.mp3", 
    "Common_超神(八连杀及以上).mp3", 
    "Common_选择英雄音乐.mp3"
]


英雄配音
http://box.dwstatic.com/sounds/Ashe/Ashe_你要来几发吗.mp3?&v=61&OSType=iOS7.0.4&versionName=2.1.1

其他配音
http://box.dwstatic.com/sounds/Common/Common_一血.mp3?&v=61&OSType=iOS7.0.4&versionName=2.1.1</span>

因为下载后图标就变成播放了,于是需要在加载TableView时先判断配音是否存在,若存在,则显示播放按钮,若不存在,则显示下载按钮。

#pragma mark设置tableView每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    <span style="white-space:pre">	</span>UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"heroDetail"];
<span style="white-space:pre">	</span>NSString *voiceStr = [self.commonVoiceList objectAtIndex:indexPath.row];
                
                if (indexPath.row > 0)
                {
                    //字符串查找并替换 找到self.enName_的字符串,替换为空
                    voiceStr = [voiceStr stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@_",self.enName] withString:@""];
                }
                voiceStr = [voiceStr stringByReplacingOccurrencesOfString:@"Common_" withString:@""];
                voiceStr = [voiceStr stringByReplacingOccurrencesOfString:@".mp3" withString:@""];
                
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 275, 16)];
                label.text = voiceStr;
                label.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:label];
                
                NSFileManager *fileManager = [NSFileManager defaultManager];
                NSString *fileName = [self.commonVoiceList objectAtIndex:indexPath.row];
                NSString *pathName = self.enName;
                //文件路径字符串拼接,拼接后自动添加"/"
                NSString *filePath = [DocumentsDirectory stringByAppendingPathComponent:pathName];
                NSString *file     = [filePath stringByAppendingPathComponent:fileName];
                
                if ([fileManager fileExistsAtPath:file])
                {
                    UIButton *playBtn = [[UIButton alloc] initWithFrame:CGRectMake(295, 15, 15, 15)];
                    [playBtn setImage:[UIImage imageNamed:@"icon_game_video"] forState:UIControlStateNormal];
                    [playBtn addTarget:self action:@selector(playVoice:) forControlEvents:UIControlEventTouchUpInside];
                    playBtn.tag = indexPath.row;
                    [cell.contentView addSubview:playBtn];
                }
                else
                {
                    UIButton *downloadBtn = [[UIButton alloc] initWithFrame:CGRectMake(295, 15, 15, 15)];
                    [downloadBtn setImage:[UIImage imageNamed:@"icon_download"] forState:UIControlStateNormal];
                    [downloadBtn addTarget:self action:@selector(downloadVoice:) forControlEvents:UIControlEventTouchUpInside];
                    downloadBtn.tag = indexPath.row;
                    [cell.contentView addSubview:downloadBtn];
                }
            }

    return cell;
}</span>
播放音频引入了第三方框架“AudioPlayer”“AudioStreamer”(改成自动释放)

代码如下:

#pragma mark播放音频
- (void)playVoice:(UIButton *)sender
{
    NSLog(@"play~");

    
        NSString *fileName = [self.commonVoiceList objectAtIndex:sender.tag];
        NSString *pathName = self.enName;
        NSString *filePath = [DocumentsDirectory stringByAppendingPathComponent:pathName];
        NSString *file     = [filePath stringByAppendingPathComponent:fileName];
        
        AudioPlayer *play = [AudioPlayer sharePlayer];
        [play playWithDataSourceType:DataSourceTypeLocal withURLString:file];
    
  

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值