实例解析iOS app开发中音频文件播放工具类的封装

2020-01-14 19:45:20王振洲

    
    //2.停止
    [player stop];
    
    //3.将播放器从字典中移除
    [[self musices] removeObjectForKey:filename];
}
@end


测试程序:

 

在storyboard中拖拽控件,并进行连线,以做控制。

实例解析iOS app开发中音频文件播放工具类的封装

导入可供播放的音乐素材。

测试程序的代码设计如下:

复制代码
//
//  YYViewController.m
//  17-多个音乐文件的播放
//
//  Created by apple on 14-8-9.
//  Copyright (c) 2014年 yangyong. All rights reserved.
//

 

#import "YYViewController.h"
#import "YYAudioTool.h"

@interface YYViewController ()
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
- (IBAction)next;

//用一个数组来保存所有的音乐文件
@property(nonatomic,strong)NSArray *songs;
//用一个int型的属性来记录当前的索引
@property(nonatomic,assign)int currentIndex;
@end


复制代码
@implementation YYViewController
#pragma mark-懒加载
-(NSArray *)songs
{
    if (_songs==nil) {
        self.songs=@[@"235319.mp3",@"309769.mp3",@"120125029.mp3"];
    }
    return _songs;
}

 

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

- (IBAction)play {
    //开始播放/继续播放
    [YYAudioTool playMusic:self.songs[self.currentIndex]];
}

- (IBAction)pause {
    //暂停播放
    [YYAudioTool pauseMusic:self.songs[self.currentIndex]];