1

サーバーの URL に mp3 ファイルがあります。次に、後でアクセスできるようにファイル ストアをダウンロードします。AFNetworking を使用して、URL からファイルをダウンロードしています。URLにアクセスするためにAFHTTPRequestOperationを使用していますが、NSLogでURLパスを取得していますが、ファイルがダウンロードされたかどうかはわかりません。ダウンロード オプションが実行されていません。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];

        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.detailsofmylife.net/wp-content/uploads/2010/02/Ellie-Goulding-Starry-Eyed-Live-Lounge-@-Radio-1.mp3"]];

        NSLog(@"The video url is %@", request);


        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:dataPath append:NO];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:dataPath]);
            NSLog(@"THE RESPONSE: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error1) {
            NSLog(@"Error: %@", error1);
        }];

        [operation start];
4

1 に答える 1

0

次のように mp3 ファイルをダウンロードしてみてください。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager.requestSerializer setValue:@"application/x-www-form-urlencoded"
                  forHTTPHeaderField:@"Content-Type"];

[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"audio/mpeg", nil];

[manager GET:@"http://www.detailsofmylife.net/wp-content/uploads/2010/02/Ellie-Goulding-Starry-Eyed-Live-Lounge-@-Radio-1.mp3"
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [operation.responseData writeToFile:[dataPath stringByAppendingPathComponent:@"file.mp3"] atomically:YES];
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@", error);
     }];

お役に立てれば!

于 2014-06-20T11:46:08.270 に答える