欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发中音频工具类的封装以及音乐播放器的细节控制

程序员文章站 2023-10-29 12:26:10
一、控制器间数据传递 两个控制器之间数据的传递 第一种方法:复制代码 代码如下:self.parentviewcontroller.music=self.music[i...

一、控制器间数据传递

两个控制器之间数据的传递
第一种方法:

复制代码 代码如下:
self.parentviewcontroller.music=self.music[indexpath.row];
不能满足
第二种做法:把整个数组传递给它
第三种做法:设置一个数据源,设置播放控制器的数据源是这个控制器。self.parentviewcontroller.datasource=self;好处:没有耦合性,任何实现了协议的可以作为数据源。
第四种做法:把整个项目会使用到的音频资源交给一个工具类去管理,这样就不用传递过去了。直接向工具类索要资源就可以。
 
二、封装一个音频工具类
新建一个音频工具类,用来管理音乐数据(音乐模型)

iOS开发中音频工具类的封装以及音乐播放器的细节控制

工具类中的代码设计如下:

复制代码 代码如下:

 yymusictool.h文件
//
//  yymusictool.h
//

#import <foundation/foundation.h>
@class yymusicmodel;
@interface yymusictool : nsobject
/**
 *  返回所有的歌曲
 */
+ (nsarray *)musics;

/**
 *  返回正在播放的歌曲
 */
+ (yymusicmodel *)playingmusic;
+ (void)setplayingmusic:(yymusicmodel *)playingmusic;

/**
 *  下一首歌曲
 */
+ (yymusicmodel *)nextmusic;

/**
 *  上一首歌曲
 */
+ (yymusicmodel *)previousmusic;
@end


yymusictool.m文件
复制代码 代码如下:

//
//  yymusictool.m
//

#import "yymusictool.h"
#import "yymusicmodel.h"
#import "mjextension.h"

@implementation yymusictool

static nsarray *_musics;
static  yymusicmodel *_playingmusic;

/**
 *  @return 返回所有的歌曲
 */
+(nsarray *)musics
{
    if (_musics==nil) {
        _musics=[yymusicmodel objectarraywithfilename:@"musics.plist"];
    }
    return _musics;
}

+(void)setplayingmusic:(yymusicmodel *)playingmusic
{
    /*
     *如果没有传入需要播放的歌曲,或者是传入的歌曲名不在音乐库中,那么就直接返回
      如果需要播放的歌曲就是当前正在播放的歌曲,那么直接返回
     */
    if (!playingmusic || ![[self musics]containsobject:playingmusic]) return;
    if (_playingmusic == playingmusic) return;
    _playingmusic=playingmusic;
}
/**
 *  返回正在播放的歌曲
 */
+(yymusicmodel *)playingmusic
{
    return _playingmusic;
}

/**
 *  下一首歌曲
 */
+(yymusicmodel *)nextmusic
{
    //设定一个初值
    int nextindex = 0;
    if (_playingmusic) {
        //获取当前播放音乐的索引
        int playingindex = [[self musics] indexofobject:_playingmusic];
        //设置下一首音乐的索引
        nextindex = playingindex+1;
        //检查数组越界,如果下一首音乐是最后一首,那么重置为0
        if (nextindex>=[self musics].count) {
            nextindex=0;
        }
    }
    return [self musics][nextindex];
}

/**
 *  上一首歌曲
 */
+(yymusicmodel *)previousmusic
{
    //设定一个初值
    int previousindex = 0;
    if (_playingmusic) {
        //获取当前播放音乐的索引
        int playingindex = [[self musics] indexofobject:_playingmusic];
        //设置下一首音乐的索引
        previousindex = playingindex-1;
        //检查数组越界,如果下一首音乐是最后一首,那么重置为0
        if (previousindex<0) {
            previousindex=[self musics].count-1;
        }
    }
    return [self musics][previousindex];
}
@end


三、封装一个音乐播放工具类

该工具类中的代码设计如下:

yyaudiotool.h文件

复制代码 代码如下:

//
//  yyaudiotool.h
//

#import <foundation/foundation.h>
#import <avfoundation/avfoundation.h>
@interface yyaudiotool : nsobject
/**
 *播放音乐文件
 */
+(bool)playmusic:(nsstring *)filename;
/**
 *暂停播放
 */
+(void)pausemusic:(nsstring *)filename;
/**
 *播放音乐文件
 */
+(void)stopmusic:(nsstring *)filename;

/**
 *播放音效文件
 */
+(void)playsound:(nsstring *)filename;
/**
 *销毁音效
 */
+(void)disposesound:(nsstring *)filename;
@end


yyaudiotool.m文件
复制代码 代码如下:

//
//  yyaudiotool.m
//

#import "yyaudiotool.h"

@implementation yyaudiotool
/**
 *存放所有的音乐播放器
 */
static nsmutabledictionary *_musicplayers;
+(nsmutabledictionary *)musicplayers
{
    if (_musicplayers==nil) {
        _musicplayers=[nsmutabledictionary dictionary];
    }
    return _musicplayers;
}

/**
 *存放所有的音效id
 */
static nsmutabledictionary *_soundids;
+(nsmutabledictionary *)soundids
{
    if (_soundids==nil) {
        _soundids=[nsmutabledictionary dictionary];
    }
    return _soundids;
}


/**
 *播放音乐
 */
+(bool)playmusic:(nsstring *)filename
{
    if (!filename) return no;//如果没有传入文件名,那么直接返回
    //1.取出对应的播放器
    avaudioplayer *player=[self musicplayers][filename];
   
    //2.如果播放器没有创建,那么就进行初始化
    if (!player) {
        //2.1音频文件的url
        nsurl *url=[[nsbundle mainbundle]urlforresource:filename withextension:nil];
        if (!url) return no;//如果url为空,那么直接返回
       
        //2.2创建播放器
        player=[[avaudioplayer alloc]initwithcontentsofurl:url error:nil];
       
        //2.3缓冲
        if (![player preparetoplay]) return no;//如果缓冲失败,那么就直接返回
       
        //2.4存入字典
        [self musicplayers][filename]=player;
    }
   
    //3.播放
    if (![player isplaying]) {
        //如果当前没处于播放状态,那么就播放
        return [player play];
    }

    return yes;//正在播放,那么就返回yes
}

+(void)pausemusic:(nsstring *)filename
{
    if (!filename) return;//如果没有传入文件名,那么就直接返回
   
    //1.取出对应的播放器
    avaudioplayer *player=[self musicplayers][filename];
   
    //2.暂停
    [player pause];//如果palyer为空,那相当于[nil pause],因此这里可以不用做处理

}

+(void)stopmusic:(nsstring *)filename
{
    if (!filename) return;//如果没有传入文件名,那么就直接返回
   
    //1.取出对应的播放器
    avaudioplayer *player=[self musicplayers][filename];
   
    //2.停止
    [player stop];
   
    //3.将播放器从字典中移除
    [[self musicplayers] removeobjectforkey:filename];
}

//播放音效
+(void)playsound:(nsstring *)filename
{
    if (!filename) return;
    //1.取出对应的音效
    systemsoundid soundid=[[self soundids][filename] unsignedintegervalue];
   
    //2.播放音效
    //2.1如果音效id不存在,那么就创建
    if (!soundid) {
       
        //音效文件的url
        nsurl *url=[[nsbundle mainbundle]urlforresource:filename withextension:nil];
        if (!url) return;//如果url不存在,那么就直接返回
       
        osstatus status = audioservicescreatesystemsoundid((__bridge cfurlref)(url), &soundid);
        nslog(@"%ld",status);
        //存入到字典中
        [self soundids][filename]=@(soundid);
    }
   
    //2.2有音效id后,播放音效
    audioservicesplaysystemsound(soundid);
}

//销毁音效
+(void)disposesound:(nsstring *)filename
{
    //如果传入的文件名为空,那么就直接返回
    if (!filename) return;
   
    //1.取出对应的音效
    systemsoundid soundid=[[self soundids][filename] unsignedintegervalue];
   
    //2.销毁
    if (soundid) {
        audioservicesdisposesystemsoundid(soundid);
       
        //2.1销毁后,从字典中移除
        [[self soundids]removeobjectforkey:filename];
    }
}
@end


四、在音乐播放控制器中的代码处理

yyplayingviewcontroller.m文件

复制代码 代码如下:

//
//  yyplayingviewcontroller.m
//

#import "yyplayingviewcontroller.h"
#import "yymusictool.h"
#import "yymusicmodel.h"
#import "yyaudiotool.h"

@interface yyplayingviewcontroller ()
@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;
- (ibaction)exit;

@end


复制代码 代码如下:

@implementation yyplayingviewcontroller
#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 rresetplayingmusic];
    }
   
    //4.使用动画让view显示
    self.view.y=self.view.height;
    [uiview animatewithduration:0.25 animations:^{
        self.view.y=0;
    } completion:^(bool finished) {
       
        //设置音乐数据
        [self starplayingmusic];
        window.userinteractionenabled=yes;
    }];
}
#pragma mark-私有方法
//重置正在播放的音乐
-(void)rresetplayingmusic
{
    //1.重置界面数据
    self.iconview.image=[uiimage imagenamed:@"play_cover_pic_bg"];
    self.songlabel.text=nil;
    self.singerlabel.text=nil;
   
    //2.停止播放
    [yyaudiotool stopmusic:self.playingmusic.filename];
}
//开始播放音乐数据
-(void)starplayingmusic
{
    //1.设置界面数据
   
    //取出当前正在播放的音乐
//    yymusicmodel *playingmusic=[yymusictool playingmusic];
   
    //如果当前播放的音乐就是传入的音乐,那么就直接返回
    if (self.playingmusic==[yymusictool playingmusic]) 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.开始播放
    [yyaudiotool playmusic:self.playingmusic.filename];
   
}

#pragma mark-内部的按钮监听方法
//返回按钮
- (ibaction)exit {
    //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;
    }];
}
@end


注意:先让用户看到界面上的所有东西后,再开始播放歌曲。

提示:一般的播放器需要做一个重置的操作。
  当从一首歌切换到另外一首时,应该先把上一首的信息删除,因此在show动画显示之前,应该检测是否换了歌曲,如果换了歌曲,则应该做一次重置操作。
 实现效果(能够顺利的切换和播放歌曲,下面是界面显示):

iOS开发中音频工具类的封装以及音乐播放器的细节控制iOS开发中音频工具类的封装以及音乐播放器的细节控制iOS开发中音频工具类的封装以及音乐播放器的细节控制

五、补充代码

yymusicsviewcontroller.m文件

复制代码 代码如下:

//
//  yymusicsviewcontroller.m
//

#import "yymusicsviewcontroller.h"
#import "yymusicmodel.h"
#import "mjextension.h"
#import "yymusiccell.h"
#import "yyplayingviewcontroller.h"
#import "yymusictool.h"

@interface yymusicsviewcontroller ()

@property(nonatomic,strong)yyplayingviewcontroller *playingviewcontroller;
@end


复制代码 代码如下:

@implementation yymusicsviewcontroller
#pragma mark-懒加载

-(yyplayingviewcontroller *)playingviewcontroller
{
    if (_playingviewcontroller==nil) {
        _playingviewcontroller=[[yyplayingviewcontroller alloc]init];
    }
    return _playingviewcontroller;
}

- (void)viewdidload
{
    [super viewdidload];
}

#pragma mark - table view data source
/**
 *一共多少组
 */
-(nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
    return 1;
}
/**
 *每组多少行
 */
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
    return [yymusictool musics].count;
}
/**
 *每组每行的cell
 */
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
    yymusiccell *cell=[yymusiccell cellwithtableview:tableview];
    cell.music=[yymusictool musics][indexpath.row];
    return cell;
}
/**
 *  设置每个cell的高度
 */
-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath
{
    return 70;
}

/**
 *  cell的点击事件
 */
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath
{
    //1.取消选中被点击的这行
    [tableview deselectrowatindexpath:indexpath animated:yes];
   
    //2.设置正在播放的歌曲
    [yymusictool setplayingmusic:[yymusictool musics][indexpath.row]];

    //调用公共方法
    [self.playingviewcontroller show];
   
//    //执行segue跳转
//    [self performseguewithidentifier:@"music2playing" sender:nil];
}
@end


六、一些细节控制
再来看一个实现的效果:

iOS开发中音频工具类的封装以及音乐播放器的细节控制

完整的代码

yyplayingviewcontroller.m文件

复制代码 代码如下:

//
//  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 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;

@end


复制代码 代码如下:

@implementation yyplayingviewcontroller
#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 rresetplayingmusic];
    }
   
    //4.使用动画让view显示
    self.view.y=self.view.height;
    [uiview animatewithduration:0.25 animations:^{
        self.view.y=0;
    } completion:^(bool finished) {
       
        //设置音乐数据
        [self starplayingmusic];
        window.userinteractionenabled=yes;
    }];
}


#pragma mark-私有方法
//重置正在播放的音乐
-(void)rresetplayingmusic
{
    //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];
}
//开始播放音乐数据
-(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];
   
}

/**
 *把时间长度-->时间字符串
 */
-(nsstring *)strwithtime:(nstimeinterval)time
{
    int minute=time / 60;
    int second=(int)time % 60;
    return [nsstring stringwithformat:@"%d:%d",minute,second];
}

#pragma mark-定时器控制
/**
 *  添加一个定时器
 */
-(void)addcurrenttimetimer
{
    //提前先调用一次进度更新,以保证定时器的工作时及时的
    [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
    self.slider.x+=t.x;
    //设置进度条的宽度
    self.progressview.width=self.slider.center.x;
   
    //3.设置时间值
    cgfloat slidermaxx=self.view.width-self.slider.width;
    double progress=self.slider.x/slidermaxx;
    //当前的时间值=音乐的时长*当前的进度值
    nstimeinterval time=self.player.duration*progress;
    [self .slider settitle:[self strwithtime:time] forstate:uicontrolstatenormal];
   
    //4.如果开始拖动,那么就停止定时器
    if (sender.state==uigesturerecognizerstatebegan) {
        //停止定时器
        [self removecurrenttime];
    }else if(sender.state==uigesturerecognizerstateended)
    {
        //设置播放器播放的时间
        self.player.currenttime=time;
        //开启定时器
        [self addcurrenttimetimer];
    }
}
@end


代码说明(一)

  调整开始播放音乐按钮,让其返回一个音乐播放器,而非bool型的。

复制代码 代码如下:

/**
 *播放音乐
 */
+(avaudioplayer *)playmusic:(nsstring *)filename
{
    if (!filename) return nil;//如果没有传入文件名,那么直接返回
    //1.取出对应的播放器
    avaudioplayer *player=[self musicplayers][filename];
   
    //2.如果播放器没有创建,那么就进行初始化
    if (!player) {
        //2.1音频文件的url
        nsurl *url=[[nsbundle mainbundle]urlforresource:filename withextension:nil];
        if (!url) return nil;//如果url为空,那么直接返回
       
        //2.2创建播放器
        player=[[avaudioplayer alloc]initwithcontentsofurl:url error:nil];
       
        //2.3缓冲
        if (![player preparetoplay]) return nil;//如果缓冲失败,那么就直接返回
       
        //2.4存入字典
        [self musicplayers][filename]=player;
    }
   
    //3.播放
    if (![player isplaying]) {
        //如果当前没处于播放状态,那么就播放
         [player play];
    }

    return player;//正在播放,那么就返回yes
}


代码说明(二)

  把时间转换为时间字符串的方法:

复制代码 代码如下:

/**
 *把时间长度-->时间字符串
 */
-(nsstring *)strwithtime:(nstimeinterval)time
{
    int minute=time / 60;
    int second=(int)time % 60;
    return [nsstring stringwithformat:@"%d:%d",minute,second];
}

代码说明(三)

  说明:进度控制

  监听当前的播放,使用一个定时器,不断的监听当前是第几秒。

  关于定时器的处理:这里使用了三个方法,分别是添加定时器,移除定时器,和更新播放进度。

注意细节:

(1)移除定时器后,对定时器进行清空处理。

复制代码 代码如下:

/**
 *移除一个定时器
 */
-(void)removecurrenttime
{
    [self.currenttimetimer invalidate];
   
    //把定时器清空
    self.currenttimetimer=nil;
}

(2)当看不到界面的时候,停止定时器。

(3)在开始播放音乐的方法中进行判断,如果当前播放的音乐和传入的音乐一致,那么添加定时器后直接返回。

(4)重置播放的音乐方法中,停止定时器。

代码说明(四)

  说明:点击和拖动进度条的处理

(1)点击进度条

  先添加单击的手势识别器。

iOS开发中音频工具类的封装以及音乐播放器的细节控制

往控制器拖线:

iOS开发中音频工具类的封装以及音乐播放器的细节控制

涉及的代码:

复制代码 代码如下:

/**
 *点击了进度条
 */
- (ibaction)tapprogressbg:(uitapgesturerecognizer *)sender {
    //获取当前单击的点
    cgpoint point=[sender locationinview:sender.view];
    //切换歌曲的当前播放时间
    self.player.currenttime=(point.x/sender.view.width)*self.player.duration;
    //更新播放进度
    [self updatecurrenttime];
}

(2)拖拽进度条

  先添加拖拽手势识别器

iOS开发中音频工具类的封装以及音乐播放器的细节控制

往控制器拖线

iOS开发中音频工具类的封装以及音乐播放器的细节控制

涉及的代码:

复制代码 代码如下:

/**
 *拖动滑块
 */
- (ibaction)panslider:(uipangesturerecognizer *)sender {
   
    //1.获得挪动的距离
    cgpoint t=[sender translationinview:sender.view];
    //把挪动清零
    [sender settranslation:cgpointzero inview:sender.view];
   
    //2.控制滑块和进度条的frame
    self.slider.x+=t.x;
    //设置进度条的宽度
    self.progressview.width=self.slider.center.x;
   
    //3.设置时间值
    cgfloat slidermaxx=self.view.width-self.slider.width;
    double progress=self.slider.x/slidermaxx;
    //当前的时间值=音乐的时长*当前的进度值
    nstimeinterval time=self.player.duration*progress;
    [self .slider settitle:[self strwithtime:time] forstate:uicontrolstatenormal];
   
    //4.如果开始拖动,那么就停止定时器
    if (sender.state==uigesturerecognizerstatebegan) {
        //停止定时器
        [self removecurrenttime];
    }else if(sender.state==uigesturerecognizerstateended)
    {
        //设置播放器播放的时间
        self.player.currenttime=time;
        //开启定时器
        [self addcurrenttimetimer];
    }
}