iOS开发拓展篇—音频处理(音乐播放器5)
实现效果:
一、半透明滑块的设置
/**
*拖动滑块
*/
- (IBAction)panSlider:(UIPanGestureRecognizer *)sender { //1.获得挪动的距离
CGPoint t=[sender translationInView:sender.view];
//把挪动清零
[sender setTranslation:CGPointZero inView:sender.view]; //2.控制滑块和进度条的frame
CGFloat sliderMaxX=self.view.width-self.slider.width;
self.slider.x+=t.x;
//控制滑块的frame,不让其越界
if(self.slider.x<)
{
self.slider.x=;
}else if (self.slider.x>sliderMaxX)
{
self.slider.x=sliderMaxX;
}
//设置进度条的宽度
self.progressView.width=self.slider.center.x; //3.设置时间值
double progress=self.slider.x/sliderMaxX;
//当前的时间值=音乐的时长*当前的进度值
NSTimeInterval time=self.player.duration*progress;
[self.slider setTitle:[self strWithTime:time] forState:UIControlStateNormal]; //设置拖拽进度的X的值
self.currentTimeView.x=self.slider.x;
[self.currentTimeView setTitle:self.slider.currentTitle forState:UIControlStateNormal]; //4.如果开始拖动,那么就停止定时器
if (sender.state==UIGestureRecognizerStateBegan) {
//停止定时器
[self removeCurrentTime]; //设置拖拽进度
//显示
self.currentTimeView.hidden=NO;
self.currentTimeView.y=self.currentTimeView.superview.height--self.currentTimeView.height; }else if(sender.state==UIGestureRecognizerStateEnded)
{
//隐藏
self.currentTimeView.hidden=YES;
//设置播放器播放的时间
self.player.currentTime=time;
#warning 如果正在播放,才需要添加定时器
// if (self.player.isPlaying) {
//开启定时器
[self addCurrentTimeTimer];
// }
}
}
裁剪圆角的细节处理:
二、播放或暂停、上一首、下一首的实现
//上一首
- (IBAction)previous {
//1.在开始播放之前,禁用一切的app点击事件
UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
window.userInteractionEnabled=NO; //2.重置当前歌曲
[self resetPlayingMusic]; //3.获得上一首歌曲
[YYMusicTool setPlayingMusic:[YYMusicTool previousMusic]]; //4.播放上一首歌曲
[self starPlayingMusic]; //5.回复window的点击为可用
window.userInteractionEnabled=YES;
}
//下一首
- (IBAction)next {
//1.在开始播放之前,禁用一切的app点击事件
UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
window.userInteractionEnabled=NO; //2.重置当前歌曲
[self resetPlayingMusic]; //3.获得下一首歌曲
[YYMusicTool setPlayingMusic:[YYMusicTool nextMusic]]; //4.播放下一首歌曲
[self starPlayingMusic]; //5.回复window的点击为可用
window.userInteractionEnabled=YES;
} //继续或暂停播放
- (IBAction)playOrPause {
if (self.playOrPauseButton.isSelected) {//暂停
self.playOrPauseButton.selected=NO;
//暂停播放
[YYAudioTool pauseMusic:self.playingMusic.filename];
//停掉定时器
[self removeCurrentTime];
}else
{
self.playOrPauseButton.selected=YES;
//继续播放
[YYAudioTool playMusic:self.playingMusic.filename];
//开启定时器
[self addCurrentTimeTimer];
}
}
说明:播放和暂停按钮的图片设置在两种状态下并不一样,设置播放按钮的状态
三、对存在的bug进行改进
拖拽还存在问题(定时器的问题)
更好的方法时在添加定时器的地方进行更细的控制:
/**
* 添加一个定时器
*/
-(void)addCurrentTimeTimer
{
//如果当前没有在播放,那么就直接返回
if (self.player.isPlaying==NO) return; //在添加一个定时器之前,先把以前的定时器移除
[self removeCurrentTime]; //提前先调用一次进度更新,以保证定时器的工作时及时的
[self updateCurrentTime]; //创建一个定时器,每一秒钟调用一次
self.CurrentTimeTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];
//把定时器加入到运行时中
[[NSRunLoop mainRunLoop]addTimer:self.CurrentTimeTimer forMode:NSRunLoopCommonModes];
}
四、补充
完整的代码如下:
//
// YYPlayingViewController.m
// 20-音频处理(音乐播放器1)
//
// Created by apple on 14-8-13.
// Copyright (c) 2014年 yangyong. All rights reserved.
// #import "YYPlayingViewController.h"
#import "YYMusicTool.h"
#import "YYMusicModel.h"
#import "YYAudioTool.h" @interface YYPlayingViewController ()
//显示拖拽进度
@property (weak, nonatomic) IBOutlet UIButton *currentTimeView;
//进度条
@property (weak, nonatomic) IBOutlet UIView *progressView;
//滑块
@property (weak, nonatomic) IBOutlet UIButton *slider;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *songLabel;
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;
//当前播放的音乐的时长
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;
//正在播放的音乐
@property(nonatomic,strong)YYMusicModel *playingMusic;
//音乐播放器对象
@property(nonatomic,strong)AVAudioPlayer *player;
//定时器
@property(nonatomic,strong)NSTimer *CurrentTimeTimer;
- (IBAction)exit;
- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender;
- (IBAction)panSlider:(UIPanGestureRecognizer *)sender;
- (IBAction)previous;
- (IBAction)playOrPause;
- (IBAction)next;
@property (weak, nonatomic) IBOutlet UIButton *playOrPauseButton; @end @implementation YYPlayingViewController -(void)viewDidLoad
{
[super viewDidLoad]; //裁剪圆角
self.currentTimeView.layer.cornerRadius=; }
#pragma mark-公共方法
-(void)show
{
//1.禁用整个app的点击事件
UIWindow *window=[UIApplication sharedApplication].keyWindow;
window.userInteractionEnabled=NO; //2.添加播放界面
//设置View的大小为覆盖整个窗口
self.view.frame=window.bounds;
//设置view显示
self.view.hidden=NO;
//把View添加到窗口上
[window addSubview:self.view]; //3.检测是否换了歌曲
if (self.playingMusic!=[YYMusicTool playingMusic]) {
[self resetPlayingMusic];
} //4.使用动画让View显示
self.view.y=self.view.height;
[UIView animateWithDuration:0.25 animations:^{
self.view.y=;
} completion:^(BOOL finished) { //设置音乐数据
[self starPlayingMusic];
window.userInteractionEnabled=YES;
}];
} #pragma mark-私有方法
//重置正在播放的音乐
-(void)resetPlayingMusic
{
//1.重置界面数据
self.iconView.image=[UIImage imageNamed:@"play_cover_pic_bg"];
self.songLabel.text=nil;
self.singerLabel.text=nil; //2.停止播放
[YYAudioTool stopMusic:self.playingMusic.filename];
//把播放器进行清空
self.player=nil; //3.停止定时器
[self removeCurrentTime]; //4.设置音乐播放按钮的状态
self.playOrPauseButton.selected=NO;
}
//开始播放音乐数据
-(void)starPlayingMusic
{
//1.设置界面数据 //如果当前播放的音乐就是传入的音乐,那么就直接返回
if (self.playingMusic==[YYMusicTool playingMusic])
{
//把定时器加进去
[self addCurrentTimeTimer];
return;
}
//存取音乐
self.playingMusic=[YYMusicTool playingMusic];
self.iconView.image=[UIImage imageNamed:self.playingMusic.icon];
self.songLabel.text=self.playingMusic.name;
self.singerLabel.text=self.playingMusic.singer; //2.开始播放
self.player = [YYAudioTool playMusic:self.playingMusic.filename]; //3.设置时长
//self.player.duration; 播放器正在播放的音乐文件的时间长度
self.durationLabel.text=[self strWithTime:self.player.duration]; //4.添加定时器
[self addCurrentTimeTimer]; //5.设置音乐播放按钮的状态
self.playOrPauseButton.selected=YES;
} /**
*把时间长度-->时间字符串
*/
-(NSString *)strWithTime:(NSTimeInterval)time
{
int minute=time / ;
int second=(int)time % ;
return [NSString stringWithFormat:@"%d:%d",minute,second];
} #pragma mark-定时器控制
/**
* 添加一个定时器
*/
-(void)addCurrentTimeTimer
{
//如果当前没有在播放,那么就直接返回
if (self.player.isPlaying==NO) return; //在添加一个定时器之前,先把以前的定时器移除
[self removeCurrentTime]; //提前先调用一次进度更新,以保证定时器的工作时及时的
[self updateCurrentTime]; //创建一个定时器,每一秒钟调用一次
self.CurrentTimeTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];
//把定时器加入到运行时中
[[NSRunLoop mainRunLoop]addTimer:self.CurrentTimeTimer forMode:NSRunLoopCommonModes];
}
/**
*移除一个定时器
*/
-(void)removeCurrentTime
{
[self.CurrentTimeTimer invalidate]; //把定时器清空
self.CurrentTimeTimer=nil;
} /**
* 更新播放进度
*/
-(void)updateCurrentTime
{
//1.计算进度值
double progress=self.player.currentTime/self.player.duration; //2.计算滑块的x值
// 滑块的最大的x值
CGFloat sliderMaxX=self.view.width-self.slider.width;
self.slider.x=sliderMaxX*progress;
//设置滑块上的当前播放时间
[self.slider setTitle:[self strWithTime:self.player.currentTime] forState:UIControlStateNormal]; //3.设置进度条的宽度
self.progressView.width=self.slider.center.x; } #pragma mark-内部的按钮监听方法
//返回按钮
- (IBAction)exit { //0.移除定时器
[self removeCurrentTime];
//1.禁用整个app的点击事件
UIWindow *window=[UIApplication sharedApplication].keyWindow;
window.userInteractionEnabled=NO; //2.动画隐藏View
[UIView animateWithDuration:0.25 animations:^{
self.view.y=window.height;
} completion:^(BOOL finished) {
window.userInteractionEnabled=YES;
//设置view隐藏能够节省一些性能
self.view.hidden=YES;
}];
} /**
*点击了进度条
*/
- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender {
//获取当前单击的点
CGPoint point=[sender locationInView:sender.view];
//切换歌曲的当前播放时间
self.player.currentTime=(point.x/sender.view.width)*self.player.duration;
//更新播放进度
[self updateCurrentTime];
}
/**
*拖动滑块
*/
- (IBAction)panSlider:(UIPanGestureRecognizer *)sender { //1.获得挪动的距离
CGPoint t=[sender translationInView:sender.view];
//把挪动清零
[sender setTranslation:CGPointZero inView:sender.view]; //2.控制滑块和进度条的frame
CGFloat sliderMaxX=self.view.width-self.slider.width;
self.slider.x+=t.x;
//控制滑块的frame,不让其越界
if(self.slider.x<)
{
self.slider.x=;
}else if (self.slider.x>sliderMaxX)
{
self.slider.x=sliderMaxX;
}
//设置进度条的宽度
self.progressView.width=self.slider.center.x; //3.设置时间值
double progress=self.slider.x/sliderMaxX;
//当前的时间值=音乐的时长*当前的进度值
NSTimeInterval time=self.player.duration*progress;
[self.slider setTitle:[self strWithTime:time] forState:UIControlStateNormal]; //设置拖拽进度的X的值
self.currentTimeView.x=self.slider.x;
[self.currentTimeView setTitle:self.slider.currentTitle forState:UIControlStateNormal]; //4.如果开始拖动,那么就停止定时器
if (sender.state==UIGestureRecognizerStateBegan) {
//停止定时器
[self removeCurrentTime]; //设置拖拽进度
//显示
self.currentTimeView.hidden=NO;
self.currentTimeView.y=self.currentTimeView.superview.height--self.currentTimeView.height; }else if(sender.state==UIGestureRecognizerStateEnded)
{
//隐藏
self.currentTimeView.hidden=YES;
//设置播放器播放的时间
self.player.currentTime=time;
#warning 如果正在播放,才需要添加定时器
// if (self.player.isPlaying) {
//开启定时器
[self addCurrentTimeTimer];
// }
}
} //上一首
- (IBAction)previous {
//1.在开始播放之前,禁用一切的app点击事件
UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
window.userInteractionEnabled=NO; //2.重置当前歌曲
[self resetPlayingMusic]; //3.获得上一首歌曲
[YYMusicTool setPlayingMusic:[YYMusicTool previousMusic]]; //4.播放上一首歌曲
[self starPlayingMusic]; //5.回复window的点击为可用
window.userInteractionEnabled=YES;
}
//下一首
- (IBAction)next {
//1.在开始播放之前,禁用一切的app点击事件
UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
window.userInteractionEnabled=NO; //2.重置当前歌曲
[self resetPlayingMusic]; //3.获得下一首歌曲
[YYMusicTool setPlayingMusic:[YYMusicTool nextMusic]]; //4.播放下一首歌曲
[self starPlayingMusic]; //5.回复window的点击为可用
window.userInteractionEnabled=YES;
}
//继续或暂停播放
- (IBAction)playOrPause {
if (self.playOrPauseButton.isSelected) {//暂停
self.playOrPauseButton.selected=NO;
//暂停播放
[YYAudioTool pauseMusic:self.playingMusic.filename];
//停掉定时器
[self removeCurrentTime];
}else
{
self.playOrPauseButton.selected=YES;
//继续播放
[YYAudioTool playMusic:self.playingMusic.filename];
//开启定时器
[self addCurrentTimeTimer];
}
} @end