今天记录一下AVAudioPlayer,这个播放器类苹果提供了一些代理方法,主要用来播放本地音频。

其实也可以用来播放网络音频,只不过是将整个网络文件下载下来而已,在实际开发中会比较耗费流量不做推荐。

下面是相关代码:

#import "ViewController.h"

//引入框架

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

<AVAudioPlayerDelegate>

//设置全局对象,否则将会出现没有声音出现的情况

@property (nonatomic,strong) AVAudioPlayer *audioPlayer;

@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,strong) NSArray *mp3Array;

@property (nonatomic) NSInteger currentMp3;

@property (nonatomic,strong) NSArray *lrcArray;

@property (nonatomic,strong) CYParserLrc *parser;

@property (nonatomic)NSInteger currentRow;

@end

@implementation ViewController

-(void)initWithMp3Path:(NSString *)mp3Path lrcPath:(NSString *)lrcPath

{

//initWithContentsOfURL 路径(本地)

self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mp3Path] error:nil];

//准备播放

[self.audioPlayer prepareToPlay];

//设置声音

self.audioPlayer.volume = 0.5f;

  //设置代理

self.audioPlayer.delegate = self;

//设置进度条最大值

//duration  当前音乐的总时间

self.song.maximumValue = self.audioPlayer.duration;

//解析歌词

[self.parser parserLrc:lrcPath];

//下一曲 刷新tableView

[self.tableView reloadData];

self.timer = [NSTimer scheduledTimerWithTimeInterval:.1f target:self selector:@selector(timeChange) userInfo:nil repeats:YES];

}

#pragma 代理方法

/* audioPlayerDidFinishPlaying:successfully: is called when a sound has finished playing. This method is NOT called if the player is stopped due to an interruption. */

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;

/* if an error occurs while decoding it will be reported to the delegate. */

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error;

#if TARGET_OS_IPHONE

/* AVAudioPlayer INTERRUPTION NOTIFICATIONS ARE DEPRECATED - Use AVAudioSession instead. */

/* audioPlayerBeginInterruption: is called when the audio session has been interrupted while the player was playing. The player will have been paused. */

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player NS_DEPRECATED_IOS(2_2, 8_0);

/* audioPlayerEndInterruption:withOptions: is called when the audio session interruption has ended and this player had been interrupted while playing. */

/* Currently the only flag is AVAudioSessionInterruptionFlags_ShouldResume. */

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags NS_DEPRECATED_IOS(6_0, 8_0);

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags NS_DEPRECATED_IOS(4_0, 6_0);

/* audioPlayerEndInterruption: is called when the preferred method, audioPlayerEndInterruption:withFlags:, is not implemented. */

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player NS_DEPRECATED_IOS(2_2, 6_0);

iOS播放器 - AVAudioPlayer的更多相关文章

  1. 谈一谈做iOS播放器库开发所涉及的知识点

    在自己研究生毕业的时候,想着能找上一份做视频编解码的工作,可惜没有如愿,最后到了一家iOS游戏渠道公司去做游戏支付业务的SDK开发,我的iOS正式开发生涯就这么开始了. 在那家iOS游戏渠道没做上一年 ...

  2. iOS播放器 - AVPlayer

    之前有说到在播放器中一点点小技巧,现在正式记录一下AVPlayer. 这里主要是说明用AVPlayer做音乐播放器的功能实现,所以不介绍AVPlayer中那个图层类. 首先我们要声明一下播放器,这里有 ...

  3. 音乐播放器 AVAudioPlayer、定时器、UISlider

    #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ...

  4. IOS播放音频 AVAudioPlayer(实例)

    1. AVFoundation Build Phases => Link Binary With Libraies => + => AVFoundation.framework =& ...

  5. OCiOS开发:音频播放器 AVAudioPlayer

    简单介绍 AVAudioPlayer音频播放器可以提供简单的音频播放功能.其头文件包括在AVFoudation.framework中. AVAudioPlayer未提供可视化界面,须要通过其提供的播放 ...

  6. iOS播放器、Flutter高仿书旗小说、卡片动画、二维码扫码、菜单弹窗效果等源码

    iOS精选源码 全网最详细购物车强势来袭 一款优雅易用的微型菜单弹窗(类似QQ和微信右上角弹窗) swift, UITableView的动态拖动重排CCPCellDragger 高仿书旗小说 Flut ...

  7. iOS播放器横竖屏切换

    http://www.cocoachina.com/cms/wap.php?action=article&id=20292 http://feihu.me/blog/2015/how-to-h ...

  8. IOS AVAUDIOPLAYER 播放器使用

    1. 导入 AVFoundation.framework 2.导入头文件  #import <AVFoundation/AVFoundation.h> 3. player = [[AVAu ...

  9. iOS开发拓展篇—音频处理(音乐播放器3)

    iOS开发拓展篇—音频处理(音乐播放器3) 说明:这篇文章主要介绍音频工具类和播放工具类的封装. 一.控制器间数据传递 1.两个控制器之间数据的传递 第一种方法:self.parentViewCont ...

随机推荐

  1. 基于redis分布式缓存实现(新浪微博案例)

    第一:Redis 是什么? Redis是基于内存.可持久化的日志型.Key-Value数据库 高性能存储系统,并提供多种语言的API. 第二:出现背景 数据结构(Data Structure)需求越来 ...

  2. Perl 脚本报Can't locate Mail/Sender.pm 解决办法

        在新的Linux Server(Red Hat Enterprise Linux Server release 5.7 (Tikanga))上配置磁盘空间告警的perl脚本后,测试时报如下错误 ...

  3. YARN基本框架介绍

    YARN基本框架介绍 转载请注明出处:http://www.cnblogs.com/BYRans/ 在之前的博客<YARN与MRv1的对比>中介绍了YARN对Hadoop 1.0的完善.本 ...

  4. YARN与MRv1的对比

    YARN与MRv1的对比 转载请注明出处:http://www.cnblogs.com/BYRans/ Hadoop 1.0存在的问题 由于Hadoop 1.0的良好特性,Hadoop 1.0被应用到 ...

  5. Java中的集合排序

    1. 定义排序 class ComparatorDefault implements Comparator { public int compare(Object arg0, Object arg1) ...

  6. ASP.NET Core AD 域登录

    在选择AD登录时,其实可以直接选择 Windows 授权,不过因为有些网站需要的是LDAP获取信息进行授权,而非直接依赖Web Server自带的Windows 授权功能. 当然如果使用的是Azure ...

  7. hadoop.home.dir is not set

    今天遇到一个错误,JAVA堆栈如下: 是在获取文件系统对象的时候出错,没有找到环境变量 HADOOP_HOME or hadoop.home.dir ,但是在本地一直没有重现这个问题. 这个问题是一个 ...

  8. Web报表工具FineReport中JavaScript的使用

    报表软件FineReport采用的是jQuery v1.9.2框架,jQuery是一个快速的,简洁的JavaScript库,能让用户更方便地处理HTML documents.events,实现动画效果 ...

  9. Label控件如何根据字符串自定义大小

    一.. this.label_Msg.AutoSize = false;  //设置label空件不能自动大小 二.. 用代码控制label控件的大小 1.根据字符串.label的宽度 计算字符串的面 ...

  10. 【CSS】过渡、动画和变换

    1. 使用过渡 过渡效果一般是由浏览器直接改变元素的CSS属性实现的.例如,如果使用:hover选择器,一旦用户将鼠标悬停在元素之上,浏览器就会应用跟选择器关联的属性. <!DOCTYPE ht ...