【问题标题】:how to read files from directory如何从目录中读取文件
【发布时间】:2013-02-03 21:12:27
【问题描述】:

我有一个别人写的项目。 该项目将视频文件保存在此路径,file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4

我想用 MPMoviePlayerController 播放文件,如何访问文件?

【问题讨论】:

    标签: iphone ios nsdocumentdirectory


    【解决方案1】:

    如果您将上述 URL 存储在某处

    NSString *fullURL = @"/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4"
    NSURL *movieURL = [NSURL fileURLWithPath: isDirectory:YES]; //If YES not works use NO
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    

    希望有帮助

    【讨论】:

      【解决方案2】:

      试试这个:-

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
      NSString *documentsDirectory = [paths objectAtIndex:0];  
      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"output.mp4"];  
      

      fullPath 中,您将拥有上述路径。

      有关如何播放视频的更多详细信息,请参阅this tutorial

      希望对你有所帮助..

      【讨论】:

      • 你为什么用stringWithFormat
      • @MidhunMP 实际上我有整数变量来播放许多视频,无论如何编辑答案。谢谢:)
      【解决方案3】:

      首先加载路径和文件URL:

      NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES) objectAtIndex:0];
      
      NSString *localFilePath = [photoDirectoryPath stringByAppendingPathComponent:@"output.mp4"];
      

      然后用 URL 加载 MPMoviePlayerController

      - (void)initMoviePlayer 
      {
      didExit = NO;
      
      [[NSNotificationCenter defaultCenter] addObserver:self 
                                               selector:@selector(moviePreloadDidFinish:) 
                                                   name:MPMoviePlayerLoadStateDidChangeNotification 
                                                 object:mMoviePlayer];
      
      [[NSNotificationCenter defaultCenter] addObserver:self 
                                               selector:@selector(moviePlayBackDidFinish:) 
                                                   name:MPMoviePlayerPlaybackDidFinishNotification 
                                                 object:nil];
      
      NSLog(@"initMoviePlayer: %@ ",[self getMovieURL]);
      
      mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]];
      
      mMoviePlayer.shouldAutoplay = YES;
      
      mMoviePlayer.view.hidden = YES;
      
      [mMoviePlayer setControlStyle:MPMovieControlStyleNone];
      
      mMoviePlayer.view.frame = [self.view bounds];
      
      mMoviePlayer.scalingMode = MPMovieScalingModeNone;
      
      [[self view] addSubview:[mMoviePlayer view]];
      
      [mMoviePlayer play];
      
      }
      
      - (void) moviePreloadDidFinish:(NSNotification*)notification {
      
      // start playing the movie
      
          mMoviePlayer.view.hidden = NO;
      
          animateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                      target:self 
                                                    selector:@selector(playMovie:) 
                                                    userInfo:nil 
                                                     repeats:NO];
      }
      
      - (void) moviePlayBackDidFinish:(NSNotification*)notification 
      {        
      
      }
      

      【讨论】:

      • 不工作我得到我得到一个错误errorLog MPMovieFinishReasonPlaybackError
      【解决方案4】:

      试试这个代码伙伴,

      NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
      
      NSString *videoFilePath = [documentDirectory stringByAppendingPathComponent:@"output.mp4"];
          NSURL *videoURL = [NSURL URLWithString:videoFilePath];
      
      MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        • 1970-01-01
        • 2010-10-11
        • 2016-01-06
        • 1970-01-01
        相关资源
        最近更新 更多