2012-01-18 31 views
-1

我寫代碼的動態視頻,即要存儲的文檔,並使用該視頻的任何wher在我們的節目如何在文件中保存視頻文件?

UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; 
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
ipc.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:ipc.sourceType]; 
ipc.delegate = self; 
ipc.editing = NO; 
[self presentModalViewController:ipc animated:YES]; 

回答

-1

下面的代碼會幫助你,

#pragma mark UIImagePickerControllerDelegate 

- (void)imagePickerController:(UIImagePickerController *)imagePickerControl didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    NSURL *videoURL; 
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ([type isEqualToString:(NSString *)kUTTypeVideo] || 
     [type isEqualToString:(NSString *)kUTTypeMovie]) 
    { 
     videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 

    } 
    [self saveVideoToFileFromURL:videoURL]; 
    [self dismissModalViewControllerAnimated:YES]; 

} 

-(void)saveVideoToFileFromURL:(NSURL*)videoStorageURL 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSDate *todayDateObj = [NSDate date]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"ddMMyyyyHHmmss"];  
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"movie%@.mov",[dateFormat stringFromDate:todayDateObj]] ]; 
    [dateFormat release]; 

    NSData *data = [NSData dataWithContentsOfURL:videoStorageURL]; 
    [data writeToFile:filePath atomically:YES]; 

} 
-1

下面的代碼方法插入AppDelegate.h文件

@property (nonatomic , retain) MPMoviePlayerViewController *mPlayer; 
-(void)initAndPlayMovie:(NSURL *)movieURL andViewController:(UIViewController*)vCtr; 

下面的代碼插入到AppDelegate中.m文件

編譯馬克 - 媒體播放器

-(void)initAndPlayMovie:(NSURL *)movieURL andViewController:(UIViewController*)vCtr 
{ 
    self.mPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]; 
    // set source type streaming 
    [self.mPlayer.moviePlayer setMovieSourceType:MPMovieSourceTypeUnknown]; 

    [self.mPlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen]; 

    // fit to screen mode 
    [self.mPlayer.moviePlayer setScalingMode:MPMovieScalingModeAspectFit]; 

    // full screen mode 
    [self.mPlayer.moviePlayer setFullscreen:YES animated:YES]; 

    [vCtr presentMoviePlayerViewControllerAnimated:self.mPlayer]; 
} 

-(void)stopPlaying_dismissMoviePlayer 
{ 
    [self.mPlayer.moviePlayer stop]; 
    [self.mPlayer dismissMoviePlayerViewControllerAnimated]; 
} 

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{ 
    [self stopPlaying_dismissMoviePlayer]; 
} 

下面的代碼插入到.h文件中

#import <UIKit/UIKit.h> 

#import <AVFoundation/AVAudioPlayer.h> 
#import "ASIHTTPRequest.h" 
#import "ASIDownloadCache.h" 

@interface locallyCacheMP3FileViewController : UIViewController 

- (IBAction)playAudio:(UIButton*)sender; 

@property (nonatomic, retain) ASIHTTPRequest *rqstForAudio; 

@end 

合成可變

@synthesize rqstForAudio=_rqstForAudio; 

創建方法的名稱playAudio其連接到按鈕按下事件。

#pragma mark - View lifecycle 

- (IBAction)playAudio:(UIButton*)sender 
{ 

    NSString *[email protected]"Your Video link"; 

    // check first locally exists or not 
    NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@/%@", 
            [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], 
            AudioFolder]; 

    NSDictionary *dOfAudios=[NSDictionary dictionaryWithContentsOfFile:strPathToAudioCache]; 

    if([dOfAudios valueForKey:strAudioURL]) { 
     [APP_DEL initAndPlayMovie:[NSURL fileURLWithPath:[dOfAudios valueForKey:strAudioURL]] andViewController:self]; 
    } else { 
     NSURL *audioURL = [NSURL URLWithString:strAudioURL]; 
     NSString *strPathToDownload=[NSString stringWithFormat:@"%@/%@",[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], 
            [strAudioURL lastPathComponent]]; 

     if(!self.rqstForAudio || [self.rqstForAudio isFinished]) { 
      self.rqstForAudio=[ASIHTTPRequest requestWithURL:audioURL]; 
      [self.rqstForAudio setDelegate:self]; 
      [self.rqstForAudio setAllowResumeForFileDownloads:YES]; 
      [self.rqstForAudio setCachePolicy:ASIUseDefaultCachePolicy]; 
      [self.rqstForAudio setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
      [self.rqstForAudio setDidFailSelector:@selector(failedToLoad:)]; 
      [self.rqstForAudio setDidFinishSelector:@selector(finishedLoading:)]; 
      [self.rqstForAudio setDownloadCache:[ASIDownloadCache sharedCache]]; 
      [self.rqstForAudio setDownloadDestinationPath:strPathToDownload]; 
      [self.rqstForAudio startAsynchronous]; 
     } 

    } 
} 

You can download the Source code and tutorial here