我用我的屏幕(桌面)捕获软件创建了.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;
}
但是我没有想要播放的视频网址..
请帮助!!
答案 0 :(得分:65)
编辑: 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 <MediaPlayer/MediaPlayer.h>
。
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];
}
答案 1 :(得分:5)
只需使用以下代码替换您的网址
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"videoFileName" ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
答案 2 :(得分:0)
以上解决方案解释了如何使用NSBundle播放Xcode中存在的视频。我的回答对那些正在寻找从设备和游戏中动态选择视频的人有所帮助。
class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate
import AVFoundation
import AVKit
import MobileCoreServices
(不要忘记在Build阶段添加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()
}
}