简单介绍

  • AVAudioPlayer音频播放器可以提供简单的音频播放功能。其头文件包括在AVFoudation.framework中。

  • AVAudioPlayer未提供可视化界面,须要通过其提供的播放控制接口自行实现。

  • AVAudioPlayer仅能播放本地音频文件,并支持以下格式文件:.mp3、.m4a、.wav、.caf、.aif
。

经常用法

  • 初始化方法
// 1、NSURL 它仅仅能从file://格式的URL装入音频数据,不支持流式音频及HTTP流和网络流。

- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;

// 2、它使用一个指向内存中一些音频数据的NSData对象,这样的形式对于已经把音频数据下载到缓冲区的情形非常实用。
- (id)initWithData:(NSData *)data error:(NSError **)outError;
  • 音频操作方法
1、将音频资源加入音频队列,准备播放
- (BOOL)prepareToPlay; 2、開始播放音乐
- (BOOL)play; 3、在指定的延迟时间后開始播放
- (BOOL)playAtTime:(NSTimeInterval)time 4、暂停播放
- (void)pause; 5、停止播放
- (void)stop;

经常使用属性

  • playing:查看播放器是否处于播放状态

  • duration:获取音频播放总时长

  • delegate:设置托付

  • currentTime:设置播放当前时间

  • numberOfLoops:设置播放循环

  • pan:设置声道

  • rate:设置播放速度

AVAudioPlayerDelegate

  • AVAudioPlayerDelegate托付协议包括了大量的播放状态协议方法,来对播放的不同状态事件进行自己定义处理:
// 1、完毕播放
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag; // 2、播放失败
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error; // 3、音频中断 // 播放中断结束后,比方突然来的电话造成的中断
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags; - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player; - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags; - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player;

AVURLAsset获取专辑信息

通过AVURLAsset可获取mp3专辑信息,包括专辑名称、专辑图片、歌曲名、艺术家、专辑缩略图等信息。

commonKey

  • AVMetadataCommonKeyArtist:艺术家

  • AVMetadataCommonKeyTitle:音乐名

  • AVMetadataCommonKeyArtwork:专辑图片

  • AVMetadataCommonKeyAlbumName:专辑名称

获取步骤

steps 1:初始化

// 获取音频文件路径集合(获取全部的.mp3格式的文件路径)
NSArray *musicNames = [NSArray arrayWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:nil]]; // 获取最后一首歌曲(假定获取最后一首歌曲)的专辑信息
NSString *musicName = [musicNames.lastObject lastPathComponent]; // 依据歌曲名称获取音频路径
NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:musicName]; // 依据音频路径创建资源地址
NSURL *url = [NSURL fileURLWithPath:path]; // 初始化AVURLAsset
AVURLAsset *mp3Asset = [AVURLAsset URLAssetWithURL:url];

steps 2:遍历获取信息

// 遍历有效元数据格式
for (NSString *format in [mp3Asset availableMetadataFormats]) { // 依据数据格式获取AVMetadataItem(数据成员)。
// 依据AVMetadataItem属性commonKey可以获取专辑信息;
for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) {
// NSLog(@"%@", metadataItem); // 1、获取艺术家(歌手)名字commonKey:AVMetadataCommonKeyArtist
if ([metadataItem.commonKey isEqual:AVMetadataCommonKeyArtist]) {
NSLog(@"%@", (NSString *)metadataItem.value);
}
// 2、获取音乐名字commonKey:AVMetadataCommonKeyTitle
else if ([metadataItem.commonKey isEqual:AVMetadataCommonKeyTitle]) {
NSLog(@"%@", (NSString *)metadataItem.value);
}
// 3、获取专辑图片commonKey:AVMetadataCommonKeyArtwork
else if ([metadataItem.commonKey isEqual:AVMetadataCommonKeyArtwork]) {
NSLog(@"%@", (NSData *)metadataItem.value);
}
// 4、获取专辑名commonKey:AVMetadataCommonKeyAlbumName
else if ([metadataItem.commonKey isEqual:AVMetadataCommonKeyAlbumName]) {
NSLog(@"%@", (NSString *)metadataItem.value);
}
}
}

音频播放器案例

案例主要实现:播放、暂停、上一曲、下一曲、拖动滑条改变音量、拖动滑条改变当前进度、播放当前时间以及剩余时间显示、通过AVURLAsset类获取音频的专辑信息(包括专辑图片、歌手、歌曲名等)。

素材下载

下载地址:http://download.csdn.net/download/hierarch_lee/9415973

效果展示

代码演示样例

上述展示效果中,歌曲名称实际上是导航栏标题。我仅仅是将导航栏背景颜色设置成黑色。标题颜色以及状态栏颜色设置成白色。

加入导航栏、导航栏配置以及改动状态栏颜色。这里省略,以下直接贴上实现代码。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> typedef enum : NSUInteger {
PlayAndPauseBtnTag,
NextMusicBtnTag,
LastMusicBtnTag,
} BtnTag; #define TIME_MINUTES_KEY @"minutes" // 时间_分_键
#define TIME_SECONDS_KEY @"seconds" // 时间_秒_键 #define RGB_COLOR(_R,_G,_B) [UIColor colorWithRed:_R/255.0 green:_G/255.0 blue:_B/255.0 alpha:1] @interface ViewController () <AVAudioPlayerDelegate> {
NSTimer *_timer; /**< 定时器 */ BOOL _playing; /**< 播放状态 */
BOOL _shouldUpdateProgress; /**< 是否更新进度指示器 */ NSArray *_musicNames; /**< 音乐名集合 */ NSInteger _currentMusicPlayIndex; /**< 当前音乐播放下标 */
} @property (nonatomic, strong) UILabel *singerLabel;/**< 歌手 */
@property (nonatomic, strong) UIImageView *imageView;/**< 专辑图片 */
@property (nonatomic, strong) UIImageView *volumeImageView; /**< 音量图片 */ @property (nonatomic, strong) UISlider *slider;/**< 进度指示器 */
@property (nonatomic, strong) UISlider *volumeSlider;/**< 音量滑条 */ @property (nonatomic, strong) UIButton *playAndPauseBtn; /**< 暂停播放button */
@property (nonatomic, strong) UIButton *nextMusicBtn; /**< 下一曲button */
@property (nonatomic, strong) UIButton *lastMusicBtn; /**< 上一曲button */ @property (nonatomic, strong) UILabel *currentTimeLabel; /**< 当前时间 */
@property (nonatomic, strong) UILabel *remainTimeLabel; /**< 剩余时间 */ @property (nonatomic, strong) AVAudioPlayer *audioPlayer; /**< 音频播放器 */ // 初始化
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
- (void)initializeAudioPlayerWithMusicName:(NSString *)musicName shouleAutoPlay:(BOOL)autoPlay; /**< 初始化音频播放器 */ // 更新
- (void)updateSlider; /**< 刷新滑条 */
- (void)updateUserInterface; /**< 刷新用户界面 */
- (void)updateTimeDisplayWithDuration:(NSTimeInterval)duration currentTime:(NSTimeInterval)currentTime; /**< 更新时间显示 */ // 定时器
- (void)startTimer; /**< 启动定时器 */
- (void)pauseTimer; /**< 暂停定时器 */
- (void)stopTimer; /**< 停止定时器 */ // 事件方法
- (void)respondsToButton:(UIButton *)sender; /**< 点击button */ - (void)respondsToSliderEventValueChanged:(UISlider *)sender; /**< 拖动滑条 */
- (void)respondsToSliderEventTouchDown:(UISlider *)sender; /**< 按下滑条 */
- (void)respondsToSliderEventTouchUpInside:(UISlider *)sender; /**< 滑条按下抬起 */
- (void)respondsToVolumeSlider:(UISlider *)sender; /**< 拖动音量滑条 */ // 其它方法
- (NSDictionary *)handleWithTime:(NSTimeInterval)time; /**< 处理时间 */ @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self initializeDataSource];
[self initializeUserInterface];
} #pragma mark *** Initialize methods ***
- (void)initializeDataSource { // 赋值是否播放。初始化音频播放器时。不播放音乐
_playing = NO; // 赋值是否刷新进度指示
_shouldUpdateProgress = YES; // 设置当前播放音乐下标
_currentMusicPlayIndex = 0; // 获取bundle路径全部的mp3格式文件集合
_musicNames = [NSArray arrayWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:nil]]; [_musicNames enumerateObjectsUsingBlock:^(NSString * _Nonnull musicName, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", musicName.lastPathComponent);
}]; } - (void)initializeUserInterface { self.view.backgroundColor = [UIColor blackColor];
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:25],
NSForegroundColorAttributeName:[UIColor whiteColor]}; // 载入视图
[self.view addSubview:self.singerLabel];
[self.view addSubview:self.imageView];
[self.view addSubview:self.slider]; [self.view addSubview:self.playAndPauseBtn];
[self.view addSubview:self.nextMusicBtn];
[self.view addSubview:self.lastMusicBtn]; [self.view addSubview:self.currentTimeLabel];
[self.view addSubview:self.remainTimeLabel]; [self.view addSubview:self.volumeSlider];
[self.view addSubview:self.volumeImageView]; // 初始化音乐播放器
[self initializeAudioPlayerWithMusicName:_musicNames[_currentMusicPlayIndex] shouleAutoPlay:_playing];
} - (void)initializeAudioPlayerWithMusicName:(NSString *)musicName shouleAutoPlay:(BOOL)autoPlay {
// 异常处理
if (musicName.length == 0) {
return;
} NSError *error = nil; // 获取音频地址
NSURL *url = [[NSBundle mainBundle] URLForAuxiliaryExecutable:musicName]; // 初始化音频播放器
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; // 设置代理
self.audioPlayer.delegate = self; // 推断是否异常
if (error) {
// 打印异常描写叙述信息
NSLog(@"%@", error.localizedDescription);
}else { // 设置音量
self.audioPlayer.volume = self.volumeSlider.value; // 准备播放
[self.audioPlayer prepareToPlay]; // 播放持续时间
NSLog(@"音乐持续时间:%.2f", self.audioPlayer.duration); if (autoPlay) {
[self.audioPlayer play];
}
} // 更新用户界面
[self updateUserInterface];
} #pragma mark *** Timer ***
- (void)startTimer {
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}
_timer.fireDate = [NSDate date];
} - (void)pauseTimer {
_timer.fireDate = [NSDate distantFuture];
} - (void)stopTimer {
// 销毁定时器
if ([_timer isValid]) {
[_timer invalidate];
}
} #pragma mark *** Events ***
- (void)respondsToButton:(UIButton *)sender {
switch (sender.tag) {
// 暂停播放
case PlayAndPauseBtnTag: {
if (!_playing) {
[_audioPlayer play];
[self startTimer];
}else {
[_audioPlayer pause];
[self pauseTimer];
}
_playing = !_playing;
sender.selected = _playing;
}
break;
// 上一曲
case LastMusicBtnTag: {
_currentMusicPlayIndex = _currentMusicPlayIndex == 0 ? _musicNames.count - 1 : --_currentMusicPlayIndex;
[self initializeAudioPlayerWithMusicName:[_musicNames[_currentMusicPlayIndex] lastPathComponent] shouleAutoPlay:_playing];
}
break;
// 下一曲
case NextMusicBtnTag: {
_currentMusicPlayIndex = _currentMusicPlayIndex == _musicNames.count - 1 ? 0 : ++_currentMusicPlayIndex;
[self initializeAudioPlayerWithMusicName:[_musicNames[_currentMusicPlayIndex] lastPathComponent] shouleAutoPlay:_playing];
}
break; default:
break;
}
} // 拖动滑条更新时间显示
- (void)respondsToSliderEventValueChanged:(UISlider *)sender {
[self updateTimeDisplayWithDuration:sender.maximumValue currentTime:sender.value];
} // 滑条按下时停止更新滑条
- (void)respondsToSliderEventTouchDown:(UISlider *)sender {
_shouldUpdateProgress = NO;
} // 滑条按下抬起,更新当前音乐播放时间
- (void)respondsToSliderEventTouchUpInside:(UISlider *)sender {
_shouldUpdateProgress = YES;
self.audioPlayer.currentTime = sender.value;
} - (void)respondsToVolumeSlider:(UISlider *)sender {
self.audioPlayer.volume = sender.value;
} #pragma mark *** Update methods ***
- (void)updateSlider {
// 在拖动滑条的过程中,停止刷新播放进度
if (_shouldUpdateProgress) {
// 更新进度指示
self.slider.value = self.audioPlayer.currentTime;
// 更新时间显示
[self updateTimeDisplayWithDuration:self.audioPlayer.duration currentTime:self.audioPlayer.currentTime];
}
} - (void)updateUserInterface { /* 进度指示更新 */
self.slider.value = 0.0;
self.slider.minimumValue = 0.0;
self.slider.maximumValue = self.audioPlayer.duration; /* 标题更新 */
self.title = [_musicNames[_currentMusicPlayIndex] substringToIndex:((NSString *)_musicNames[_currentMusicPlayIndex]).length - 4]; /* 获取MP3专辑信息 */
// 获取音乐路径
NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:[_musicNames[_currentMusicPlayIndex] lastPathComponent]];
// 依据音乐路径创建url资源地址
NSURL *url = [NSURL fileURLWithPath:path];
// 初始化AVURLAsset
AVURLAsset *map3Asset = [AVURLAsset assetWithURL:url];
// 遍历有效元数据格式
for (NSString *format in [map3Asset availableMetadataFormats]) {
// 依据数据格式获取AVMetadataItem数据成员
for (AVMetadataItem *metadataItem in [map3Asset metadataForFormat:format]) {
// 获取专辑图片commonKey:AVMetadataCommonKeyArtwork
if ([metadataItem.commonKey isEqualToString:AVMetadataCommonKeyArtwork]) {
UIImage *image = [UIImage imageWithData:(NSData *)metadataItem.value];
// 更新专辑图片
self.imageView.image = image;
}
// 获取音乐名字commonKey:AVMetadataCommonKeyTitle
else if([metadataItem.commonKey isEqualToString:AVMetadataCommonKeyTitle]){
// 更新音乐名称
self.title = (NSString *)metadataItem.value;
}
// 获取艺术家(歌手)名字commonKey:AVMetadataCommonKeyArtist
else if ([metadataItem.commonKey isEqual:AVMetadataCommonKeyArtist]){
// 更新歌手
self.singerLabel.text = [NSString stringWithFormat:@"- %@ -", metadataItem.value];
}
}
}
} - (void)updateTimeDisplayWithDuration:(NSTimeInterval)duration currentTime:(NSTimeInterval)currentTime {
/* 处理当前时间 */
NSDictionary *currentTimeInfoDict = [self handleWithTime:currentTime];
// 取出相应的分秒组件
NSInteger currentMinutes = [[currentTimeInfoDict objectForKey:TIME_MINUTES_KEY] integerValue];
NSInteger currentSeconds = [[currentTimeInfoDict objectForKey:TIME_SECONDS_KEY] integerValue];
// 时间格式处理
NSString *currentTimeFormat = currentSeconds < 10 ? @"0%d:0%d" : @"0%d:%d";
NSString *currentTimeString = [NSString stringWithFormat:currentTimeFormat, currentMinutes, currentSeconds];
self.currentTimeLabel.text = currentTimeString; /* 处理剩余时间 */
NSDictionary *remainTimeInfoDict = [self handleWithTime:duration - currentTime];
// 取出相应的过分秒组件
NSInteger remainMinutes = [[remainTimeInfoDict objectForKey:TIME_MINUTES_KEY] integerValue];
NSInteger remainSeconds = [[remainTimeInfoDict objectForKey:TIME_SECONDS_KEY] integerValue];
// 时间格式处理
NSString *remainTimeFormat = remainSeconds < 10 ? @"0%d:0%d" : @"-0%d:%d";
NSString *remainTimeString = [NSString stringWithFormat:remainTimeFormat, remainMinutes, remainSeconds];
self.remainTimeLabel.text = remainTimeString;
} - (NSDictionary *)handleWithTime:(NSTimeInterval)time {
NSMutableDictionary *timeInfomationDict = [@{} mutableCopy];
// 获取分
NSInteger minutes = (NSInteger)time/60;
// 获取秒
NSInteger seconds = (NSInteger)time%60;
// 打包字典
[timeInfomationDict setObject:@(minutes) forKey:TIME_MINUTES_KEY];
[timeInfomationDict setObject:@(seconds) forKey:TIME_SECONDS_KEY];
return timeInfomationDict;
} #pragma mark *** AVAudioPlayerDelegate ***
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[self respondsToButton:self.nextMusicBtn];
} #pragma mark *** Setters ***
- (void)setAudioPlayer:(AVAudioPlayer *)audioPlayer {
// 在设置音频播放器的时候,假设正在播放,则先暂停音乐再进行配置
if (_audioPlayer.playing) {
[_audioPlayer stop];
}
_audioPlayer = audioPlayer;
} #pragma mark *** Getters ***
- (UILabel *)singerLabel {
if (!_singerLabel) {
_singerLabel = [[UILabel alloc] init];
_singerLabel.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 40);
_singerLabel.center = CGPointMake(CGRectGetMidX(self.view.bounds), 64 + CGRectGetMidY(_singerLabel.bounds));
_singerLabel.textColor = [UIColor whiteColor];
_singerLabel.font = [UIFont systemFontOfSize:17];
_singerLabel.textAlignment = NSTextAlignmentCenter;
}
return _singerLabel;
} - (UIImageView *)imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] init];
_imageView.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) - 180, CGRectGetWidth(self.view.bounds) - 180);
_imageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.singerLabel.frame) + CGRectGetMidY(_imageView.bounds) + 30);
_imageView.backgroundColor = [UIColor cyanColor];
_imageView.layer.cornerRadius = CGRectGetMidX(_imageView.bounds);
_imageView.layer.masksToBounds = YES;
_imageView.alpha = 0.85;
}
return _imageView;
} - (UISlider *)slider {
if (!_slider) {
_slider = [[UISlider alloc] init];
_slider.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) - 100, 30);
_slider.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.imageView.frame) + CGRectGetMidY(_slider.bounds) + 50);
_slider.maximumTrackTintColor = [UIColor lightGrayColor];
_slider.minimumTrackTintColor = RGB_COLOR(30, 177, 74);
_slider.layer.masksToBounds = YES; // 自己定义拇指图片
[_slider setThumbImage:[UIImage imageNamed:@"iconfont-yuan"] forState:UIControlStateNormal]; // 事件监听
[_slider addTarget:self action:@selector(respondsToSliderEventValueChanged:) forControlEvents:UIControlEventValueChanged];
[_slider addTarget:self action:@selector(respondsToSliderEventTouchDown:) forControlEvents:UIControlEventTouchDown];
[_slider addTarget:self action:@selector(respondsToSliderEventTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; }
return _slider;
} - (UIButton *)playAndPauseBtn {
if (!_playAndPauseBtn) {
_playAndPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_playAndPauseBtn.bounds = CGRectMake(0, 0, 100, 100);
_playAndPauseBtn.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.slider.frame) + CGRectGetMidY(_playAndPauseBtn.bounds) + 30);
_playAndPauseBtn.tag = PlayAndPauseBtnTag;
[_playAndPauseBtn setImage:[UIImage imageNamed:@"iconfont-bofang"] forState:UIControlStateNormal];
[_playAndPauseBtn setImage:[UIImage imageNamed:@"iconfont-zanting"] forState:UIControlStateSelected];
[_playAndPauseBtn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _playAndPauseBtn;
} - (UIButton *)nextMusicBtn {
if (!_nextMusicBtn) {
_nextMusicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_nextMusicBtn.bounds = CGRectMake(0, 0, 60, 60);
_nextMusicBtn.center = CGPointMake(CGRectGetMaxX(self.playAndPauseBtn.frame) + CGRectGetMidX(_nextMusicBtn.bounds) + 20, CGRectGetMidY(self.playAndPauseBtn.frame));
_nextMusicBtn.tag = NextMusicBtnTag;
[_nextMusicBtn setImage:[UIImage imageNamed:@"iconfont-xiayiqu"] forState:UIControlStateNormal];
[_nextMusicBtn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _nextMusicBtn;
} - (UIButton *)lastMusicBtn {
if (!_lastMusicBtn) {
_lastMusicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_lastMusicBtn.bounds = CGRectMake(0, 0, 60, 60);
_lastMusicBtn.center = CGPointMake(CGRectGetMinX(self.playAndPauseBtn.frame) - 20 - CGRectGetMidX(_lastMusicBtn.bounds), CGRectGetMidY(self.playAndPauseBtn.frame));
_lastMusicBtn.tag = LastMusicBtnTag;
[_lastMusicBtn setImage:[UIImage imageNamed:@"iconfont-shangyiqu"] forState:UIControlStateNormal];
[_lastMusicBtn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _lastMusicBtn;
} - (UILabel *)currentTimeLabel {
if (!_currentTimeLabel) {
_currentTimeLabel = [[UILabel alloc] init];
_currentTimeLabel.bounds = CGRectMake(0, 0, 50, 30);
_currentTimeLabel.center = CGPointMake(CGRectGetMidX(_currentTimeLabel.bounds), CGRectGetMidY(self.slider.frame));
_currentTimeLabel.textAlignment = NSTextAlignmentRight;
_currentTimeLabel.font = [UIFont systemFontOfSize:14];
_currentTimeLabel.textColor = [UIColor lightGrayColor];
_currentTimeLabel.text = @"00:00";
}
return _currentTimeLabel;
} - (UILabel *)remainTimeLabel {
if (!_remainTimeLabel) {
_remainTimeLabel = [[UILabel alloc] init];
_remainTimeLabel.bounds = self.currentTimeLabel.bounds;
_remainTimeLabel.center = CGPointMake(CGRectGetMaxX(self.slider.frame) + CGRectGetMidX(_remainTimeLabel.bounds), CGRectGetMidY(self.slider.frame));
_remainTimeLabel.font = [UIFont systemFontOfSize:14];
_remainTimeLabel.textColor = [UIColor lightGrayColor];
_remainTimeLabel.text = @"00:00";
}
return _remainTimeLabel;
} - (UIImageView *)volumeImageView {
if (!_volumeImageView) {
_volumeImageView = [[UIImageView alloc] init];
_volumeImageView.bounds = CGRectMake(0, 0, 35, 35);
_volumeImageView.center = CGPointMake(20 + CGRectGetMidX(_volumeImageView.bounds), CGRectGetMaxY(self.playAndPauseBtn.frame) + CGRectGetMidY(_volumeImageView.bounds) + 40);
_volumeImageView.image = [UIImage imageNamed:@"iconfont-yinliang"];
}
return _volumeImageView;
} - (UISlider *)volumeSlider {
if (!_volumeSlider) {
_volumeSlider = [[UISlider alloc] init];
_volumeSlider.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) - 2 * CGRectGetMinX(self.volumeImageView.frame) - CGRectGetWidth(self.volumeImageView.bounds), CGRectGetHeight(self.slider.bounds));
_volumeSlider.center = CGPointMake(CGRectGetMaxX(self.volumeImageView.frame) + CGRectGetMidX(_volumeSlider.bounds), CGRectGetMidY(self.volumeImageView.frame));
_volumeSlider.maximumTrackTintColor = [UIColor lightGrayColor];
_volumeSlider.minimumTrackTintColor = RGB_COLOR(30, 177, 74);
_volumeSlider.minimumValue = 0.0;
_volumeSlider.maximumValue = 1.0;
_volumeSlider.value = 0.5;
[_volumeSlider setThumbImage:[UIImage imageNamed:@"iconfont-yuan"] forState:UIControlStateNormal];
[_volumeSlider addTarget:self action:@selector(respondsToVolumeSlider:) forControlEvents:UIControlEventValueChanged];
}
return _volumeSlider;
}
@end

OCiOS开发:音频播放器 AVAudioPlayer的更多相关文章

  1. iOS开发----音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  2. IOS开发之简单音频播放器

    今天第一次接触IOS开发的UI部分,之前学OC的时候一直在模拟的使用Target-Action回调模式,今天算是真正的用了一次.为了熟悉一下基本控件的使用方法,和UI部分的回调,下面开发了一个特别简易 ...

  3. 基于c开发的全命令行音频播放器

    cmus是一个内置了音频播放器的强大的音乐文件管理器.用它的基于ncurses的命令行界面,你可以浏览你的音乐库,并从播放列表或队列中播放音乐,这一切都是在命令行下. Linux上安装cmus 首先, ...

  4. HTML5 音频播放器-Javascript代码(短小精悍)

    直接上干货咯! //HTML5 音频播放器 lzpong 2015/01/19 var wavPlayer = function () { if(window.parent.wavPlayer) re ...

  5. 【jquery】一款不错的音频播放器——Amazing Audio Player

    前段时间分享了一款视频播放器,点击这里.今天介绍一款不错的音频播放器——Amazing Audio Player. 介绍: Amazing Audio Player 是一个使用很方便的 Windows ...

  6. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...

  7. Unity3D音频播放器 动态装载组件

    大多数在线Unity有关如何只教程Unity在播放音乐.之后如何通过拖动它们无法继续添加音频文件 但有时在游戏中的对象要玩几个声音.这时候我们就需要使用代码控制,拖动推教程AudioClip颂值的方法 ...

  8. 最简单的基于FFMPEG+SDL的音频播放器 ver2 (采用SDL2.0)

    ===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...

  9. 最简单的基于FFMPEG+SDL的音频播放器 ver2 (採用SDL2.0)

    ===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...

随机推荐

  1. Android Developers:支持不同的屏幕密度

    这节课程向你展示如何通过提供不同的资源和使用与分辨率无关的测量单位,支持不同屏幕密度. 使用密度无关的像素 —————————————————————————————————————————————— ...

  2. Java Date and Calendar examples

    Java Date and Calendar examples This tutorial shows you how to work with java.util.Date and java.uti ...

  3. ElementUI 按需引入坑爹的点记录

    官网说的是这样的: 但实际上,应该是这样修改: { "presets": [ ["env", { "targets": { "br ...

  4. 如何上传代码到github?

    如何上传代码到github? 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安 ...

  5. supervisor 安装脚本

    mkdir /data/tools && cd /data/tools wget --no-check-certificate https://bootstrap.pypa.io/ez ...

  6. Xilinx FPGA 的PCIE 设计

    写在前面 近两年来和几个单位接触下来,发现PCIe还是一个比较常用的,有些难度的案例,主要是涉及面比较广,需要了解逻辑设计.高速总线.Linux和Windows的驱动设计等相关知识. 这篇文章主要针对 ...

  7. sql server获取时间格式

    在本文中,GetDate()获得的日期由两部分组成,分别是今天的日期和当时的时间: Select GetDate()  用DateName()就可以获得相应的年.月.日,然后再把它们连接起来就可以了: ...

  8. markModified声明要修改的数组字段

    更新一个文档的字段的时候,如果该字段的类型是数组类型,则必须在更新保存前声明一下这个数组字段要被修改,否则这个数组字段的值不会被修改.如 article.markModified('categorys ...

  9. zookeeper的原理讲解

    留着以后看:http://blog.csdn.net/u010311445/article/category/1677839

  10. windows开通https服务

    一.申请ssl证书 建议1个免费的ssl证书申请网站,已测试,可用 1.注册https://login.wosign.com/reg.html?rf=buy 2.邮箱验证登录后访问https://bu ...