zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    第二周总结
    2019春总结作业
    第二次编程总结
    第四周作业
    第十二周作业
    第十一周作业
    第十周作业
    第九周作业
    第八周作业
    第六周作业
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7359750.html
Copyright © 2011-2022 走看看