ios - 在 iOS 中播放视频

标签 ios objective-c iphone avplayer

我正在尝试播放存储在我设备上的视频,同时尝试使用 AVPlayerMPMoviePlayerController。 视频 URL 为:file:///var/mobile/Containers/Data/Application/73695F3E-9351-447B-BB8A-0B4A62CE430F/Documents/08-03-201711:48:50.3gp。现在的问题是我出现黑屏。

AVPlayer *player = [AVPlayer playerWithURL:fileURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[player pause];
[player play];
playerViewController.player = player;

[self addChildViewController: playerViewController];
[self.view addSubview: playerViewController.view];
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];

我认为问题出在链接上。我正在做的是获取本地目录链接并在该链接后附加名称。

最佳答案

我会给你一个好的解决方案。它完美地工作。

ViewController.h

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

@interface ViewController : UIViewController
@property (strong, nonatomic) AVPlayerViewController *playerViewController;
- (IBAction)actionPlayVideo:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController (){
    NSURL *vedioURL;
}

@end

@implementation ViewController
@synthesize playerViewController;

- (IBAction)actionPlayVideo:(id)sender{
    NSString *fullpath = [[self documentsDirectory] stringByAppendingPathComponent:@"yourdate.3gp"];
    vedioURL =[NSURL fileURLWithPath:fullpath];
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:vedioURL];
    AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    playerViewController = [[AVPlayerViewController alloc] init];
    playerViewController.player = playVideo;
    playerViewController.player.volume = 0;
    playerViewController.view.frame = self.view.bounds;
    [self.view addSubview:playerViewController.view];
    [playVideo play];
}
-(NSString *)documentsDirectory{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

关于ios - 在 iOS 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129915/

相关文章:

ios - 如何使用swift将两个加在一起的变量转换为标签文本

iphone - CFArray 和 NSArray 的区别

android - Android 和 iPhone 的推送通知

iphone - 如何在获取数据之前停止加载 UITableView?

ios - 使用 linphone ios sdk 的冲突类型错误

ios - parse.com action-loc-key 推送通知

ios - iOS UIButton 选中区域

iphone - 丑陋的 heightForRowAtIndexPath 和 cellForRowAtIndexPath

objective-c - AVAudioPlayer 拒绝播放修改后的 wav 文件

android - Bootstrap - 无法单击移动设备上的下拉子菜单项,并且第一次单击不会触发导航折叠