OCiOS開發:音頻播放器 AVAudioPlayer
簡單介紹
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