欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)中音頻工具類的封裝以及音樂播放器的細(xì)節(jié)控制

 更新時(shí)間:2015年12月29日 09:31:27   作者:文頂頂  
這篇文章主要介紹了iOS開發(fā)中音頻工具類的封裝以及音樂播放器的細(xì)節(jié)控制,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

一、控制器間數(shù)據(jù)傳遞

兩個(gè)控制器之間數(shù)據(jù)的傳遞
第一種方法:

復(fù)制代碼 代碼如下:
self.parentViewController.music=self.music[indexPath.row];
不能滿足
第二種做法:把整個(gè)數(shù)組傳遞給它
第三種做法:設(shè)置一個(gè)數(shù)據(jù)源,設(shè)置播放控制器的數(shù)據(jù)源是這個(gè)控制器。self.parentViewController.dataSource=self;好處:沒有耦合性,任何實(shí)現(xiàn)了協(xié)議的可以作為數(shù)據(jù)源。
第四種做法:把整個(gè)項(xiàng)目會(huì)使用到的音頻資源交給一個(gè)工具類去管理,這樣就不用傳遞過去了。直接向工具類索要資源就可以。
 
二、封裝一個(gè)音頻工具類
新建一個(gè)音頻工具類,用來管理音樂數(shù)據(jù)(音樂模型)

2015122992144575.png (516×121)

工具類中的代碼設(shè)計(jì)如下:

復(fù)制代碼 代碼如下:

 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文件
復(fù)制代碼 代碼如下:

//
//  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
{
    /*
     *如果沒有傳入需要播放的歌曲,或者是傳入的歌曲名不在音樂庫中,那么就直接返回
      如果需要播放的歌曲就是當(dāng)前正在播放的歌曲,那么直接返回
     */
    if (!playingMusic || ![[self musics]containsObject:playingMusic]) return;
    if (_playingMusic == playingMusic) return;
    _playingMusic=playingMusic;
}
/**
 *  返回正在播放的歌曲
 */
+(YYMusicModel *)playingMusic
{
    return _playingMusic;
}

/**
 *  下一首歌曲
 */
+(YYMusicModel *)nextMusic
{
    //設(shè)定一個(gè)初值
    int nextIndex = 0;
    if (_playingMusic) {
        //獲取當(dāng)前播放音樂的索引
        int playingIndex = [[self musics] indexOfObject:_playingMusic];
        //設(shè)置下一首音樂的索引
        nextIndex = playingIndex+1;
        //檢查數(shù)組越界,如果下一首音樂是最后一首,那么重置為0
        if (nextIndex>=[self musics].count) {
            nextIndex=0;
        }
    }
    return [self musics][nextIndex];
}

/**
 *  上一首歌曲
 */
+(YYMusicModel *)previousMusic
{
    //設(shè)定一個(gè)初值
    int previousIndex = 0;
    if (_playingMusic) {
        //獲取當(dāng)前播放音樂的索引
        int playingIndex = [[self musics] indexOfObject:_playingMusic];
        //設(shè)置下一首音樂的索引
        previousIndex = playingIndex-1;
        //檢查數(shù)組越界,如果下一首音樂是最后一首,那么重置為0
        if (previousIndex<0) {
            previousIndex=[self musics].count-1;
        }
    }
    return [self musics][previousIndex];
}
@end


三、封裝一個(gè)音樂播放工具類

該工具類中的代碼設(shè)計(jì)如下:

YYAudioTool.h文件

復(fù)制代碼 代碼如下:

//
//  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文件
復(fù)制代碼 代碼如下:

//
//  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.取出對應(yīng)的播放器
    AVAudioPlayer *player=[self musicPlayers][filename];
   
    //2.如果播放器沒有創(chuàng)建,那么就進(jìn)行初始化
    if (!player) {
        //2.1音頻文件的URL
        NSURL *url=[[NSBundle mainBundle]URLForResource:filename withExtension:nil];
        if (!url) return NO;//如果url為空,那么直接返回
       
        //2.2創(chuàng)建播放器
        player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
       
        //2.3緩沖
        if (![player prepareToPlay]) return NO;//如果緩沖失敗,那么就直接返回
       
        //2.4存入字典
        [self musicPlayers][filename]=player;
    }
   
    //3.播放
    if (![player isPlaying]) {
        //如果當(dāng)前沒處于播放狀態(tài),那么就播放
        return [player play];
    }

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

+(void)pauseMusic:(NSString *)filename
{
    if (!filename) return;//如果沒有傳入文件名,那么就直接返回
   
    //1.取出對應(yīng)的播放器
    AVAudioPlayer *player=[self musicPlayers][filename];
   
    //2.暫停
    [player pause];//如果palyer為空,那相當(dāng)于[nil pause],因此這里可以不用做處理

}

+(void)stopMusic:(NSString *)filename
{
    if (!filename) return;//如果沒有傳入文件名,那么就直接返回
   
    //1.取出對應(yīng)的播放器
    AVAudioPlayer *player=[self musicPlayers][filename];
   
    //2.停止
    [player stop];
   
    //3.將播放器從字典中移除
    [[self musicPlayers] removeObjectForKey:filename];
}

//播放音效
+(void)playSound:(NSString *)filename
{
    if (!filename) return;
    //1.取出對應(yīng)的音效
    SystemSoundID soundID=[[self soundIDs][filename] unsignedIntegerValue];
   
    //2.播放音效
    //2.1如果音效ID不存在,那么就創(chuàng)建
    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.取出對應(yīng)的音效
    SystemSoundID soundID=[[self soundIDs][filename] unsignedIntegerValue];
   
    //2.銷毀
    if (soundID) {
        AudioServicesDisposeSystemSoundID(soundID);
       
        //2.1銷毀后,從字典中移除
        [[self soundIDs]removeObjectForKey:filename];
    }
}
@end


四、在音樂播放控制器中的代碼處理

YYPlayingViewController.m文件

復(fù)制代碼 代碼如下:

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


復(fù)制代碼 代碼如下:

@implementation YYPlayingViewController
#pragma mark-公共方法
-(void)show
{
    //1.禁用整個(gè)app的點(diǎn)擊事件
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    window.userInteractionEnabled=NO;
   
    //2.添加播放界面
    //設(shè)置View的大小為覆蓋整個(gè)窗口
    self.view.frame=window.bounds;
    //設(shè)置view顯示
    self.view.hidden=NO;
    //把View添加到窗口上
    [window addSubview:self.view];
   
    //3.檢測是否換了歌曲
    if (self.playingMusic!=[YYMusicTool playingMusic]) {
        [self RresetPlayingMusic];
    }
   
    //4.使用動(dòng)畫讓View顯示
    self.view.y=self.view.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.view.y=0;
    } completion:^(BOOL finished) {
       
        //設(shè)置音樂數(shù)據(jù)
        [self starPlayingMusic];
        window.userInteractionEnabled=YES;
    }];
}
#pragma mark-私有方法
//重置正在播放的音樂
-(void)RresetPlayingMusic
{
    //1.重置界面數(shù)據(jù)
    self.iconView.image=[UIImage imageNamed:@"play_cover_pic_bg"];
    self.songLabel.text=nil;
    self.singerLabel.text=nil;
   
    //2.停止播放
    [YYAudioTool stopMusic:self.playingMusic.filename];
}
//開始播放音樂數(shù)據(jù)
-(void)starPlayingMusic
{
    //1.設(shè)置界面數(shù)據(jù)
   
    //取出當(dāng)前正在播放的音樂
//    YYMusicModel *playingMusic=[YYMusicTool playingMusic];
   
    //如果當(dāng)前播放的音樂就是傳入的音樂,那么就直接返回
    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-內(nèi)部的按鈕監(jiān)聽方法
//返回按鈕
- (IBAction)exit {
    //1.禁用整個(gè)app的點(diǎn)擊事件
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    window.userInteractionEnabled=NO;
   
    //2.動(dòng)畫隱藏View
    [UIView animateWithDuration:0.25 animations:^{
        self.view.y=window.height;
    } completion:^(BOOL finished) {
        window.userInteractionEnabled=YES;
        //設(shè)置view隱藏能夠節(jié)省一些性能
        self.view.hidden=YES;
    }];
}
@end


注意:先讓用戶看到界面上的所有東西后,再開始播放歌曲。

提示:一般的播放器需要做一個(gè)重置的操作。
  當(dāng)從一首歌切換到另外一首時(shí),應(yīng)該先把上一首的信息刪除,因此在show動(dòng)畫顯示之前,應(yīng)該檢測是否換了歌曲,如果換了歌曲,則應(yīng)該做一次重置操作。
 實(shí)現(xiàn)效果(能夠順利的切換和播放歌曲,下面是界面顯示):

2015122992308216.png (318×499)2015122992326380.png (314×497)2015122992342522.png (314×497)

五、補(bǔ)充代碼

YYMusicsViewController.m文件

復(fù)制代碼 代碼如下:

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


復(fù)制代碼 代碼如下:

@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;
}
/**
 *  設(shè)置每個(gè)cell的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

/**
 *  cell的點(diǎn)擊事件
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.取消選中被點(diǎn)擊的這行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    //2.設(shè)置正在播放的歌曲
    [YYMusicTool setPlayingMusic:[YYMusicTool musics][indexPath.row]];

    //調(diào)用公共方法
    [self.playingViewController show];
   
//    //執(zhí)行segue跳轉(zhuǎn)
//    [self performSegueWithIdentifier:@"music2playing" sender:nil];
}
@end


六、一些細(xì)節(jié)控制
再來看一個(gè)實(shí)現(xiàn)的效果:

2015122992419921.png (315×492)

完整的代碼

YYPlayingViewController.m文件

復(fù)制代碼 代碼如下:

//
//  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 ()
//進(jìn)度條
@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;
//當(dāng)前播放的音樂的時(shí)長
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;
//正在播放的音樂
@property(nonatomic,strong)YYMusicModel *playingMusic;
//音樂播放器對象
@property(nonatomic,strong)AVAudioPlayer *player;
//定時(shí)器
@property(nonatomic,strong)NSTimer *CurrentTimeTimer;
- (IBAction)exit;
- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender;
- (IBAction)panSlider:(UIPanGestureRecognizer *)sender;

@end


復(fù)制代碼 代碼如下:

@implementation YYPlayingViewController
#pragma mark-公共方法
-(void)show
{
    //1.禁用整個(gè)app的點(diǎn)擊事件
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    window.userInteractionEnabled=NO;
   
    //2.添加播放界面
    //設(shè)置View的大小為覆蓋整個(gè)窗口
    self.view.frame=window.bounds;
    //設(shè)置view顯示
    self.view.hidden=NO;
    //把View添加到窗口上
    [window addSubview:self.view];
   
    //3.檢測是否換了歌曲
    if (self.playingMusic!=[YYMusicTool playingMusic]) {
        [self RresetPlayingMusic];
    }
   
    //4.使用動(dòng)畫讓View顯示
    self.view.y=self.view.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.view.y=0;
    } completion:^(BOOL finished) {
       
        //設(shè)置音樂數(shù)據(jù)
        [self starPlayingMusic];
        window.userInteractionEnabled=YES;
    }];
}


#pragma mark-私有方法
//重置正在播放的音樂
-(void)RresetPlayingMusic
{
    //1.重置界面數(shù)據(jù)
    self.iconView.image=[UIImage imageNamed:@"play_cover_pic_bg"];
    self.songLabel.text=nil;
    self.singerLabel.text=nil;
   
    //2.停止播放
    [YYAudioTool stopMusic:self.playingMusic.filename];
    //把播放器進(jìn)行清空
    self.player=nil;
   
    //3.停止定時(shí)器
    [self removeCurrentTime];
}
//開始播放音樂數(shù)據(jù)
-(void)starPlayingMusic
{
    //1.設(shè)置界面數(shù)據(jù)
   
    //如果當(dāng)前播放的音樂就是傳入的音樂,那么就直接返回
    if (self.playingMusic==[YYMusicTool playingMusic])
    {
        //把定時(shí)器加進(jìn)去
        [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.設(shè)置時(shí)長
    //self.player.duration;  播放器正在播放的音樂文件的時(shí)間長度
    self.durationLabel.text=[self strWithTime:self.player.duration];
   
    //4.添加定時(shí)器
    [self addCurrentTimeTimer];
   
}

/**
 *把時(shí)間長度-->時(shí)間字符串
 */
-(NSString *)strWithTime:(NSTimeInterval)time
{
    int minute=time / 60;
    int second=(int)time % 60;
    return [NSString stringWithFormat:@"%d:%d",minute,second];
}

#pragma mark-定時(shí)器控制
/**
 *  添加一個(gè)定時(shí)器
 */
-(void)addCurrentTimeTimer
{
    //提前先調(diào)用一次進(jìn)度更新,以保證定時(shí)器的工作時(shí)及時(shí)的
    [self updateCurrentTime];
   
    //創(chuàng)建一個(gè)定時(shí)器,每一秒鐘調(diào)用一次
    self.CurrentTimeTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];
    //把定時(shí)器加入到運(yùn)行時(shí)中
    [[NSRunLoop mainRunLoop]addTimer:self.CurrentTimeTimer forMode:NSRunLoopCommonModes];
}
/**
 *移除一個(gè)定時(shí)器
 */
-(void)removeCurrentTime
{
    [self.CurrentTimeTimer invalidate];
   
    //把定時(shí)器清空
    self.CurrentTimeTimer=nil;
}

/**
 *  更新播放進(jìn)度
 */
-(void)updateCurrentTime
{
    //1.計(jì)算進(jìn)度值
    double progress=self.player.currentTime/self.player.duration;
   
    //2.計(jì)算滑塊的x值
    // 滑塊的最大的x值
    CGFloat sliderMaxX=self.view.width-self.slider.width;
    self.slider.x=sliderMaxX*progress;
    //設(shè)置滑塊上的當(dāng)前播放時(shí)間
    [self.slider setTitle:[self strWithTime:self.player.currentTime] forState:UIControlStateNormal];
   
    //3.設(shè)置進(jìn)度條的寬度
    self.progressView.width=self.slider.center.x;
   
}

#pragma mark-內(nèi)部的按鈕監(jiān)聽方法
//返回按鈕
- (IBAction)exit {
   
    //0.移除定時(shí)器
    [self  removeCurrentTime];
    //1.禁用整個(gè)app的點(diǎn)擊事件
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    window.userInteractionEnabled=NO;
   
    //2.動(dòng)畫隱藏View
    [UIView animateWithDuration:0.25 animations:^{
        self.view.y=window.height;
    } completion:^(BOOL finished) {
        window.userInteractionEnabled=YES;
        //設(shè)置view隱藏能夠節(jié)省一些性能
        self.view.hidden=YES;
    }];
}

/**
 *點(diǎn)擊了進(jìn)度條
 */
- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender {
    //獲取當(dāng)前單擊的點(diǎn)
    CGPoint point=[sender locationInView:sender.view];
    //切換歌曲的當(dāng)前播放時(shí)間
    self.player.currentTime=(point.x/sender.view.width)*self.player.duration;
    //更新播放進(jìn)度
    [self updateCurrentTime];
}

- (IBAction)panSlider:(UIPanGestureRecognizer *)sender {
   
    //1.獲得挪動(dòng)的距離
    CGPoint t=[sender translationInView:sender.view];
    //把挪動(dòng)清零
    [sender setTranslation:CGPointZero inView:sender.view];
   
    //2.控制滑塊和進(jìn)度條的frame
    self.slider.x+=t.x;
    //設(shè)置進(jìn)度條的寬度
    self.progressView.width=self.slider.center.x;
   
    //3.設(shè)置時(shí)間值
    CGFloat sliderMaxX=self.view.width-self.slider.width;
    double progress=self.slider.x/sliderMaxX;
    //當(dāng)前的時(shí)間值=音樂的時(shí)長*當(dāng)前的進(jìn)度值
    NSTimeInterval time=self.player.duration*progress;
    [self .slider setTitle:[self strWithTime:time] forState:UIControlStateNormal];
   
    //4.如果開始拖動(dòng),那么就停止定時(shí)器
    if (sender.state==UIGestureRecognizerStateBegan) {
        //停止定時(shí)器
        [self removeCurrentTime];
    }else if(sender.state==UIGestureRecognizerStateEnded)
    {
        //設(shè)置播放器播放的時(shí)間
        self.player.currentTime=time;
        //開啟定時(shí)器
        [self addCurrentTimeTimer];
    }
}
@end


代碼說明(一)

  調(diào)整開始播放音樂按鈕,讓其返回一個(gè)音樂播放器,而非BOOL型的。

復(fù)制代碼 代碼如下:

/**
 *播放音樂
 */
+(AVAudioPlayer *)playMusic:(NSString *)filename
{
    if (!filename) return nil;//如果沒有傳入文件名,那么直接返回
    //1.取出對應(yīng)的播放器
    AVAudioPlayer *player=[self musicPlayers][filename];
   
    //2.如果播放器沒有創(chuàng)建,那么就進(jìn)行初始化
    if (!player) {
        //2.1音頻文件的URL
        NSURL *url=[[NSBundle mainBundle]URLForResource:filename withExtension:nil];
        if (!url) return nil;//如果url為空,那么直接返回
       
        //2.2創(chuàng)建播放器
        player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
       
        //2.3緩沖
        if (![player prepareToPlay]) return nil;//如果緩沖失敗,那么就直接返回
       
        //2.4存入字典
        [self musicPlayers][filename]=player;
    }
   
    //3.播放
    if (![player isPlaying]) {
        //如果當(dāng)前沒處于播放狀態(tài),那么就播放
         [player play];
    }

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


代碼說明(二)

  把時(shí)間轉(zhuǎn)換為時(shí)間字符串的方法:

復(fù)制代碼 代碼如下:

/**
 *把時(shí)間長度-->時(shí)間字符串
 */
-(NSString *)strWithTime:(NSTimeInterval)time
{
    int minute=time / 60;
    int second=(int)time % 60;
    return [NSString stringWithFormat:@"%d:%d",minute,second];
}

代碼說明(三)

  說明:進(jìn)度控制

  監(jiān)聽當(dāng)前的播放,使用一個(gè)定時(shí)器,不斷的監(jiān)聽當(dāng)前是第幾秒。

  關(guān)于定時(shí)器的處理:這里使用了三個(gè)方法,分別是添加定時(shí)器,移除定時(shí)器,和更新播放進(jìn)度。

注意細(xì)節(jié):

(1)移除定時(shí)器后,對定時(shí)器進(jìn)行清空處理。

復(fù)制代碼 代碼如下:

/**
 *移除一個(gè)定時(shí)器
 */
-(void)removeCurrentTime
{
    [self.CurrentTimeTimer invalidate];
   
    //把定時(shí)器清空
    self.CurrentTimeTimer=nil;
}

(2)當(dāng)看不到界面的時(shí)候,停止定時(shí)器。

(3)在開始播放音樂的方法中進(jìn)行判斷,如果當(dāng)前播放的音樂和傳入的音樂一致,那么添加定時(shí)器后直接返回。

(4)重置播放的音樂方法中,停止定時(shí)器。

代碼說明(四)

  說明:點(diǎn)擊和拖動(dòng)進(jìn)度條的處理

(1)點(diǎn)擊進(jìn)度條

  先添加單擊的手勢識(shí)別器。

2015122992457222.png (337×423)

往控制器拖線:

2015122992516751.png (257×117)

涉及的代碼:

復(fù)制代碼 代碼如下:

/**
 *點(diǎn)擊了進(jìn)度條
 */
- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender {
    //獲取當(dāng)前單擊的點(diǎn)
    CGPoint point=[sender locationInView:sender.view];
    //切換歌曲的當(dāng)前播放時(shí)間
    self.player.currentTime=(point.x/sender.view.width)*self.player.duration;
    //更新播放進(jìn)度
    [self updateCurrentTime];
}

(2)拖拽進(jìn)度條

  先添加拖拽手勢識(shí)別器

2015122992536070.png (312×662)

往控制器拖線

2015122992557756.png (270×122)

涉及的代碼:

復(fù)制代碼 代碼如下:

/**
 *拖動(dòng)滑塊
 */
- (IBAction)panSlider:(UIPanGestureRecognizer *)sender {
   
    //1.獲得挪動(dòng)的距離
    CGPoint t=[sender translationInView:sender.view];
    //把挪動(dòng)清零
    [sender setTranslation:CGPointZero inView:sender.view];
   
    //2.控制滑塊和進(jìn)度條的frame
    self.slider.x+=t.x;
    //設(shè)置進(jìn)度條的寬度
    self.progressView.width=self.slider.center.x;
   
    //3.設(shè)置時(shí)間值
    CGFloat sliderMaxX=self.view.width-self.slider.width;
    double progress=self.slider.x/sliderMaxX;
    //當(dāng)前的時(shí)間值=音樂的時(shí)長*當(dāng)前的進(jìn)度值
    NSTimeInterval time=self.player.duration*progress;
    [self .slider setTitle:[self strWithTime:time] forState:UIControlStateNormal];
   
    //4.如果開始拖動(dòng),那么就停止定時(shí)器
    if (sender.state==UIGestureRecognizerStateBegan) {
        //停止定時(shí)器
        [self removeCurrentTime];
    }else if(sender.state==UIGestureRecognizerStateEnded)
    {
        //設(shè)置播放器播放的時(shí)間
        self.player.currentTime=time;
        //開啟定時(shí)器
        [self addCurrentTimeTimer];
    }
}

相關(guān)文章

  • iOS圖片拉伸技巧(iOS5.0、iOS6.0)

    iOS圖片拉伸技巧(iOS5.0、iOS6.0)

    這篇文章主要為大家詳細(xì)介紹了iOS圖片拉伸技巧,提供了3種圖片拉伸的解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時(shí)傳輸

    iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時(shí)傳輸

    這篇文章主要為大家詳細(xì)介紹了iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時(shí)傳輸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • iOS實(shí)現(xiàn)圖片水印與簡單封裝示例代碼

    iOS實(shí)現(xiàn)圖片水印與簡單封裝示例代碼

    這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)圖片水印與簡單封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • 輕松理解iOS 11中webview的視口

    輕松理解iOS 11中webview的視口

    這篇文章主要介紹了iOS 11中webview的視口知識(shí),需要的朋友可以參考下
    2017-09-09
  • iOS13 適配和Xcode11.0踩坑小結(jié)

    iOS13 適配和Xcode11.0踩坑小結(jié)

    這篇文章主要介紹了iOS13 適配和Xcode11.0踩坑小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • iOS中自定義彈出pickerView效果(DEMO)

    iOS中自定義彈出pickerView效果(DEMO)

    這篇文章主要介紹了iOS中自定義簡單彈出pickerView(DEMO)的實(shí)例代碼,需要的朋友可以參考下
    2017-01-01
  • ios 獲取或修改網(wǎng)頁上的內(nèi)容

    ios 獲取或修改網(wǎng)頁上的內(nèi)容

    UIWebView是iOS最常用的SDK之一,它有一個(gè)stringByEvaluatingJavaScriptFromString方法可以將javascript嵌入頁面中,通過這個(gè)方法我們可以在iOS中與UIWebView中的網(wǎng)頁元素交互
    2016-12-12
  • IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

    IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

    這篇文章主要介紹了IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10
  • IOS自適配利器Masonry使用指南

    IOS自適配利器Masonry使用指南

    如果說自動(dòng)布局解救了多屏幕適配,那眾多三方庫的出現(xiàn)就解救了系統(tǒng)自動(dòng)布局的寫法。Masonry就是其中一個(gè)。用法上也比較簡單靈活,很大程度上替代了傳統(tǒng)的NSLayoutConstraint布局方式。下面我們就來具體探討下吧
    2016-01-01
  • iOS應(yīng)用中UISearchDisplayController搜索效果的用法

    iOS應(yīng)用中UISearchDisplayController搜索效果的用法

    這篇文章主要介紹了iOS應(yīng)用中UISearchDisplayController搜索效果的用法,包括點(diǎn)擊搜索出現(xiàn)黑條問題的解決方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-02-02

最新評論