【iOS】音频的简单使用(1播放音乐)
2016-08-24 00:00
621 查看
#import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVFoundation.h> @property (strong, nonatomic) AVAudioPlayer *audioPlayer; /*单个声音,需要全局*/ - (void)playSound:(NSString*)name { /************ 增加启动声音 **************/ //1.音频文件的url路径 NSURL *url=[[NSBundle mainBundle] URLForResource:name withExtension:Nil]; //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url) _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; //3.缓冲 [_audioPlayer prepareToPlay]; //4.播放 [_audioPlayer play]; } /*other.uiviewcontroller*/ @property (strong, nonatomic) AVAudioPlayer *audioPlayer; @property (strong, nonatomic) AVAudioPlayer *beginPlayer; @property (strong, nonatomic) AVAudioPlayer *pullPlayer; /*多个声音*/ - (void)initSound { /************ 增加声音 *********声音文件放在bundley文件下*****/ NSString *bundlePath =[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sound.bundle"]; NSURL *urlb = [NSURL fileURLWithPath:[bundlePath stringByAppendingPathComponent:@"ja.wav"]]; NSURL *urlp = [NSURL fileURLWithPath:[bundlePath stringByAppendingPathComponent:@"jb.wav"]]; NSURL *url = [NSURL fileURLWithPath:[bundlePath stringByAppendingPathComponent:@"jc.wav"]]; _beginPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:urlb error:Nil]; [_beginPlayer prepareToPlay]; _pullPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:urlp error:Nil]; [_pullPlayer prepareToPlay]; _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; [_audioPlayer prepareToPlay]; /*声音停止通知*/ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(soundStop) name:Notification_sound_Key object:nil]; } - (void)plays { [_audioPlayer play]; } - (void)soundStop { [_beginPlayer stop]; [_pullPlayer stop]; [_audioPlayer stop]; }
以上代码适合,播放mp3这种格式的长音频文件
相关文章推荐
- iOS音频篇:使用AVPlayer播放网络音乐
- iOS音频播放的几种方式,打开后台模式的音乐播放、使用AVAudioPlayer播放音乐、播放指定的音频文件、注册音频文件
- iOS音频篇:使用AVPlayer播放网络音乐
- [IOS 开发] iOS音频篇:使用AVPlayer播放网络音乐
- [IOS 开发] iOS音频篇:使用AVPlayer播放网络音乐
- iOS音频篇:使用AVPlayer播放网络音乐
- iOS音频篇:使用AVPlayer播放网络音乐
- iOS中AVFoundation的简单使用—音乐的播放
- iOS音频篇:使用AVPlayer播放网络音乐
- ios 关于系统音乐和AVAudioPlayer 播放的音频冲突的情况
- iOS开发—音频的播放的简单介绍和封装工具类
- 在IOS中使用AVPlayer去播放在线音频文件,并设置音量
- [iOS]使用Audio Queue Services 播放和录制音频
- ios简单的音频播放
- iOS开发 应用退到后台继续播放音乐或音频文件
- [iOS]使用Audio Queue Services 播放和录制音频
- 最简单的视音频播放示例6:OpenGL播放YUV420P(通过Texture,使用Shader)
- [iOS]使用Audio Queue Services 播放和录制音频
- iOS 简单实现音频的播放
- iOS音频播放 (六):简单的音频播放器实现