音视频开发-CMTime的运用

CMTime

/*!
	@typedef	CMTime
	@abstract	Rational time value represented as int64/int32.
*/
typedef struct
{
   
	CMTimeValue	value;		/*! @field value The value of the CMTime. value/timescale = seconds. */
	CMTimeScale	timescale;	/*! @field timescale The timescale of the CMTime. value/timescale = seconds.  */
	CMTimeFlags	flags;		/*! @field flags The flags, eg. kCMTimeFlags_Valid, kCMTimeFlags_PositiveInfinity, etc. */
	CMTimeEpoch	epoch;		/*! @field epoch Differentiates between equal timestamps that are actually different because
												 of looping, multi-item sequencing, etc.  
												 Will be used during comparison: greater epochs happen after lesser ones. 
												 Additions/subtraction is only possible within a single epoch,
												 however, since epoch length may be unknown/variable. */
} CMTime;

CMTime是专门针对视频时间的一种数据类型,首先我们探讨一下,有两个视频ABA视频共15帧,播放速度为

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用AVAudioRecorder和AVPlayer来实现在iOS设备上边录音边播放。具体步骤如下: 1. 配置AVAudioSession,设置播放和录音的类别和模式: ```swift let session = AVAudioSession.sharedInstance() try? session.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetoothA2DP]) try? session.setActive(true) ``` 2. 创建AVAudioRecorder实例,设置录音参数和文件路径: ```swift let recordSettings = [ AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 44100, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ] let audioURL = URL(fileURLWithPath: NSTemporaryDirectory() + "recording.m4a") let audioRecorder = try? AVAudioRecorder(url: audioURL, settings: recordSettings) audioRecorder?.prepareToRecord() audioRecorder?.record() ``` 3. 创建AVPlayer实例,设置播放URL和缓存: ```swift let audioURL = URL(string: "http://example.com/audio.mp3")! let playerItem = AVPlayerItem(url: audioURL) let player = AVPlayer(playerItem: playerItem) player.automaticallyWaitsToMinimizeStalling = false player.play() ``` 4. 在AVPlayer的代理方法中,检查缓存是否够用,如果不够用就暂停AVPlayer,等待缓存: ```swift func player(_ player: AVPlayer, didOutputCueMarkers markers: [AVMetadataItem], at time: CMTime) { let currentTime = player.currentTime() let bufferDuration = CMTimeGetSeconds(player.currentItem?.loadedTimeRanges.first?.timeRange.duration ?? CMTime.zero) let playbackDuration = CMTimeGetSeconds(currentTime) if bufferDuration - playbackDuration < 5 { player.pause() } else { player.play() } } ``` 这样就可以在iOS设备上边录音边播放了。注意,由于网络传输和设备性能的限制,可能会出现播放卡顿或录音失真的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值