【发布时间】:2016-12-30 21:51:20
【问题描述】:
我的代码如下所示:
_player = [AVPlayer playerWithURL:destination];
_playerVC = [AVPlayerViewController new];
_playerVC.player = _player;
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:_playerVC animated:YES completion:^{
[_player play];
}];
});
_player 代表 AVPlayer,_playerVC 代表 AVPlayerViewController。我对这些对象有很强的全局引用。
使用终端,我播放了位于目的地的文件(打开 -a quicktime\ player destinationURLAbsoluteString),并看到文件在播放后已正确加载。
这是一个 m4v 文件。我播放了一个 m4a 文件,它正确地给了我音频。我还将目的地的 url 替换为一些远程 url,并且适用于视频。这让我相信它与我的视频有关。奇怪的是,我的 AVPlayerViewController 确实显示所有正常控件的黑屏,甚至显示视频为 2 分 23 秒。手动打开视频文件,我看到也是2分23秒。
我可以通过拖动指示视频位置的白点正确地向前浏览视频,但我什么也没听到,除了黑屏和控件我什么也看不到。
TL;DR
NSLog(@"目的地:%@",destination); 打印:file:///Users/MyLogin/Library/Developer/CoreSimulator/Devices/694F4C94-F785-4931-A312-4C3E7DE8673A/data/Containers/Data/Application/76C923BE-5B25-41F7-9145-63414657FDF6/Documents/mzvf_59140025198 .640x352.h264lc.D2.p.m4v
总而言之,看起来我正在正确检索视频,但由于某种原因,我的播放器控制器完全变黑并且没有音频。非常感谢任何帮助。
源代码:
.m 文件:
#import "ViewController.h"
#import "View2ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property NSDictionary *requestedSong;
@property (strong) AVPlayer *player; //Declare Globally
@property (strong) AVPlayerViewController *playerVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
button.center = self.view.center;
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"Query Songs" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
button2.center = CGPointMake(button.center.x, button.center.y + 200);
button2.backgroundColor = [UIColor blueColor];
[button2 setTitle:@"Listen to the first song" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(button2Clicked) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button2];
}
-(void)button2Clicked{
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
NSURL *url = [NSURL URLWithString:[_requestedSong objectForKey:@"previewUrl"]];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url];
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:req];
[task resume];
}
-(void)buttonClicked{
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/search?media=movie&entity=movie&term=Mad"];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url];
NSURLSessionDataTask *task = [session dataTaskWithRequest:req];
[task resume];
}
-(NSURL*)localFilePathForURL:(NSURL *) previewUrl{
//get the directory to use
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) objectAtIndex:0];
//create the full path
NSString *fullPath = [documentsPath stringByAppendingPathComponent:previewUrl.lastPathComponent];
//append the filename and append to document path url
return [NSURL fileURLWithPath:fullPath];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location{
NSURL* originalURL = downloadTask.originalRequest.URL;
NSURL *destination = [self localFilePathForURL:originalURL];
NSLog(@"Destination: %@",destination);
NSFileManager *defaultFileManager = [NSFileManager defaultManager];
if([defaultFileManager fileExistsAtPath:destination.absoluteString]){
[defaultFileManager removeItemAtURL:destination error:nil];
}
NSError *error;
@try {
[defaultFileManager copyItemAtURL:location toURL:destination error:&error];
} @catch (NSException *exception) {
NSLog(@"copy caught with exception: %@",exception);
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
_player = [AVPlayer playerWithURL:destination];
_playerVC = [AVPlayerViewController new];
_playerVC.player = _player;
[self presentViewController:_playerVC animated:YES completion:^{
[_player play];
}];
});
NSLog(@"Hello");
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data{
NSError *error2;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:1 error:&error2];
_requestedSong = [[dict valueForKey:@"results"] objectAtIndex:0];
NSLog(@"Searched: %@",[_requestedSong objectForKey:@"trackName"]);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
completionHandler(NSURLSessionResponseAllow);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
.h 文件:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>
@end
更新:
在使用 mad max 的实际 URL 端点而不是下载视频时,它还会显示一个空白视频。这一定意味着视频的格式或它与 AVPlayer 的交互方式有些奇怪。
【问题讨论】:
-
你检查过控制台的输出吗?这可能是 IOS 中不那么新的应用程序传输安全性。您需要允许来自您的源的连接,因为默认情况下 ios 会阻止任何不是 https 的内容。
-
我的控制台输出完全是我自己的。我已经允许任意加载,但这应该纯粹是下载相关的权利吗?我正在尝试查看本地视频文件。即便如此,我正在查看一个具有正确时间范围的空白屏幕作为我要查看的视频,所以我被允许访问该文件,所以我认为它不可能是这样的。
-
哦,我明白了,你是如何加载你的资产的?您可能希望为您的 AVPlayer 使用备用构造函数,以便您可以从文件中传递资产。您可以使用
(AVPlayerItem *)playerItemWithAsset:(AVAsset *)asset实例化您的资产,然后使用(instancetype)initWithPlayerItem:(AVPlayerItem *)item实例化您的AVPlayer。 -
得到相同的行为 AVAsset *asset = [AVAsset assetWithURL:destination]; AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset]; _player = [AVPlayer playerWithPlayerItem:item]; _playerVC = [AVPlayerViewController 新]; _playerVC.player = _player;
-
在问题中添加了 nsurl 打印
标签: ios objective-c video avplayer avplayerviewcontroller