流媒体在线播放和断点续传下载到本地demo


下面将演示流媒体在线播放和断点续传下载到本地的功能,由于代码比较多,所以会分成几期来写
方便总结和让大家看得清楚。

本文利用 table view 组合 cell 和实现 流媒体的显示和下载、暂停、继续等功能。

cell代码:

@interface MusicCell : UITableViewCell <UIAlertViewDelegate,UIAlertViewDelegate>{
    IBOutlet UILabel *musicName;
    IBOutlet UILabel *musicSize;
    IBOutlet UIButton *musicDown;
    FileModel *fileInfo;
}

@property(nonatomic,retain)IBOutlet UILabel *musicName;
@property(nonatomic,retain)IBOutlet UILabel *musicSize;
@property(nonatomic,retain)IBOutlet UIButton *musicDown;
@property(nonatomic,retain)FileModel *fileInfo;

-(IBAction)downMusic:(id)sender;//点击下载开始下载
@end
//
//  MusicCell.m
//  Hayate
//
//  Created by 韩 国翔 on 11-12-2.
//  Copyright 2011年 山东海天软件学院. All rights reserved.
//

#import "MusicCell.h"


@implementation MusicCell

@synthesize musicName;
@synthesize musicSize;
@synthesize musicDown;
@synthesize fileInfo;

- (void)dealloc
{
    [fileInfo release];
    [musicDown release];
    [musicSize release];
    [musicName release];
    [super dealloc];
}

-(void)downMusic:(id)sender
{    
    FileModel *selectFileInfo=self.fileInfo;
    
    //选择点击的行
//    UITableView *tableView=(UITableView *)[sender superview];
//    NSLog(@"%d",[fileInfo.fileID integerValue]);
//    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:[fileInfo.fileID integerValue] inSection:0];
//    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
    
    //因为是重新下载,则说明肯定该文件已经被下载完,或者有临时文件正在留着,所以检查一下这两个地方,存在则删除掉
    NSString *targetPath=[[CommonHelper getTargetFloderPath]stringByAppendingPathComponent:selectFileInfo.fileName];
    NSString *tempPath=[[[CommonHelper getTempFolderPath]stringByAppendingPathComponent:selectFileInfo.fileName]stringByAppendingString:@".temp"];
    if([CommonHelper isExistFile:targetPath])//已经下载过一次该音乐
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"该文件已经添加到您的下载列表中了!是否重新下载该文件?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
        return;
    }
    //存在于临时文件夹里
    if([CommonHelper isExistFile:tempPath])
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"该文件已经添加到您的下载列表中了!是否重新下载该文件?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
        return;
    }
    selectFileInfo.isDownloading=YES;
    //若不存在文件和临时文件,则是新的下载
    HayateAppDelegate *appDelegate=APPDELEGETE;
    [appDelegate beginRequest:selectFileInfo isBeginDown:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==1)//确定按钮
    {
        NSFileManager *fileManager=[NSFileManager defaultManager];
        NSError *error;
        HayateAppDelegate *appDelegate=APPDELEGETE;
        NSString *targetPath=[[CommonHelper getTargetFloderPath]stringByAppendingPathComponent:self.fileInfo.fileName];
        NSString *tempPath=[[[CommonHelper getTempFolderPath]stringByAppendingPathComponent:self.fileInfo.fileName]stringByAppendingString:@".temp"];
        if([CommonHelper isExistFile:targetPath])//已经下载过一次该音乐
        {
            [fileManager removeItemAtPath:targetPath error:&error];
            if(!error)
            {
                NSLog(@"删除文件出错:%@",error);
            }
            for(FileModel *file in appDelegate.finishedlist)
            {
                if([file.fileName isEqualToString:self.fileInfo.fileName])
                {
                    [appDelegate.finishedlist removeObject:file];
                    break;
                }
            }
        }    
        //存在于临时文件夹里
        if([CommonHelper isExistFile:tempPath])
        {
            [fileManager removeItemAtPath:tempPath error:&error];
            if(!error)
            {
                NSLog(@"删除临时文件出错:%@",error);
            }
        }
        
        for(ASIHTTPRequest *request in appDelegate.downinglist)
        {
            FileModel *fileModel=[request.userInfo objectForKey:@"File"];
            if([fileModel.fileName isEqualToString:self.fileInfo.fileName])
            {
                [appDelegate.downinglist removeObject:request];
                break;
            }
        }
        self.fileInfo.isDownloading=YES;
        self.fileInfo.fileReceivedSize=[CommonHelper getFileSizeString:@"0"];
        [appDelegate beginRequest:self.fileInfo isBeginDown:YES];
    }
}

@end
MusicCell 这个类主要实现上面提到的功能,就是对单个流媒体的操作。

更多的移动互联网的发展趋势拓者设计吧效果图移动互联网应用相关的资料请到互联网的一点事www.yidin.net 留言

欢迎各位同学加入 ios 技术二群 60440788 

posted on 2013-04-17 10:06  梁DH  阅读(753)  评论(0编辑  收藏  举报

导航