2012-05-06 6 views
0

私はNSURLConnectionデリゲートメソッドを使用してビデオファイルまたはオーディオファイルをダウンロードします。ダウンロードは正常です。ダウンロードが完了したらすぐに再生する必要があります。私はそれが(オートプレイ)がダウンロードされた直後に、ファイル(オーディオやビデオのいずれか)を再生することができますどのようにオーディオまたはビデオファイルをダウンロードした直後に再生する

-(IBAction)downloadAndPlay:(id)sender{ 
    NSString *fileUrlPath=[host stringByAppendingString:rowContent]; 


      // Create the request. 
      NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:fileUrlPath] 
      cachePolicy:NSURLRequestUseProtocolCachePolicy 
      timeoutInterval:60.0]; 


      // create the connection with the request 
      // and start loading the data 
      NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

      if (theConnection) { 
       // Create the NSMutableData to hold the received data. 

       receivedData = [NSMutableData data] ; 
       //NSLog(@"connection succeeded"); 

      } else { 

       // Inform the user that the connection failed. 
       UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Music Album" message:@"Connection failed, please try again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alertView show]; 

      } 
    } 

    //NSURLConnection delegate methods 
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    { 
     // This method is called when the server has determined that it 
     // has enough information to create the NSURLResponse. 

     // It can be called multiple times, for example in the case of a 
     // redirect, so each time we reset the data. 

     // receivedData is an instance variable declared elsewhere. 
     [receivedData setLength:0]; 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    { 
     // Append the new data to receivedData. 
     // receivedData is an instance variable declared elsewhere. 
     [receivedData appendData:data]; 
    } 

    - (void)connection:(NSURLConnection *)connection 
     didFailWithError:(NSError *)error 
    { 

     // inform the user 
     NSLog(@"Connection failed! Error - %@ %@", 
       [error localizedDescription], 
       [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
    } 

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     // do something with the data 
     // receivedData is declared as a method instance elsewhere 
     NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

    } 

私の関連するコードはこれです。 Thanxは事前に

+0

質問がありましたか? –

+0

ファイルをダウンロードした直後にファイルを再生するにはどうすればいいですか? – Luca

答えて

0

ダウンロードしたデータをファイルに保存してから、MPMoviePlayerViewControllerなどを使用してファイルを開く必要があります。今すぐファイルをNSDataとしてダウンロードしておきます。

NSDataオブジェクトを適切な名前/拡張子を持つファイルシステムに保存します(または、ファイルシステム自体にダウンロードを書き込んでください)。ファイルのURLハンドルを取得してあなたのメディアプレーヤー。

+0

ファイルをダウンロードしてファイルに保存する場合は、NSURLConnection'の代わりにNSURLDownloadを使用する必要があります。 –

関連する問題