首页 文章 精选 留言 我的

精选列表

搜索[ios],共10000篇文章
优秀的个人博客,低调大师

iOS开发-音乐播放(AVAudioPlayer)

现在的手机的基本上没有人不停音乐的,我们无法想象在一个没有声音的世界里我们会过的怎么样,国内现在的主流的主流网易云音乐,QQ音乐,酷狗,虾米,天天基本上霸占了所有的用户群体,不过并没有妨碍大家对音乐的追求,乐流算是突围成功了,据说卖给QQ啦,有兴趣的可以看下。我们做不了那么高大上的就先做个简单的,最核心的就是播放,暂停,切歌,其他的基本上围绕这个修修补补锦上添花的,比如说歌曲名称,专辑,谁听了这首歌。。。铺垫的多了,直接看效果吧,三个按钮一个进度条: 初始化按钮: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 self .playButton=[[UIButton alloc]initWithFrame:CGRectMake(40, 100, 60, 30)]; [ self .playButton setTitle:@ "播放" forState:UIControlStateNormal]; [ self .playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [ self .playButton.titleLabel setFont:[UIFont systemFontOfSize:14]]; self .playButton.layer.borderColor=[UIColor blackColor].CGColor; self .playButton.layer.borderWidth=1.0; self .playButton.layer.cornerRadius=5.0; [ self .playButton addTarget: self action: @selector (playMusic:) forControlEvents:UIControlEventTouchUpInside]; [ self .view addSubview: self .playButton]; self .pauseButton=[[UIButton alloc]initWithFrame:CGRectMake(140, 100, 60, 30)]; [ self .pauseButton setTitle:@ "暂停" forState:UIControlStateNormal]; [ self .pauseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [ self .pauseButton.titleLabel setFont:[UIFont systemFontOfSize:14]]; self .pauseButton.layer.borderColor=[UIColor blackColor].CGColor; self .pauseButton.layer.borderWidth=1.0; self .pauseButton.layer.cornerRadius=5.0; [ self .pauseButton addTarget: self action: @selector (pauseMusic:) forControlEvents:UIControlEventTouchUpInside]; [ self .view addSubview: self .pauseButton]; self .switchButton=[[UIButton alloc]initWithFrame:CGRectMake(240, 100, 60, 30)]; [ self .switchButton setTitle:@ "切歌" forState:UIControlStateNormal]; [ self .switchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [ self .switchButton.titleLabel setFont:[UIFont systemFontOfSize:14]]; self .switchButton.layer.borderColor=[UIColor blackColor].CGColor; self .switchButton.layer.borderWidth=1.0; self .switchButton.layer.cornerRadius=5.0; [ self .switchButton addTarget: self action: @selector (switchMusic:) forControlEvents:UIControlEventTouchUpInside]; [ self .view addSubview: self .switchButton]; 初始化进度条: 1 2 self .progressView=[[UIProgressView alloc]initWithFrame:CGRectMake(40, 180, 200, 50)]; [ self .view addSubview: self .progressView]; AVAudioPlayer可以看成一个简易播放器,支持多种音频格式,能够进行进度、音量、播放速度等控制,已经满足了基本需求,接下来是播放音乐的代码: 1 2 3 4 5 6 if ( self .audioPlayer.isPlaying) { [ self .audioPlayer pause]; } else { [ self loadMusicByAsset:[[AVURLAsset alloc] initWithURL:[[ NSBundle mainBundle] URLForResource:@ "我最亲爱的" withExtension:@ "mp3" ] options: nil ]]; [ self .audioPlayer play]; } 实例化AVAudioPlayer: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -( void )loadMusicByAsset:(AVURLAsset*)avUrlAsset { if ([[ NSFileManager defaultManager] fileExistsAtPath:avUrlAsset.URL.path]){ NSError *error= nil ; self .audioPlayer= [[AVAudioPlayer alloc] initWithContentsOfURL:avUrlAsset.URL error:&error]; self .audioPlayer.delegate= self ; //准备buffer,减少播放延时的时间 [ self .audioPlayer prepareToPlay]; [ self .audioPlayer setVolume:1]; //设置音量大小 self .audioPlayer.numberOfLoops =0; //设置播放次数,0为播放一次,负数为循环播放 if (error) { NSLog (@ "初始化错误:%@" ,error.localizedDescription); } } } 上面是通过AVURLAsset实例化的,还可以直接通过名称实例化: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 -( void )loadMusic:( NSString *)name { NSString *musicFilePath = [[ NSBundle mainBundle] pathForResource:name ofType:@ "mp3" ]; //创建音乐文件路径 if ([[ NSFileManager defaultManager] fileExistsAtPath:musicFilePath]){ NSURL *musicURL = [[ NSURL alloc] initFileURLWithPath:musicFilePath]; NSError *error= nil ; self .audioPlayer= [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:&error]; self .audioPlayer.delegate= self ; //准备buffer,减少播放延时的时间 [ self .audioPlayer prepareToPlay]; [ self .audioPlayer setVolume:1]; //设置音量大小 self .audioPlayer.numberOfLoops =0; //设置播放次数,0为播放一次,负数为循环播放 if (error) { NSLog (@ "初始化错误:%@" ,error.localizedDescription); } } } 暂停音乐,这里为了方便单独写了一个按钮,大多数情况下,播放是暂停都是同一个按钮,仅供参考: 1 2 3 4 5 -( void )pauseMusic:(UIButton *)sender{ if ( self .audioPlayer.isPlaying) { [ self .audioPlayer pause]; } } 切歌就是通常大家点的上一曲下一曲,很好理解: 1 2 3 4 5 -( void )switchMusic:(UIButton *)sender{ [ self .audioPlayer stop]; [ self loadMusicByAsset:[[AVURLAsset alloc] initWithURL:[[ NSBundle mainBundle] URLForResource:@ "我需要一美元" withExtension:@ "mp3" ] options: nil ]]; [ self .audioPlayer play]; } 通过timer实时更新进度条: 1 2 3 self .timer = [ NSTimer scheduledTimerWithTimeInterval:0.1 target: self selector: @selector (changeProgress) userInfo: nil repeats: YES ]; 进度更新: 1 2 3 4 5 -( void )changeProgress{ if ( self .audioPlayer.isPlaying) { self .progressView.progress = self .audioPlayer.currentTime/ self .audioPlayer.duration; } } 效果如下: 音乐播放完成之后可以在AVAudioPlayerDelegate的代理方法里面根据业务场景执行自己安排: 1 2 3 4 5 6 7 -( void )audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:( BOOL )flag{ // [self.timer invalidate];直接销毁,之后不可用,慎重考虑 // [self.timer setFireDate:[NSDate date]]; //继续 // [self.timer setFireDate:[NSDate distantPast]];//开启 [ self .timer setFireDate:[ NSDate distantFuture]]; //暂停 } 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4540286.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS OC开发代码规范

1.变量、类名、函数名 使用驼峰命名法 2.尽量使用完整的单词命名,尽量不采用 缩写单词 3.类名使用大写字母打头,前缀统一加上HH 例如:HHHomePageController 4.类的成员变量使用 下划线打头, 例如_age 5.禁止在项目中的任何地方,包括文件名、目录名、逻辑目录名、项目名等地方使用空格或中文字符 6.为了不影响阅读,一个方法中代码不要超过100行。一个类中代码不要超过600行, 有超过的再回头重构的时候想办法分解 7.每个.h文件最开始处用简短文字说明一下该类的功能 8.类中第___个功能模块以 #pragma mark - 分隔,上空两行,下空一行 9.常用对象 命名采用后缀,如下 UIView: View 例如:UICandidateView : viewCandidate; UILabel: Lab 例如: UICopyLabel: labCopy; UIButton: Btn 例如: UISelectedButton: btnSelected; UIImage: img imgAvatar; UIImageView: imgViewAbc UITextField: textFiledName; UITextView: textViewEdit; NSArray: arrName; NSMutableArray: mArrName; NSDictionary: dictPosition; NSMutableDictionary :MDictPosition; NSString :strName; NSMutableString :mStrName; NSAttributedString :attributedStrName; NSMutableAttributedString :mAttributedStrName; 10. 文档注释格式如下 /** 方法或变量名说明 @param 参数1说明 @param 参数2说明 ... @return 若方法有返回值,则对返回值进行说明 */ 12. 注意 常量和宏的声明 CGFloat cellHeight = 12.0f; static CGFloat const cellHeight = 12.0f; static NSString *const JumpNotificationName = @“JumpNotificationName”; 13.关于.h空行规定 13.1.文件说明与头文件包涵(#import)之间空1行 13.2.头文件包涵(#import)之间如果需要分类区别,各类别之间空1行 13.3.头文件包涵(#import)与@class之间空2行 13.4.@interface与@class之间空1行 13.5.头文件{}里面,空1行开始声明成员对象,如果需要分类别,各类别之间空1行 13.6.{}外空一行开始书写属性,如果需要分类,各类别之间空1行 13.7.空1行书写方法,如果各方法之间需要分类别,各类别之间空1行 13.8.方法完成之后,空1行@end 13.9.如果需要声明protocal,空2行接着写。通常protocal写在@end后,但是声明在@interface前,也可以另写1个文件 14.关于.m中空行的规定 14.1.文件说明与头文件包涵(#import)之间空1行 14.2.头文件包涵(#import)之间如果需要分类区别,各类别之间空1行 14.3.@implementation和@synthesize之间空1行,@synthesize不要使用逗号(,)如果需要分类区别,各类别之间空1行 14.4.@synthesize与 法之间空2 14.5.各方法之间空2行 15.方法里面的空行 15.1.方法名后空1行开始写 15.2.变量声明后需要空1行,如果需要分类区别区分,各类别之间空1行 15.3.条件,循环,选择语句,整个语句结束,需要空1行 15.4.各功能块之间空1行 15.5.最后1个反括号之前不空 15.6.注释与代码之间不空 15.7.#pragma mark与方法之间空1行 16关于空格 16.1.h中协议<>前 有1个空格 16.2.h中成员声明时,类型与变量之间有少1个空格。星号(*)靠近变量,不靠近类型 16.3.@property后有1各空格,()里面,逗号后面有1个空格,括号外面,先留1个空格,再声明属性 16.4.方法+,-后,与()之间有1个空格 16.5.返回类型与*之间有1个空格, 方法参数中返回类型与*之间有1个空格 16.6.多参数的方法,每个参数后都有1个空格 17.关于bool值 17.1.不要 if(obj==nil){}, if(!obj){} 17.2.比较时把常量放前 可以避免错误 17.3.不要 if(aBool==YES){},直接 if(aBool){} 或if(!!aBool){} 18.变量属性名和其它 18.1.尽量在使用的时候才声明变量,尽量少声明全局变量 18.2.变量名不要只使1个字 ,尽量能表明变量的意思 18.3.@synthesize和@dynamic,应该放在类实现的最上面,每一个声明都单独一行 18.4.协议protocol需要加#pragma marks-来区分 18.5.obj = nil; delegate也应该在dealloc里面写 delegate = nil; 19其它 19.1@interface与@implementation与import之间空1行 19.2属性的声明和实现,尽量避免书写@synthesize,如果用到@synthesize,要紧接着@implementation书写,不要换行 19.3成员变量尽量写在@implementation内部,有必要对外暴露时,才写在@interface下 19.4临时变量的声明,尽量赋初始值, eg: NSInteger a = 0; 19.5声明多个临时变量,必须另起一行,禁止书写 int a = 3, b = 5; 19.6临时变量的声明语法写完后,必须各行开始书写逻辑代码,逻辑代码中可以穿插 临时变量的声明,同样必须隔行书写 19.7遇到新的代码块,必须隔行书写。包括函数的实现,if, switch分支,while,do while,for循环等 19.8所有的代码块必须用{}, 即只有1行的if代码也必须用{}, if (success) { return YES; } 19.9所有的二次元运算符,必须以空格隔开,如 x += 12, 不能写成 x += 12; 19.10.所有的逗号后必须追加一个空格, 例如CGRectMake(12, 12, 43, 40); 19.11.尽量用NSInteger 取代int, 用NSUInteger 取代unsigned int

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册
修改密码
忘记密码