デリゲート メソッドを使用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]);
}
ダウンロードした直後にファイル (オーディオまたはビデオ) を再生するにはどうすればよいですか (自動再生)。事前にサンクス。