AVAudioPlayer播放本地音频
2016-10-12 14:56
489 查看
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () //记录systemSoundID @property (nonatomic, assign) SystemSoundID systemID; //记录播放器对象 @property (nonatomic, strong) AVAudioPlayer *audioPlayer; @end @implementation ViewController - (AVAudioPlayer *)audioPlayer { if (!_audioPlayer) { NSURL *audioFilePath = [[NSBundle mainBundle] URLForResource:@"AllOfMe.mp3" withExtension:nil]; _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFilePath error:nil]; } return _audioPlayer; } //播放音效(<=30s) - (IBAction)playShortAudio:(id)sender { //真机:如果播放系统提供声音(1000 ~ 2000)/震动(静音状态) AudioServicesPlaySystemSound(1600); AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //播放本地音效文件(创建systemID+播放) NSString *shortAudioPath = [[NSBundle mainBundle] pathForResource:@"audio.wav" ofType:nil]; NSURL *url = [NSURL fileURLWithPath:shortAudioPath]; AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &_systemID); //播放 AudioServicesPlaySystemSound(_systemID); } //播放本地音频文件 - (IBAction)playLocalAudioFile:(id)sender { if ([self.audioPlayer prepareToPlay]) { //可以将音频文件文件的数据读到内存(快) [self.audioPlayer play]; } } //暂停 - (IBAction)pauseAudioFile:(id)sender { if (self.audioPlayer.playing) { //正在播放 [self.audioPlayer pause]; } } //停止(下次播放,从头开始) - (IBAction)stopAudioFile:(id)sender { //设置当前播放的时间偏移量offset为0 [self.audioPlayer stop]; self.audioPlayer.currentTime = 0; } @end
相关文章推荐
- 本地音频播放AVAudioPlayer
- iOS Dev (20) 用 AVAudioPlayer 播放一个本地音频文件
- iOS Dev (20) 用 AVAudioPlayer 播放一个本地音频文件
- iOS 本地音频播放AVAudioPlayer
- IOS AVAudioPlay 播放本地音频没声音问题
- 使用AVAudioPlayer播放音频文件
- iOS学习,音频,本地选歌,播放
- iOS开发之音频播放AVAudioPlayer 类的介绍
- Android用SurfaceView播放本地视屏(音频)文件
- iPhone应用 AVAudioPlayer播放音频讲解
- iOS - AVAudioPlayer 音频播放
- iOS音频播放之AudioQueue(一):播放本地音乐
- iOS开发之音频播放AVAudioPlayer 类的介绍
- 音频播放(System Sound Services、MPMusicPlayerController、AVAudioplayer) for IOS
- Android实现本地音乐播放(一、实现音频的读取)
- Pyqt4,选择本地路径,播放一段音频文件
- MP3 音频录制,支持类似 IOS 原生的单边或者双边波形显示,低版本音频权限兼容,本地或者在线音频播放支持。
- iOS 音频播放 AVAudioPlayer
- iOS从零开始学习直播之音频1.播放本地音频文件
- IOS播放音频 AVAudioPlayer(实例)