
苹果
下面是简单的使用方法:
1.导入AVFoundation框架。

IDEO
#import
2.创建AVPlayer实例。
AVPlayer*player=[[AVPlayeralloc]initWithURL:[NSURLURLWithString:@"http://example.com/vIDEO.mp4"]];
3.创建AVPlayerLayer实例并添加到视图。
AVPlayerLayer*playerLayer=[AVPlayerLayerplayerLayerWithPlayer:player];
playerLayer.frame=self.view.bounds;
[self.view.layeraddSublayer:playerLayer];
4.开始播放。
[playerplay];
其他常用方法:
-暂停
[playerpause];
-跳转到指定时间
CMTimetime=CMTimeMakeWithSeconds(30,1);
[playerseekToTime:time];
-监听播放状态
[playeraddObserver:selfforKeyPath:@"status"options:NSKeyValueObservingOptionNewcontext:nil];
-实现观察者方法
-(void)observeValueForKeyPath:(NSString*)keyPathofObject:(id)objectchange:(NSDictionary*)changecontext:(void*)context{
if([keyPathisEqualToString:@"status"]){
if(player.status==AVPlayerStatusReadyToPlay){
//可以开始播放
}elseif(player.status==AVPlayerStatusFAIled){
//播放失败
}
}
}
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号