iOS中音频
来源:互联网 发布:知乎a tender feeling 编辑:程序博客网 时间:2024/06/06 02:14
我们把音频播放分成两部分:
一种播放时间短的
一种播放时间长的
短的常做于系统提示音 它需要导入的框架为AudioToolBox
使用:
步奏1、声明声音ID SystemSoundID系统播放声音文件时,就是靠声音ID来区分是哪一个音效 ID 是无符号整形
步奏2、创建播放声音文件的服务 这个服务里有两个参数,第一个指的是声音文件的路劲 第二个指的是声音的ID 这个步奏只是告诉系统,你要准备播放的资源,并没有开始播放
步奏3、播放声音
下面用一个代码的例子来表演给大家看
首先得导入这个框架
#import <AudioToolbox/AudioToolbox.h>
SystemSoundID soundtID =1; 声明ID
AudioServicesCreateSystemSoundID((__bridgeCFURLRef_Nonnull)([[NSBundlemainBundle]URLForResource:@"胜利.aiff"withExtension:nil]),&sountID); 创建系统声音的服务 这里我是以胜利的这个声音文件为例
AudioServicesPlaySystemSound(sountID); 播放
播放的时候有两个情况:
1.没有震动AudioServicesPlaySystemSound(sountID);
2.有震动AudioServicesPlayAlertSound(sountID);
长的就是像我们的音乐播放器里面的那些资源
它使用的框架为AVFoundation
需要这种播放的需要注意下几点:
1、必须声明全局变量的音乐播放对象或者是声明为属性
2、在退出音乐播放页面时,需要把播放对象给置空同时把delegate给置空
使用:
步奏1、资源文件路劲
步奏2、初始化播放器
步奏3、设置播放器
步奏4、预播放
步奏5、播放
其实真使用时比较简单
我这的视图控制器叫 MusicPlayViewController
#import <AVFoundation/AVFoundation.h>
@interfaceMusicPlayViewController ()<AVAudioPlayerDelegate>
{
AVAudioPlayer *audioPlayer;
}
@end
@implementationMusicPlayViewController
- (void)viewDidLoad {
[superviewDidLoad];
UIButton *playSound = [UIButtonbuttonWithType:UIButtonTypeCustom];
[playSound setTitle:@"播放"forState:UIControlStateNormal];
[playSound setTitle:@"暂停"forState:UIControlStateSelected];
playSound.tag= 10;
playSound.frame= CGRectMake(100,100,100,100);
[playSound addTarget:selfaction:@selector(playSound:)forControlEvents:UIControlEventTouchUpInside];
[playSound setBackgroundColor:[UIColorredColor]];
[self.viewaddSubview:playSound];
UIButton *playSound1 = [UIButtonbuttonWithType:UIButtonTypeCustom];
[playSound1 setTitle:@"停止"forState:UIControlStateNormal];
playSound1.frame= CGRectMake(50,210,100,100);
playSound1.tag= 11;
[playSound1 addTarget:selfaction:@selector(playSound:)forControlEvents:UIControlEventTouchUpInside];
[playSound1 setBackgroundColor:[UIColorredColor]];
}
[audioPlayerstop];//停止
UIButton *button = [self.viewviewWithTag:10];
[audioPlayerplay];//播放
}else{
[audioPlayerpause];//暂停
}
-(void)playMusicWithName:(NSString*)name Type:(UIButton*)sender{
//获得文件的路劲并声明一个播放器对象有错误会把错误消息给error
NSError *error;
audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:[[NSBundlemainBundle]URLForResource:namewithExtension:nil]error:&error];
//判断是否有错误
if (error) {
NSLog(@"%@",error);
return;
}
//开始预播放
[audioPlayerprepareToPlay];
//audioPlayer.numberOfChannels 获得音乐的声道
//设置速率必须设置enableRate为yes
audioPlayer.enableRate= YES;
audioPlayer.rate= 2;//0.5一半1.0正常2.0双倍
//设置峰值必须设置meteringEnabled为yes
audioPlayer.meteringEnabled=YES;
[audioPlayerupdateMeters];//更新峰值
//当前峰值[audioPlayer peakPowerForChannel:2];平均峰值[audioPlayer averagePowerForChannel:2];
//设置播放次数audioPlayer.numberOfLoops 0一次 1播放两次 负数是无限循环
audioPlayer.numberOfLoops= 0;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{
[superviewWillDisappear:animated];
//如果不置空下次返回来时会没有声音 *
audioPlayer = nil;
audioPlayer.delegate= nil;
今天就给大家分享这么多,希望对大家有帮助,明天介绍下录音。谢谢!
- iOS中音频
- iOS中截取视频中的音频
- iOS中截取视频中的音频
- ios中AVAudioPlayer音频播放器
- iOS 音频播放的3中方式
- 浅谈iOS中音频的开发
- iOS中截取视频中的音频
- iOS 音频
- iOS音频
- IOS - 音频
- ios 音频
- iOS音频
- iOS音频
- iOS-音频
- iOS中 MPMoviePlayer 实现视频音频播放 作者:韩俊强
- ios音频播放--归纳
- ios 录制音频
- ios 音频底层
- 支付介绍
- linux下启动tomcat
- 提升网站性能开发的10个技巧
- Andrew Ng机器学习笔记+Weka相关算法实现(一)梯度下降与回归方法
- 利用时间戳解决频繁更新图片造成的图片缓存问题
- iOS中音频
- hdu1561(树形dp)
- DAO,如何实现模糊查询
- Bulb Switcher---319
- Spring框架中CharacterEncodingFilter的作用真的没那么大
- 数据挖掘算法之K-Means算法演示
- Linux&Unix 分类下的文章
- hdu2639(01背包的第K大)
- bat修改文件内容