【问题标题】:how to play local video file?如何播放本地视频文件?
【发布时间】:2012-03-21 10:11:33
【问题描述】:

我已经使用我的屏幕(桌面)捕获软件创建了 .mov 视频文件,我希望该视频能够在我的 UIWebview 应用程序中播放。有什么方法可以播放该视频或任何其他方式,以便我可以为我的本地视频创建一个 URL ???

现在我使用默认视频链接在 UIWebview 中播放视频。

代码如下:

- (void)applicationDidBecomeActive:(UIApplication *)application 
{

    self.viewVideoDisplay.frame = CGRectMake(0, 0, 1024, 1024);
    [self.window addSubview:self.viewVideoDisplay];
    [self.window bringSubviewToFront:self.viewVideoDisplay];
    NSString *urlAddress = @"https://response.questback.com/pricewaterhousecoopersas/zit1rutygm/";
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];            
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];            
    //Load the request in the UIWebView.
    [self.webViewVideo loadRequest:requestObj];

    IsLoadingSelf = YES;
}

但我没有想要播放的视频网址..

请帮忙!!

【问题讨论】:

    标签: iphone objective-c ios video uiwebview


    【解决方案1】:

    编辑: MPMoviePlayerController 现已弃用。所以我使用了AVPlayerViewController。并编写了以下代码:

        NSURL *videoURL = [NSURL fileURLWithPath:filePath];
    //filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
        AVPlayer *player = [AVPlayer playerWithURL:videoURL];
        AVPlayerViewController *playerViewController = [AVPlayerViewController new];
        playerViewController.player = player;
        //[playerViewController.player play];//Used to Play On start
        [self presentViewController:playerViewController animated:YES completion:nil];
    

    请不要忘记导入以下框架:

    #import <AVFoundation/AVFoundation.h>
    #import <AVKit/AVKit.h>
    

    您可以使用MPMoviePlayerController播放本地文件。

    1. 添加Mediaplayer 框架并在您的viewController 中执行#import &lt;MediaPlayer/MediaPlayer.h&gt;。

    2. 将您在桌面上创建的视频文件拖放到 xcode 中。

    3.获取本地视频的路径。

    NSString*thePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"];
    NSURL*theurl=[NSURL fileURLWithPath:thePath];
    

    4.用你的路径初始化moviePlayer。

    self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
    [self.moviePlayer.view setFrame:CGRectMake(40, 197, 240, 160)];
    [self.moviePlayer prepareToPlay];
    [self.moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
    [self.view addSubview:self.moviePlayer.view];
    

    5. 控制播放后要执行的操作:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 
    //playBackFinished will be your own method.
    

    编辑 2:要处理 AVPlayerViewController 而不是 MPMoviePlayerController 的完成,请使用以下...

    AVPlayerItem *playerItem = player.currentItem;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
    

    在此示例中,我在完成后关闭 AVPlayerViewController:

    -(void)playBackFinished:(NSNotification *) notification {
        // Will be called when AVPlayer finishes playing playerItem
    
        [playerViewController dismissViewControllerAnimated:false completion:nil];
    }
    

    【讨论】:

    • 感谢您的帮助,我想问一件事,如果我要更改文件类型,此代码能够播放 mp4 文件???
    • @IphoneDevloper,当然,如果它高于 ios 4.2 应该没问题,我的 mp4 文件不能在 ios 4.2 设备上播放,所以必须将其更改为 mov,但效果很好适用于 ios 5 设备。因此,只需检查格式是否与该 ios 兼容,就可以了。
    • 当我添加子视图时,在子视图中我也可以看到状态。它就像两个状态栏。
    • 使用“MOV”作为文件扩展名给了我一个零路径。我不得不使用“mov”来让它工作。
    • 在视频再次开始播放时显示一些毫秒的间隔。我该如何解决这个问题。请帮帮我。
    【解决方案2】:

    只需将您的网址替换为以下代码

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"videoFileName" ofType:@"m4v"];  
    
    NSURL *fileURL    =   [NSURL fileURLWithPath:filepath];  
    

    【讨论】:

      【解决方案3】:

      上述解决方案解释了如何使用 NSBundle 播放 Xcode 中的视频。我的回答将有助于那些正在寻找从设备中动态选择视频并播放的人。

      class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate
      
      import AVFoundation
      import AVKit
      import MobileCoreServices 
      

      (别忘了在 Build Phases 中添加 MobileCoreServicesFramework)

      设置视频的属性。例如

      @IBAction func buttonClick(sender: AnyObject)
      {
          imagePicker.delegate = self
          imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
          imagePicker.mediaTypes = [kUTTypeMovie as String]
          imagePicker.allowsEditing = true
          self.presentViewController(imagePicker, animated: true,
              completion: nil)
      }
      

      然后实现UIImagePickerControllerDelegate函数

      func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
          {
              var filename = ""
              let mediaType = info[UIImagePickerControllerMediaType] as! NSString
              if mediaType.isEqualToString(kUTTypeMovie as String)
              {
                  let url = info[UIImagePickerControllerMediaURL] as! NSURL
                  filename = url.pathComponents!.last!
              }
              self.dismissViewControllerAnimated(true, completion: nil)
              self.playVideo(filename)
          }
      

      并使用上面的文件名,您可以播放视频:)

          func playVideo(fileName : String)
          {
              let filePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(fileName)
              let player = AVPlayer(URL: filePath)
              let playerViewController = AVPlayerViewController()
              playerViewController.player = player
              self.presentViewController(playerViewController, animated: true)
              {
                  player.play()
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2018-11-22
        • 2019-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多