0

ファイルを解析し.m3u8、参照ファイルをダウンロードし、.m3u8オフライン再生用にローカルで提供される新しいファイルを再作成するメソッドを作成しました。すべてが機能しますが、おそらく私はどちらかNSScanerまたはGrand Central Dispatchを誤用しています。私の考えでは、メソッドはすばやく実行され、ダウンロードをGCDにキューイングする必要があるためです。ただし、このメソッドの実行には完全な時間がかかります(バックグラウンドでは可能ですが、すべてのダウンロードが完了したときではなく、できるだけ早く新しいファイルを作成したいと思います)。誰かが私のボトルネックがどこにあるかを見ることができますか?前もって感謝します。

- (void)beginDownloadAndCreateLocalM3U8FileForLocalPlaybackFromPlaylist:(NSString*)playlist forId:(NSString*)_id withProgressBlock:(void (^)(float))progress withCompletionBlock:(void (^)(id))success
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        @autoreleasepool {
            NSString *stringURL = playlist;
            NSURL  *url = [NSURL URLWithString:stringURL];
            NSError *error;
            NSString *stringData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
            NSString *foundData;
            NSScanner *scanner=[NSScanner scannerWithString:stringData];
            NSUInteger counter = 0;
            NSMutableString *m3u8 = [NSMutableString new];
            [scanner scanUpToString:@"#EXTINF" intoString:&foundData];
            [m3u8 appendString:foundData];
            NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths objectAtIndex:0];
            if (![self directoryExistsAtAbsolutePath:[NSString stringWithFormat:@"%@/Web", documentsDirectory]]) {
                [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/Web", documentsDirectory] withIntermediateDirectories: YES attributes:nil error: &error];
            }
            if (![self directoryExistsAtAbsolutePath:[NSString stringWithFormat:@"%@/Web/%@", documentsDirectory, _id]]) {
                [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/Web/%@", documentsDirectory, _id] withIntermediateDirectories: YES attributes:nil error: &error];
            } else {
                success([NSString stringWithFormat:@"http://127.0.0.1:12345/%@.m3u8", _id]);
                return;
            }
            while (![scanner isAtEnd]) {
                [scanner scanUpToString:@"\n" intoString: &foundData];
                if ([foundData hasPrefix:@"#EXTINF:"]) {
                    [m3u8 appendFormat:@"%@\n", foundData];
                } else if ([foundData hasPrefix:@"http:"]) {
                    NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:foundData]];
                    if ( urlData )
                    {
                        NSString  *filePath = [NSString stringWithFormat:@"%@/Web/%@/%i.ts", documentsDirectory, _id, counter];
                        NSString *localURL = [NSString stringWithFormat:@"http://127.0.0.1:12345/%@/%i.ts", _id, counter];
                        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                            @autoreleasepool {
                                [urlData writeToFile:filePath atomically:YES];
                            }
                        });
                        //assemble the m3u8 file here for each entry with the original durations.  No need to recalculate.
                        //NSLog(@"Adding this: %@", filePath);
                        [m3u8 appendFormat:@"%@\n", localURL];
                        counter++;
                    }
                }
            }
            [m3u8 appendString:@"#EXT-X-ENDLIST"];
            [m3u8 writeToFile:[NSString stringWithFormat:@"%@/Web/%@.m3u8", documentsDirectory, _id] atomically:YES encoding:NSUTF8StringEncoding error:&error];
            NSLog(@"Final m3u8 is %@", m3u8);
            success([NSString stringWithFormat:@"http://127.0.0.1:12345/%@.m3u8", _id]);
        }
    });
}
4

1 に答える 1

2

この行はここにあります

NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:foundData]];

...whileループがスキャナーと同期して実行されている場合、ダウンロードしているURLがダウンロードされるまで、スキャナーは次のトークンに進むことができません。呼び出しelseだけでなく、ケースのすべてのロジックをカバーするために、優先度の低いdispatch_async()を持ち上げてみてください。writeToFile:

于 2013-02-01T17:12:46.070 に答える