1.音频播放
iOS开发中的音频播放大致分为两类:一种是短音频(通常称为音效,时长不超过30s),一种是长音频(通常称为音乐)。前者通常可以通过AudioToolbox.framework进行操作(由一系列C语言的函数构成),后者要使用AVFoundation.framework(用Objective-C封装好的一系列的类)。 播放音效(短音频)通常打包成.caf、.aif和.wav格式。下面为大家封装了一段播放音频的代码,由于音频播放的AudioToolBox底层都是C函数,所以使用了函数指针,不熟悉的可以翻翻之前C的东西
- (void)playSoundEffect:(NSString *)name withCallback:(void (*)(SystemSoundID, void *)) callback {
NSString *audioFile = [[NSBundle mainBundle] pathForResource:name ofType:nil];
NSURL *fileUrl = [NSURL fileURLWithPath:audioFile];
SystemSoundID soundID;
// 在系统中创建一个音效对象并获得其唯一ID
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
// 注册在播放完之后执行的回调函数
// 第二个和第三个参数跟循环播放有关
// 第五个参数是指向传给回调函数的数据的指针
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, callback, NULL);
// 播放音效
AudioServicesPlaySystemSound(soundID);
// 播放音效并震动
// AudioServicesPlayAlertSound(soundID);
}
播放音乐可以使用AVAudioPlayer类:
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"后会无期" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.player.volume = 5;
self.player.delegate = self;
[self.player prepareToPlay];
[self.player play];
Demo代码如下:




2.录音
使用AVAudioRecorder可以实现录音功能;代码如下:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityLow], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt:2],AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],AVSampleRateKey,
nil
];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/record.caf", [[NSBundle mainBundle] resourcePath]]];
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:dict error:nil];
[self.recorder prepareToRecord];
Demo代码如下:



3.视频播放

Demo代码如下:

效果如下:

4.相机和相册
- 开启相机或相册的代码如下所示:
// 创建一个图像选择视图控制器
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
// 检查相机是否可用
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// 相机可用就使用相机
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else {
// 相机不可用就使用相册
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
// 允许编辑照片
ipc.allowsEditing = YES;
// 绑定委托
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
- UIImagePickerControllerDelegate的两个回调方法:
// 选中照片执行的回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// info参数是一个字典,可以取出原始照片或编辑后的照片
}
// 取消选择照片的回调方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
}
说明:在开启相机时可以通过设置UIImagePickerController对象的mediaTypes属性来支持摄像操作,摄像完成后需要调用UISaveVideoAtPathToSavedPhotosAlbum函数将保存在临时文件夹下的视频文件保存到相册中,当然在此之前最好用UIVideoAtPathIsCompatibleWithSavedPhotosAlbum函数判断能否保存,在此之后可以通过NSFileManager对象的removeItemAtPath:error:方法将临时文件删除
Demo代码如下:




注意在plist文件中添加相应属性(否则会奔溃):
NSMicrophoneUsageDescription
NSCameraUsageDescription
NSPhotoLibraryUsageDescription

效果:








补充拓展:

参考内容:控制器了解篇章中的 UIImagePickController部分:http://www.jianshu.com/p/6ce0a8cdc02a
网友评论