2011-02-24 9 views
1

私のプロジェクトでは、URLを接続していて、MPMoviePlayerViewControllerでビデオを見ています。しかしそれだけでは私には不十分です。私はまた、私のiPhoneにファイルを保存したい。私はボタンが必要です。 1つはビデオを見る、もう1つはビデオを保存することです。私がボタンの時計を押すと、私はそれをwatchngです。しかし、それを保存することができません。これで私はビデオを後で見ることができます。だから別のビューで私は救われたビデオ等を見たいと思います。誰かが私を助けることができるか、または方法を示すことができますか?私は次のコードフレーズを試してみましたが、コードが開始されると、しばらくの間(おそらくダウンロード時間です)動作しますが、保存するときには、EXC_BAD_ACCESSエラーが発生します。
ここに私のコードです。ストリームをURLから保存する

CFStringRef *docsDirectory = (CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: @"recordedFile.mp4"]; 


NSString *temPath=NSTemporaryDirectory(); 
NSString *tempfile=[temPath stringByAppendingPathComponent:@"recode.mp4"]; 


NSLog(@" DOSYA ADI MADI %@",docsDirectory); 


NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error = [[NSError alloc] init]; 

[fileManager removeItemAtPath:docsDirectory error:&error]; 

NSURL *url = [NSURL URLWithString:@"http://video.teknomart.com.tr/3-13-2.mp4"]; 

NSMutableURLRequest *liveRequest = [[NSMutableURLRequest alloc] initWithURL:url]; 
[liveRequest setCachePolicy:NSURLRequestReloadIgnoringCacheData]; 

[liveRequest setValue:@"headervalue" forHTTPHeaderField:@"headerfield"]; 

NSURLResponse *response; 
NSData *myData = [NSURLConnection sendSynchronousRequest:liveRequest returningResponse:&response error:&error]; 
NSData *myData2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://video.teknomart.com.tr/3-13-2.mp4"]]; 

NSString *myString=[[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding]; 

NSLog(@"gelen sey %@",myString); 

[myString writeToFile:tempfile writeToFile:tempfile automatically:YES encoding:NSASCIIStringEncoding error:nil]; 

[myString release]; 
return true; 

答えて

2
-(void)viewDidLoad { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     if (!documentsDirectory) { 
      NSLog(@"Documents directory not found!"); 
     } 
     NSArray *myWords = [songNameString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; 
     NSString *appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]]; 
     NSLog(@"%@",[myWords lastObject]); 
     NSURL *url = [NSURL fileURLWithPath:appFile]; 
     NSLog(@"%@",url); 
     [[UIApplication sharedApplication] setStatusBarHidden:YES]; 
     self.navigationController.navigationBarHidden=YES; 
     currentTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showCurrentTime) userInfo:nil repeats:YES]; 
     alarmTimeLabel.text =alarmTimeString; 
     alarmSongLabel.text = [myWords lastObject] ; 
     [self performSelector:@selector(loadVideoInBackground)]; 
     //[NSThread detachNewThreadSelector:@selector(loadVideoInBackground) toTarget:self withObject:nil]; 


    } 

    -(void)loadVideoInBackground 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     if (!documentsDirectory) { 
      NSLog(@"Documents directory not found!"); 
     } 
     NSString *appFile; 
     NSArray *myWords = [songNameString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; 
     appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]]; 
     NSFileManager *fileMgr=[NSFileManager defaultManager]; 
     if (![fileMgr fileExistsAtPath:appFile]) { 
      alarmCanPlay = FALSE; 
      NSURL *imageURL = [[[NSURL alloc] initWithString:songNameString]autorelease]; 
      NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageURL 
                  cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120.0]; 
      imageConnection = [[NSURLConnection alloc] initWithRequest:imageRequest delegate:self]; 

      if(imageConnection) 
      { 
       videoData = [[NSMutableData data] retain]; 
      } 

     } 
    } 


    - (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 declared as a method instance elsewhere 
     [videoData setLength:0]; 

    } 
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    { 
     // append the new data to the receivedData 
     // receivedData is declared as a method instance elsewhere 
     //NSLog(@"%d",[data size]); 
     [videoData appendData:data]; 
    } 

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    { 
     // release the data object 
     [videoData release]; 

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

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     if (!documentsDirectory) { 
      NSLog(@"Documents directory not found!"); 
     } 
     NSString *appFile; 
     NSArray *myWords = [songNameString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; 
     appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]]; 
     [videoData writeToFile:appFile atomically:YES]; 
     alarmCanPlay = TRUE; 
    } 
+0

と – GhostRider

+0

おかげで、あなたの再生のためのダウンロード後にその動きを果たしています。私はそれを使用しようとしました。私の映画がダウンロードされたようです。どうすればそのファイルにアクセスできますか?私はファイルを取得しようとしています。 \t NSString * documentsDirectory = NSTemporaryDirectory(); \t \t if(!documentsDirectory){ \t \t \t NSLog(@ "Documents directory not found!"); \t \t} \t \t NSString * appFile; \t \t appFile = [documentsDirectory stringByAppendingPathComponent:@ "mymoview.mp4"]; – ozgur

+0

新しい回答を1つ追加して、その動画を私の見解でどのように再生しましたか?助けることを望む – GhostRider

0

これは私が私のコードを持っているどのようなものです。映画をダウンロードしているようです。しかし、私はファイルを見つけることができません。また私はsimilatorでそれを実行します。私のmovienameという名前のファイルはtmpディレクトリにありました。何が間違っているか不足している?

-(void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    NSString *documentsDirectory = NSTemporaryDirectory(); 
    if (!documentsDirectory) { 
     NSLog(@"Documents directory not found!"); 
    } 
    downloaded=FALSE; 

    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"mymoview.mp4"]; 

    NSURL *url = [NSURL fileURLWithPath:appFile]; 
    NSLog(@"%@",url); 
    [[UIApplication sharedApplication] setStatusBarHidden:NO]; 
    self.navigationController.navigationBarHidden=NO; 

    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showVideo) userInfo:nil repeats:YES]; 

    [self performSelector:@selector(loadVideoInBackground)]; 
    [NSThread detachNewThreadSelector:@selector(loadVideoInBackground) toTarget:self withObject:nil]; 
    } 

-(void)showVideo{ 

    NSString *homeDir = NSHomeDirectory(); 


    NSString *tempDir = NSTemporaryDirectory(); 

    // Format output 
    NSString *s = 
    [NSString stringWithFormat:@"homeDir:\n" 
    @"%@\n" 
    @"tempDir:\n" 
    @"%@\n", 
    homeDir, 
    tempDir]; 
    NSLog(@" %@ ",s); 



     if (downloaded) { 

       NSLog(@"burdayim"); 

     NSString *documentsDirectory = NSTemporaryDirectory(); 
     if (!documentsDirectory) { 
      NSLog(@"Documents directory not found!"); 
     } 

     NSString *appFile; 
     appFile = [documentsDirectory stringByAppendingPathComponent:@"mymoview.mp4" ]; 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myvoview" ofType:@"mp4" inDirectory:@"/tmp"]; 
       NSLog(@" %@ lafta oynatilan dosya ",filePath); 



      NSFileManager *fileMgr=[NSFileManager defaultManager]; 
      if (![fileMgr fileExistsAtPath:filePath]) { 
       NSLog(@"dosya yok"); 
      } 
      else   
      { 

       NSLog(@"dosyayi da buldum"); 


     NSURL *movieURL = [NSURL fileURLWithPath:appFile]; 

      NSLog(@" %@ lafta oynatilan dosya ",appFile); 
     MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 



     [player setFullscreen:YES]; 
     [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft]; 
    player.fullscreen=YES; 
     //self.view.autoresizesSubviews=YES; 
     //[self presentModalViewController:player animated:YES]; 



    self.view=player.view; 

     [player play]; 
      } 

    } 
    } 

-(void)loadVideoInBackground{ 

NSString *documentsDirectory = NSTemporaryDirectory(); 

if (!documentsDirectory) { 
NSLog(@"Documents directory not found!"); 
} 

NSString *appFile; 
    appFile = [documentsDirectory stringByAppendingPathComponent:@"mymoview.mp4" ]; 
    NSFileManager *fileMgr=[NSFileManager defaultManager]; 
    if (![fileMgr fileExistsAtPath:appFile]) { 

     NSURL *videoURL = [[[NSURL alloc] initWithString:@"http://video.teknomart.com.tr/3-13-2.mp4"] autorelease]; 
     NSURLRequest *videoRequest = [NSURLRequest requestWithURL:videoURL 
                 cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120.0]; 
     videoconnection = [[NSURLConnection alloc] initWithRequest:videoRequest delegate:self]; 

     if(videoconnection) 
     { 
      videoData = [[NSMutableData data] retain]; 
     } 

    } 
} 


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 


    //[videoData setLength:0]; 

} 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // append the new data to the receivedData 
    // receivedData is declared as a method instance elsewhere 
    NSLog(@" bisiler yukleniyor "); 
    [videoData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
[videoData release]; 

NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    if (!documentsDirectory) { 
     NSLog(@"Documents directory not found!"); 
    }else 
{ 
    NSString *appFile; 
    appFile = [documentsDirectory stringByAppendingPathComponent:@"mymoview.mp4"]; 
    [videoData writeToFile:appFile atomically:YES]; 
    downloaded = TRUE; 
    NSLog(@"Yuklandi"); 
} 

    } 
} 
0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    if (!documentsDirectory) { 
     NSLog(@"Documents directory not found!"); 
    } 
    NSArray *myWords = [songNameString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; 
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]]; 
    NSLog(@"%@",[myWords lastObject]); 
    NSURL *url = [NSURL fileURLWithPath:appFile]; 
    NSLog(@"%@",appFile); 
self.videoMPPlayer =[[MPMoviePlayerController alloc] init]; 
      videoMPPlayer.view.frame = CGRectMake(0, 0, 768, 1024); 
      videoMPPlayer.scalingMode = MPMovieScalingModeAspectFill; 

      videoMPPlayer.controlStyle = MPMovieControlStyleNone; 
      videoMPPlayer.shouldAutoplay = NO; 
      [videoMPPlayer pause]; 
私のiphoneのドキュメントディレクトリに私はURLから動画を保存しています。このリンクで
関連する問題