为什么此 Objective-C 代码无法播放 AVPlayerViewController 视频?
Why this Objective-C code won't play an AVPlayerViewController video?
两者都在viewDidLoad()中。
这个 objective-c 源文件有什么问题?
swift 版本就像一个魅力...
Xcode 版本。 8.2 iOS 10
Swift(确定):
let path : String = Bundle.main.path(forResource: "charlie", ofType: "mp4")!
let movieurl : URL = URL(fileURLWithPath: path)
let movie : AVPlayerViewController = AVPlayerViewController()
movie.view.frame = self.view.bounds
let player : AVPlayer = AVPlayer(url: movieurl)
movie.player = player
self.view.addSubview(movie.view)
player.play()
Objective-C(不会show/play视频):
AVPlayerViewController* movie;
AVPlayer* player;
NSString *path = [[NSBundle mainBundle]
pathForResource:@"charlie" ofType:@"mp4"];
NSURL *url = [NSURL URLWithString:path] ;
movie = [[AVPlayerViewController alloc] init];
movie.view.frame = self.view.bounds;
player = [[AVPlayer alloc] initWithURL:url];
[movie setPlayer:player];
[self.view addSubview:movie.view];
[player play];
@import AVKit;
@import AVFoundation;
@property (nonatomic) AVPlayer *avPlayer;
@property (nonatomic) AVPlayerViewController* avPlayerView;
NSString *path = [[NSBundle mainBundle] pathForResource:@"Video1" ofType:@"mp4"];
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
avPlayer = [AVPlayer playerWithPlayerItem:anItem];
[avPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
self.avPlayerView = [[AVPlayerViewController alloc] init];
self.avPlayerView.view.frame = self.view.bounds;
[self.avPlayerView setPlayer:_avPlayer];
[self.view addSubview:_avPlayerView.view];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == avPlayer && [keyPath isEqualToString:@"status"]) {
if (avPlayer.status == AVPlayerStatusFailed) {
NSLog(@"AVPlayer Failed");
} else if (avPlayer.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayer Ready to Play");
} else if (avPlayer.status == AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}
}
-(IBAction) BtnPlay:(id)sender {
[avPlayer play];
}
-(IBAction) BtnPause:(id)sender {
[avPlayer pause];
}
试试我的代码。如果我的代码有任何问题请发表评论。
谢谢你。
快乐编码。
NSString *path = [[NSBundle mainBundle]
pathForResource:@"charlie" ofType:@"mp4"];
尝试
使用 [NSURL fileURLWithPath:path] 而不是 [NSURL URLWithString:path]
[NSURL fileURLWithPath]
或
[[NSBundle mainBundle] URLForResource:@"charlie" withExtension:@"mp4"]
Swift(确定):
let path : String = Bundle.main.path(forResource: "charlie", ofType: "mp4")!
let movieurl : URL = URL(fileURLWithPath: path)
let movie : AVPlayerViewController = AVPlayerViewController()
movie.view.frame = self.view.bounds
let player : AVPlayer = AVPlayer(url: movieurl)
movie.player = player
self.view.addSubview(movie.view)
player.play()
Objective-C(不会show/play视频):
AVPlayerViewController* movie;
AVPlayer* player;
NSString *path = [[NSBundle mainBundle]
pathForResource:@"charlie" ofType:@"mp4"];
NSURL *url = [NSURL URLWithString:path] ;
movie = [[AVPlayerViewController alloc] init];
movie.view.frame = self.view.bounds;
player = [[AVPlayer alloc] initWithURL:url];
[movie setPlayer:player];
[self.view addSubview:movie.view];
[player play];
@import AVKit;
@import AVFoundation;
@property (nonatomic) AVPlayer *avPlayer;
@property (nonatomic) AVPlayerViewController* avPlayerView;
NSString *path = [[NSBundle mainBundle] pathForResource:@"Video1" ofType:@"mp4"];
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
avPlayer = [AVPlayer playerWithPlayerItem:anItem];
[avPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
self.avPlayerView = [[AVPlayerViewController alloc] init];
self.avPlayerView.view.frame = self.view.bounds;
[self.avPlayerView setPlayer:_avPlayer];
[self.view addSubview:_avPlayerView.view];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == avPlayer && [keyPath isEqualToString:@"status"]) {
if (avPlayer.status == AVPlayerStatusFailed) {
NSLog(@"AVPlayer Failed");
} else if (avPlayer.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayer Ready to Play");
} else if (avPlayer.status == AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}
}
-(IBAction) BtnPlay:(id)sender {
[avPlayer play];
}
-(IBAction) BtnPause:(id)sender {
[avPlayer pause];
}
试试我的代码。如果我的代码有任何问题请发表评论。 谢谢你。 快乐编码。
NSString *path = [[NSBundle mainBundle] pathForResource:@"charlie" ofType:@"mp4"];
尝试
使用 [NSURL fileURLWithPath:path] 而不是 [NSURL URLWithString:path]
[NSURL fileURLWithPath]
或
[[NSBundle mainBundle] URLForResource:@"charlie" withExtension:@"mp4"]