短音频播放
/* 声音长度不能超过30秒
声音文件必须是 PCM 或者是 IMA4(IMA/ADPCM) 格式。
必须是 .caf、.aif 、.wav的文件
不能控制播放进度
没有循环播放
*/
// 导入#import
SystemSoundID soundID = 0;
// 获取音效文件的url
CFURLRef soundUrl = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:@"m_09.wav" withExtension:nil];
// 根据音效文件的url创建soundID
AudioServicesCreateSystemSoundID(soundUrl, &soundID);
// 播放soundID对应的音效
// 播放声音并震动,如果用户设置为静音就没声音,如果设置为震动就震动
AudioServicesPlayAlertSound(soundID);
// 仅播放声音,如果用户设置为静音就没声音
//AudioServicesPlaySystemSound(soundID);
本地音乐播放AVAudioPlayer(只能播放本地音乐)
- (IBAction)beginBtnClick:(id)sender {
if (!self.musicPlayer) {
// 获取音乐文件的url
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"120125029" withExtension:@"mp3"];
NSError *error;
// 创建播放器(一个播放器只能播放一个音乐)
AVAudioPlayer *musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
self.musicPlayer = musicPlayer;
// 播放前的准备工作
[musicPlayer prepareToPlay];
}
// 开始播放
[self.musicPlayer play];
}
- (IBAction)puseBtnClick:(id)sender {
// 暂停播放(不会清除preparetoplay方法做的准备工作)
[self.musicPlayer pause];
}
- (IBAction)stopBtnClick:(id)sender {
// 停止(会清除preparetoplay方法做的准备工作(但是测试不是这样的。。。))
[self.musicPlayer stop];
self.musicPlayer = nil;
}
常用属性
// 音乐的音量(默认1.0,可设置范围为0.0至1.0,两个极端为静音、系统音量
@property float volume;
// 左右声道的平衡(默认0.0,可设置范围为-1.0至1.0,两个极端分别为只有左声道、只有右声道)
@property float pan NS_AVAILABLE(10_7, 4_0);
// 播放速度(默认1.0,可设置范围为0.5至2.0,两个极端分别为一半速度、两倍速度)
@property float rate;
// 循环播放(默认1,若设置值大于0,则为相应的循环次数,设置为-1可以实现无限循环)
@property NSInteger numberOfLoops;
网络&本地音视频播放(AVPlayer,播放视频的话需要借助于AVPlayerLayer,支持播放进度监听)
使用到的相关类:
AVPlayer:控制播放器的播放,暂停,播放速度
AVURLAsset : AVAsset 的一个子类,使用 URL 进行实例化,实例化对象包含 URL 对应视频资源的所有信息。
AVPlayerItem:管理资源对象,提供播放数据源(一个item就是一个播放源,音视频切换只需切换播放源就行了)
AVPlayerLayer:负责显示视频,如果没有添加该类,只有声音没有画面
AVPlayer 类的常用方法
// 创建一个AVPlayer (此方法返回一个AVPlayer实例)
+ (instancetype)playerWithPlayerItem:(nullable AVPlayerItem *)item;
/* AVPlayer播放器的状态
typedef NS_ENUM(NSInteger, AVPlayerStatus) {
AVPlayerStatusUnknown, 位置状态
AVPlayerStatusReadyToPlay, 准备播放
AVPlayerStatusFailed 播放失败
};
*/
@property (nonatomic, readonly) AVPlayerStatus status;
// 播放错误信息(只有播放状态为AVPlayerStatusFailed才有值,否则为nil)
@property (nonatomic, readonly, nullable) NSError *error;
// 播放控制
// 倍率 0.0:暂停 1.0:按正常速率播放
@property (nonatomic) float rate;
// 播放、暂停
- (void)play;
- (void)pause;
// 切换资源
- (void)replaceCurrentItemWithPlayerItem:(nullable AVPlayerItem *)item;
// 设置当前播放时间 (调整播放进度)
NSTimeInterval currentTime = CMTimeGetSeconds(self.playerItem.duration) * sender.value;
[self.player seekToTime:CMTimeMakeWithSeconds(currentTime, NSEC_PER_SEC) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
CMTime是专门用于标识电影时间的结构体
typedef struct{
CMTimeValue value; // 帧数
CMTimeScale timescale; // 帧率(影片每秒有几帧)
CMTimeFlags flags;
CMTimeEpoch epoch;
} CMTime;
CMTime定义是一个C语言的结构体,CMTime是以分数的形式表示时间,value表示分子,timescale表示分母,flags是位掩码,表示时间的指定状态。
// seconds当前时间,preferredTimescale每秒钟多少帧.
CMTime CMTimeMakeWithSeconds(
Float64 seconds,
int32_t preferredTimescale)
__OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
函数获取时间的秒数:
CMTimeGetSeconds(t1)
// 获取总时间
NSTimeInterval duration = CMTimeGetSeconds(self.player.currentItem.duration);
// 获取当前播放时间
NSTimeInterval currentTime = CMTimeGetSeconds(self.player.currentTime);
demo地址:https://github.com/XPentaKill/AVPlayerDemo.git
****************************************************************************************
demo中用到了UISlide小控件
// 滑块当前的值
@property(nonatomic) float value;
// 滑块能划到的最小值,默认为0
@property(nonatomic) float minimumValue;
// 滑块能滑到的最大值,默认为1
@property(nonatomic) float maximumValue;
// 默认为yes,滑动的时候滑块的值随动;如果设置为NO,滑动结束的时候滑块的值才会变
@property(nonatomic,getter=isContinuous) BOOL continuous;
// 小于滑块当前值滑块条的颜色,默认为蓝色
@property(nullable, nonatomic,strong) UIColor *minimumTrackTintColor;
// 大于滑块当前值滑块条的颜色,默认为白色
@property(nullable, nonatomic,strong) UIColor *maximumTrackTintColor;
// 当前滑块的颜色,默认为白色
@property(nullable, nonatomic,strong) UIColor *thumbTintColor;
// 设置滑块的图片
- (void)setThumbImage:(nullable UIImage *)image forState:(UIControlState)state;
// 滑块条最小值处设置的图片
- (void)setMinimumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;
// 滑块条最大值处设置的图片
- (void)setMaximumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;