调用系统封装好的播放界面
//视频播放的url
NSURL *playerURL = [NSURL fileURLWithPath:self.datasource[indexPath.row]];
//初始化
self.playerView = [[AVPlayerViewController alloc]init];
//AVPlayerItem 视频的一些信息 创建AVPlayer使用的
AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:playerURL];
//通过AVPlayerItem创建AVPlayer
self.player = [[AVPlayer alloc]initWithPlayerItem:item];
//设置AVPlayerViewController内部的AVPlayer为刚创建的AVPlayer
self.playerView.player = self.player;
//关闭AVPlayerViewController内部的约束
self.playerView.view.translatesAutoresizingMaskIntoConstraints = YES;
[self presentViewController:self.playerView animated:YES completion:nil];
播放一段文本
将文本当做声音播放(默认播放英文)
AVSpeechSynthesizer *syn = [[AVSpeechSynthesizeralloc]init];
AVSpeechUtterance *utterance = [[AVSpeechUtterancealloc]initWithString:@“hello world"];
[syn speakUtterance:utterance];
AVAduioSession 应用程序与音频会话交互的接口
session = [AVAudioSession sharedInstance]//获取session单利
[session setCateGory:AVAudioSessionCategoryPlayback error:&error];// 设置会话模式
[session setActive:YES error:&error];//激活设置的模式
AVAudioPlayer 音频播放器属性
self.audioPlayer.numberOfLoops = -1;//循环
self.audioPlayer.volume = self.volumeValue;//音量
self.audioPlayer.pan = self.panValue;//左右声道
self.audioPlayer.enableRate = YES;
self.audioPlayer.rate = 1;//播放速率
音频会话的中断通知
//添加中断通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
权限判断
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
{
//无权限
}
#import <AssetsLibrary/AssetsLibrary.h>
ALAuthorizationStatus author = [ALAssetsLibraryauthorization Status];
if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
//无权限
}
typedef enum {
kCLAuthorizationStatusNotDetermined = 0, // 用户尚未做出选择这个应用程序的问候
kCLAuthorizationStatusRestricted, // 此应用程序没有被授权访问的照片数据。可能是家长控制权限
kCLAuthorizationStatusDenied, // 用户已经明确否认了这一照片数据的应用程序访问
kCLAuthorizationStatusAuthorized // 用户已经授权应用访问照片数据} CLAuthorizationStatus;
}
- 打开应用程序权限设置
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:UIApplicationOpenSettingsURLString]];
开关手电筒
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
if (open) {
[device setTorchMode:AVCaptureTorchModeOn];
} else {
[device setTorchMode:AVCaptureTorchModeOff];
}
[device unlockForConfiguration];
}