avplayer怎么用

1个回答

写回答

陈宇默

2023-01-09 19:00

+ 关注

苹果
苹果

AVPlayer是苹果提供的音视频播放器,可以用来播放本地或在线音视频文件。

下面是简单的使用方法:

1.导入AVFoundation框架。

IDEO
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){

//播放失败

}

}

}

举报有用(17分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号