27

アプリで Youtube ビデオを再生するのは簡単で、よく文書化されています。

これには 2 つの問題があります。

  1. Youtubeプレーヤーを閉じた後、ユーザーがもう一度再生したい場合、オンラインストリーミングを再度待つ必要があります
  2. オフラインで再生できません (自宅でビデオを読み込んで外出先で視聴できます)

誰もコードを持っていますか:

  1. Youtube ビデオをドキュメント フォルダにダウンロードし、ダウンロードの進行状況を表示する
  2. ドキュメントフォルダからファイルをロードして、ダウンロードしたビデオを再生します(インターネットに接続していない場合でも)
4

6 に答える 6

42

YouTubeからビデオをダウンロードするには:

  1. YouTube APIまたはその他の方法で、ダウンロードするURLを取得します。
  2. Create an NSOutputStream or NSFileHandle opened on a temporary file (in NSTemporaryDirectory() or a temp-named file in your Documents directory).
  3. Set up your progress bar and whatever else you need to do.
  4. Allocate and start an NSURLConnection to fetch the file from the URL. Do not use sendSynchronousRequest:returningResponse:error:, of course.
  5. In the connection:didReceiveResponse: delegate method, read out the length of data to be downloaded for proper updating of the progress bar.
  6. In the connection:didReceiveData: delegate method, write the data to the output stream/file handle and update the progress bar as necessary.
  7. In connectionDidFinishLoading: or connection:didFailWithError:, close the output stream/file handle and rename or delete the temporary file as appropriate.

再生するには、NSURLを使用fileURLWithPath:してDocumentsディレクトリ内のローカルファイルを指すURLを作成し、リモートビデオと同じように再生します。

于 2011-05-23T14:53:33.240 に答える
7

このプロジェクトのクラスを使用しました: https://github.com/larcus94/LBYouTubeView 私にとっては問題なく動作します。ユーチューブの動画をダウンロードできます。

私はこのコードを使用しました:

LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(@"http://www.youtube.com/watch?v=%@"), self.videoID ]] quality:LBYouTubeVideoQualityLarge] autorelease];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
    if(!error) {
        NSLog(@"Did extract video URL using completion block: %@", videoURL);

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *data = [NSData dataWithContentsOfURL: videoURL];
            NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *filename = [NSString stringWithFormat:(@"video_%@.mp4"), self.videoID ];
            [data writeToFile:[pathTODocs stringByAppendingPathComponent:filename] atomically:YES];
            NSLog(@"File %@ successfully saved", filename);
        });
    } else {
        NSLog(@"Failed extracting video URL using block due to error:%@", error);
    }
}];

上記の投稿で説明されている手法を使用して、ダウンロードの進行状況を表示できます。

于 2013-01-16T07:07:11.140 に答える
6

これが私の例です: https://github.com/comonitos/youtube_video

私はPeter SteinbergerによるPSYouTubeExtractor.hクラスを使用しました。YouTubeのmp4ビデオのURLを取得でき、ダウンロードして表示することは問題ありません

NSURLConnection + NSNotificationCenter + PSYouTubeExtractor + NSMutableData

于 2012-02-27T09:40:23.037 に答える
5

私は個人的にYouTubeビデオをダウンロードする方法を知りません(そしてコードが大きすぎてここに答えを入れることができません)。

ただし、ここに完全なYouTubeダウンロードの例があります。これは、LoadTubeと呼ばれるオープンソースのYouTubeダウンローダーです。ここにソースコードへのリンクがあります

于 2011-05-23T12:22:46.190 に答える
5

これらのプロジェクトをチェックしてください -

https://github.com/iosdeveloper/MyTube
https://github.com/pvinis/mytube

これらは間違いなくあなたを助けます!!

于 2011-05-28T06:47:39.053 に答える
1

ビデオを再生して、一時ファイルが保存されている場所を見つけます。アクセスできる場合は、オフラインで表示できるようにドキュメント フォルダーにコピーします。

于 2011-02-23T05:16:57.617 に答える