AVFoundation – AVPlayer 播放音频

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods


一.前言

1.AVAsset

Assets  可以来自一个文件或用户的相册,可以理解为多媒体资源,通过 URL 作为一个 asset 对象的标识. 这个 URL 可以是本地文件路径或网络流;


2.AVAssetTrack

AVAsset 包含很多轨道 AVAssetTrack的结合,如 audio, video, text, closed captions, subtitles…


3.AVComposition / AVMutableComposition

使用  AVMutableComposition  类可以增删 AVAsset 来将单个或者多个 AVAsset 集合到一起,用来合成新视频。除此之外,若想将集合到一起的视听资源以自定义的方式进行播放,需要使用 AVMutableAudioMix 和 AVMutableVideoComposition 类对其中的资源进行协调管理;


4.AVMutableVideoComposition

AVFoundation 类 API 中最核心的类是 AVVideoComposition / AVMutableVideoComposition 。

AVVideoComposition / AVMutableVideoComposition 对两个或多个视频轨道组合在一起的方法给出了一个总体描述。它由一组时间范围和描述组合行为的介绍内容组成。这些信息出现在组合资源内的任意时间点。

AVVideoComposition / AVMutableVideoComposition 管理所有视频轨道,可以决定最终视频的尺寸,裁剪需要在这里进行;


5.AVMutableCompositionTrack

AVMutableCompositionTrack 是多个 AVAsset 集合到一起合成新视频中轨道信息,有音频轨、视频轨等,里面可以插入各种对应的素材(画中画,水印等);


6.AVMutableVideoCompositionLayerInstruction

AVMutableVideoCompositionLayerInstruction 主要用于对视频轨道中的一个视频处理缩放、模糊、裁剪、旋转等;


7.AVMutableVideoCompositionInstruction

表示一个指令,决定一个 timeRange 内每个轨道的状态,每一个指令包含多个 AVMutableVideoCompositionLayerInstruction ;而 AVVideoComposition 由多个 AVVideoCompositionInstruction 构成;

AVVideoCompositionInstruction 所提供的最关键的一段数据是组合对象时间轴内的时间范围信息。这一时间范围是在某一组合形式出现时的时间范围。要执行的组全特质是通过其 AVMutableVideoCompositionLayerInstruction 集合定义的。


8.AVAssetExportSession

AVAssetExportSession 主要用于导出视频;


9.AVAssetTrackSegment

AVAssetTrackSegment 不可变轨道片段;


10.AVCompositionTrackSegment

AVCompositionTrackSegment 可变轨道片段,继承自 AVAssetTrackSegment;


二.AVPlayer 播放音频

AVPlayer 播放音频《AVFoundation – AVPlayer 播放视频》类似,直接上代码:

- (BOOL)playVideoFile
{
    //创建一个资源实例
    NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"123.mp3" ofType:nil]];
    AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    
    //AVPlayerItem关联播放资源
    AVPlayerItem* item = [[AVPlayerItem alloc] initWithAsset:asset];
    
    //创建AVPlayer
    AVPlayer* player = [AVPlayer playerWithPlayerItem:item];
    
    //创建AVPlayerLayer用来显示资源内容
    AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    //设置frame
    playerLayer.frame = self.view.frame;
    //添加到当前的view,否则无法显示
    [self.view.layer addSublayer:playerLayer];
    
    //播放
    [player play];
    
    return YES;
}

关于这个 seektotime,拖动进度条跳转的时候,比如跳到 10 秒,结果跳到了 6 秒或者 8 秒,很奇怪!seektotime 跳转不准确 ?

解决办法: 使用这个方法 seekToTime:toleranceBefore:toleranceAfter: 通过设置偏差tolerance,来精确设定跳转的误差。

/*!
 @method            seekToTime:toleranceBefore:toleranceAfter:
 @abstract          Moves the playback cursor within a specified time bound.
 @param             time
 @param             toleranceBefore
 @param             toleranceAfter
 @discussion        Use this method to seek to a specified time for the current player item.
                    The time seeked to will be within the range [time-toleranceBefore, time+toleranceAfter] and may differ from the specified time for efficiency.
                    Pass kCMTimeZero for both toleranceBefore and toleranceAfter to request sample accurate seeking which may incur additional decoding delay. 
                    Messaging this method with beforeTolerance:kCMTimePositiveInfinity and afterTolerance:kCMTimePositiveInfinity is the same as messaging seekToTime: directly.
 */
- (void)seekToTime:(CMTime)time toleranceBefore:(CMTime)toleranceBefore toleranceAfter:(CMTime)toleranceAfter;

示例代码

    [_player seekToTime:playTime toleranceBefore:CMTimeMake(1, 1000) toleranceAfter:CMTimeMake(1, 1000)];

关于 seek 时间不准确的问题可以参考文章:《AVFoundation – AVPlayer 播放视频》,包含完整代码!!


三.猜你喜欢


ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容