3

現在、次のコードを使用して AVPlayer で mp3 を再生しています

    NSString *title = [[NSString alloc] initWithFormat:@"http://.......com/%@", mp3File];
    NSURL *url = [[NSURL alloc] initWithString:title];
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithURL:url];


    player = [AVPlayer playerWithPlayerItem:anItem];

    [player play];

ただし、ダウンロードしたファイルも保存したいので、その後の再生ではファイルを再度ダウンロードする必要はありません。このために、私は以下を使用しています

     NSString  *path = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:surahAyah]];

     if (![[NSFileManager defaultManager] fileExistsAtPath:path])
     {
        NSString *title = [[NSString alloc] initWithFormat:@"http://.......com/%@", mp3File];
        NSURL *url = [[NSURL alloc] initWithString:title];

        NSData *dbFile = [[NSData alloc] initWithContentsOfURL:url];
        [dbFile writeToFile:path atomically:YES];
     }

上記を統合してキャッシュを利用するためのベストプラクティスは何ですか? ファイルをダウンロードして保存するための新しい呼び出しを行う代わりに、AVPlayerItem を保存して保存しますか?

MP3 ファイルのサイズは約 100 KB なので、ダウンロードの進行状況はあまり気にしません。

4

1 に答える 1

3

素晴らしいAFNetworkingライブラリを使用して、これをうまく機能させることができました

  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

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

  [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];


    [operation start];
于 2012-09-25T18:35:45.670 に答える