gpt4 book ai didi

ios - 从 AVFullScreenViewController iOS 8 获取媒体链接

转载 作者:可可西里 更新时间:2023-11-01 03:34:58 32 4
gpt4 key购买 nike

我正在尝试在开始播放某些内容(例如任何 YouTube 视频)时获取视频链接。

首先我捕捉到视频开始播放的时间

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:) name:@"UIWindowDidBecomeVisibleNotification" object:nil];

然后延迟尝试获取链接

-(void)videoStarted:(NSNotification *)notification
{
NSLog(@"notification dic = %@", [notification userInfo]);
[self performSelector:@selector(detectModal) withObject:nil afterDelay:2.5f];
}

-(void)detectModal
{
CUViewController *controller = (CUViewController *)[appDelegate.window rootViewController].presentedViewController;
NSLog(@"Presented modal = %@", [appDelegate.window rootViewController].presentedViewController);
if(controller && [controller respondsToSelector:@selector(item)])
{
id currentItem = [controller item];
if(currentItem && [currentItem respondsToSelector:@selector(asset)])
{
AVURLAsset *asset = (AVURLAsset *)[currentItem asset];
if([asset respondsToSelector:@selector(URL)] && asset.URL)
[self newLinkDetected:[[asset URL] absoluteString]];
NSLog(@"asset find = %@", asset);
}
}
else
{
for (UIWindow *window in [UIApplication sharedApplication].windows) {
if ([window.rootViewController.presentedViewController isKindOfClass:NSClassFromString(@"AVFullScreenViewController")])
{
controller = (CUViewController *)window.rootViewController.presentedViewController;
for(int i = 0; i < [controller.view.subviews count]; i++)
{
UIView *topView = [controller.view.subviews objectAtIndex:i];
NSLog(@"top view = %@", topView);
for(int j = 0; j < [topView.subviews count]; j++)
{
UIView *subView = [topView.subviews objectAtIndex:j];
NSLog(@"sub view = %@", subView);
for (int k = 0; k < [subView.subviews count]; k++)
{
CUPlayerView *subsubView = (CUPlayerView *)[subView.subviews objectAtIndex:k];
NSLog(@"sub sub view = %@ class = %@", subsubView, NSStringFromClass([subsubView class]));
if([NSStringFromClass([subsubView class]) isEqualToString:@"AVVideoLayerView"])
{
NSLog(@"player view controller = %@", subsubView.playerController);
CUPlayerController *pController = subsubView.playerController;
NSLog(@"item = %@", [pController player]);
CUPlayerController *proxyPlayer = pController.playerControllerProxy;
if(proxyPlayer)
{
AVPlayer *player = [proxyPlayer player];
NSLog(@"find player = %@ chapters = %@", player, proxyPlayer.contentChapters);
break;
}
}
}
}
}
}
}
}
}

CUViewController、CUPlayerView、CUPlayerController - 伪类,看起来像这样

@interface CUPlayerController : UIViewController

@property(nonatomic, retain) id playerControllerProxy;
@property(nonatomic, retain) id player;
@property(nonatomic, retain) id item;

- (id)contentChapters;

@end

一切正常,直到这一行

NSLog(@"find player = %@ chapters = %@", player, proxyPlayer.contentChapters);

玩家总是零。也许有更简单的方法来获取媒体链接?

最佳答案

首先,我想关注播放 AVPlayerItem 的 AVPlayer。 AVPlayerItem 对象包含对 AVAsset 对象的引用和该 Assets 的演示设置。当您使用 AVPlayer 的 playerWithURL: 方法时,它会自动创建由 Assets 支持的 AVPlayerItem,该 Assets 是名为 AVURLAsset 的 AVAsset 的子类。 AVURLAsset 有一个 URL 属性。因此,如果您使用它,您可以获得当前播放项目的 NSURL。这是获取 URL 的示例函数:

-(NSURL *)urlOfCurrentlyPlayingInPlayer:(AVPlayer *)player{
// get current asset
AVAsset *currentPlayerAsset = player.currentItem.asset;
// make sure the current asset is an AVURLAsset
if (![currentPlayerAsset isKindOfClass:AVURLAsset.class]) return nil;
// return the NSURL
return [(AVURLAsset *)currentPlayerAsset URL];
}

我认为这只是您如何在您的代码中使用它的问题。希望这会有所帮助。

关于ios - 从 AVFullScreenViewController iOS 8 获取媒体链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31356999/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com