iPhone SDK: How do you download video files to the Document Directory and then play them?

后端 未结 2 503
甜味超标
甜味超标 2020-12-01 03:57

I\'ve been fooling around with code for ages on this one, I would be very grateful if someone could provide a code sample that downloaded this file from a server http://www.

相关标签:
2条回答
  • 2020-12-01 04:31

    If you want to download videos asynchronous I would suggest using the ASI library, so your app doesn't lock up while you wait for the download to finish.

    http://allseeing-i.com/ASIHTTPRequest/

    0 讨论(0)
  • 2020-12-01 04:38

    Your code will work to play a movie file.

    The simplest way to download is synchronously:

    NSData *data = [NSData dataWithContentsOfURL:movieUrl];
    [data writeToURL:movieUrl atomically:YES];
    

    But it is better (for app responsiveness, etc) to download asynchronously:

        NSURLRequest *theRequest = [NSURLRequest requestWithURL:movieUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
        receivedData = [[NSMutableData alloc] initWithLength:0];
            NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    

    This requires implementing the informal NSURLConnection protocol:

    - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        [receivedData setLength:0];
    }
    
    - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [receivedData appendData:data];
    }
    
    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [connection release];
    }
    
    - (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
        return nil;
    }
    
    - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [connection release];
        [self movieReceived];
    }
    

    and then saving (and playing) the movie file in the movieReceived method.

    0 讨论(0)
提交回复
热议问题