DWAppDelegate.m 8.09 KB
Newer Older
Han Ruisong's avatar
Han Ruisong committed
1
2
3
4
5
6
#import "DWAppDelegate.h"
#import "DWDownloadSessionManager.h"
#import "DWMainViewController.h"
#import "DWNavigationViewController.h"
#import "DWOfflineModel.h"
#import "MJExtension.h"
Han Ruisong's avatar
Han Ruisong committed
7
#import <Photos/Photos.h>
Han Ruisong's avatar
Han Ruisong committed
8
9
10
#import "DWVodPlayViewController.h"
#import "DWLocalPlayViewController.h"
#import "DWVodPlayerView.h"
Han Ruisong's avatar
Han Ruisong committed
11
12
13
14
15
16
17
18
19
20
21
22

#define DWUploadItemPlistFilename @"uploadItems.plist"

@interface DWAppDelegate ()

@end

@implementation DWAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [DWLog setIsDebugHttpLog:YES];
Han Ruisong's avatar
Han Ruisong committed
23

Han Ruisong's avatar
Han Ruisong committed
24
    
Han Ruisong's avatar
Han Ruisong committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    //设置AVAudioSession
    NSError *categoryError = nil;
    BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&categoryError];
    if (!success)
    {
        NSLog(@"Error setting audio session category: %@", categoryError);
    }
    
    NSError *activeError = nil;
    success = [[AVAudioSession sharedInstance] setActive:YES error:&activeError];
    if (!success)
    {
        NSLog(@"Error setting audio session active: %@", activeError);
    }
Han Ruisong's avatar
Han Ruisong committed
39

Han Ruisong's avatar
Han Ruisong committed
40
    //后台下载设置
Han Ruisong's avatar
Han Ruisong committed
41
42
43
    //设置最大下载并发数,默认不设置
//    [DWDownloadSessionManager manager].maxDownloadCount = 2;
//    [DWDownloadSessionManager manager].isBatchDownload = NO;
Han Ruisong's avatar
Han Ruisong committed
44
45
46
47
48
49
50
51
52
53
54
55
56
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    DWMainViewController * mainVC = [[DWMainViewController alloc]initWithCollectionViewLayout:[self getLayout]];
    DWNavigationViewController * nc = [[DWNavigationViewController alloc]initWithRootViewController:mainVC];
    nc.navigationBar.translucent = NO;
    self.window.rootViewController = nc;
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    //根据自己项目原业务逻辑,自行斟酌调用即可。
    [self migrateOldDownloadTaskToNewVersion];
Han Ruisong's avatar
Han Ruisong committed
57
58
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumePlayVCAction:) name:DWVODPLAYERRESUMEEVENTNOTIFICATION object:nil];
Han Ruisong's avatar
Han Ruisong committed
59
        
Han Ruisong's avatar
Han Ruisong committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    [self.window makeKeyAndVisible];
    
    return YES;
}

//下载任务过渡到新版
-(void)migrateOldDownloadTaskToNewVersion
{
    //!!!仅供参考
    //对于旧版本转移逻辑,请参照自己项目中的业务逻辑来处理,此处只是针对旧版Demo中的下载任务,转移到新版的示例。
    
    //1。首先,需要获取到自己项目中的下载队列。
    NSArray * downingArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"downingArray"];
    
    NSArray * finishDicArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"finishDicArray"];
    
    //2。通过[DWDownloadSessionManager manager]的 migrateDownloadTask:。。。方法,将旧版的下载任务过渡到新版。
    [downingArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DWOfflineModel * model = [DWOfflineModel mj_objectWithKeyValues:obj];
        //未下载完成的视频
        //本地路径请务必传nil
        DWDownloadModel * downloadModel = [[DWDownloadSessionManager manager]
                                           migrateDownloadTask:nil
                                           DownloadUrl:model.playurl
                                           MediaType:model.mediatype
                                           Quality:model.definition Desp:model.desp
                                           VRMode:NO
                                           OthersInfo:@{@"title":model.videoId,@"imageUrl":@"icon_placeholder.png"}
                                           UserId:[DWConfigurationManager sharedInstance].DWAccount_userId
                                           VideoId:model.videoId
                                           TotalBytesWritten:0
                                           TotalBytesExpectedToWrite:0];
        
        //如不开始下载,页面显示的进度根据totalBytesWritten,totalBytesExpectedToWrite参数决定。
        //如果以前没有存储此字段,继续开始下载即可获取到正确的数据。
//        if (downloadModel) {
//            //证明过渡成功,否则返回nil
//            [[DWDownloadSessionManager manager] resumeWithDownloadModel:downloadModel];
//        }
    }];
    
    [finishDicArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DWOfflineModel * model = [DWOfflineModel mj_objectWithKeyValues:obj];
        
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentDirectory = [paths objectAtIndex:0];
        NSString * localPath = [NSString stringWithFormat:@"%@/%@",documentDirectory,[model.videoPath lastPathComponent]];
        
        DWDownloadModel * downloadModel = [[DWDownloadSessionManager manager]
                                           migrateDownloadTask:localPath
                                           DownloadUrl:model.playurl
                                           MediaType:model.mediatype
                                           Quality:model.definition
                                           Desp:model.desp
                                           VRMode:NO
                                           OthersInfo:@{@"title":model.videoId,@"imageUrl":@"icon_placeholder.png"}
                                           UserId:[DWConfigurationManager sharedInstance].DWAccount_userId
                                           VideoId:model.videoId
                                           TotalBytesWritten:0
                                           TotalBytesExpectedToWrite:0];
    }];
    
    //3.下载任务过渡完成后,将原本地保存的下载数据删除掉即可。
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"downingArray"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"finishDicArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Han Ruisong's avatar
Han Ruisong committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-(void)resumePlayVCAction:(NSNotification *)noti
{
    UINavigationController * na = (UINavigationController *)self.window.rootViewController;
    UIViewController * preVC = na.viewControllers.lastObject;
    if (self.vodPlayerView.videoModel) {
        //在线视频
        DWVodPlayViewController * vodPlayVC = [[DWVodPlayViewController alloc]init];
        vodPlayVC.vodModel = self.vodPlayerView.vodModel;
        vodPlayVC.vidoeList = self.videoList;
        vodPlayVC.landScape = [noti.object boolValue];
        [preVC.navigationController pushViewController:vodPlayVC animated:YES];
    }else{
        //离线视频
        DWLocalPlayViewController * localPlayVC = [[DWLocalPlayViewController alloc]init];
        localPlayVC.downloadModel = self.vodPlayerView.downloadModel;
        [preVC.navigationController pushViewController:localPlayVC animated:YES];
    }
}
Han Ruisong's avatar
Han Ruisong committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

- (void)applicationWillResignActive:(UIApplication *)application
{
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
      [application beginReceivingRemoteControlEvents];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{
    
}

-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
Han Ruisong's avatar
Han Ruisong committed
173
174
175
176
177
{
    //下载的应该也得 搞个id传进去
    [[DWDownloadSessionManager manager] setBackgroundSession:identifier CompletionHandler:completionHandler];
 
    [[DWUploadSessionManager manager] setUploadSession:identifier CompletionHandler:completionHandler];
Han Ruisong's avatar
Han Ruisong committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
}

-(UICollectionViewFlowLayout *)getLayout
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((ScreenWidth - 5) / 2.0, ScaleHeight((ScreenWidth - 20 - 5) / 2.0, 175 / 98.0) + 10 + 14);
    layout.headerReferenceSize = CGSizeMake(ScreenWidth, ScaleHeight((ScreenWidth - 20),355 / 200.0) + 10);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 15;
    return layout;
}


@end