首先,需要导入AudioToolbox.framework和OpenAL.framework。
OpenALViewController.h
#import <UIKit/UIKit.h>
#import <OpenAL/alc.h>
@interface OpenALViewController : UIViewController {
ALCcontext *mContext;
ALCdevice *mDevice;
NSUInteger sourceID;
NSUInteger bufferID;
BOOL isPlaying;
}
- (IBAction)playPause:(id)sender;
@end
OpenALViewController.m
#import "OpenALViewController.h"
#import <AudioToolbox/AudioFile.h>
#import <OpenAL/al.h>
@implementation OpenALViewController
- (void)initOpenAL {
mDevice = alcOpenDevice(NULL);
if (mDevice) {
mContext = alcCreateContext(mDevice, NULL);
alcMakeContextCurrent(mContext);
}
}
- (AudioFileID)openAudioFile:(NSString *)filePath {
AudioFileID outAFID;
NSURL *afUrl = [NSURL fileURLWithPath:filePath];
#if TARGET_OS_IPHONE
OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, kAudioFileReadPermission, 0, &outAFID);
#else
OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, fsRdPerm, 0, &outAFID);
#endif
if (result != 0) NSLog(@"cannot open file: %@", filePath);
return outAFID;
}
- (UInt32)audioFileSize:(AudioFileID)fileDescriptor {
UInt64 outDataSize = 0;
UInt32 thePropSize = sizeof(UInt64);
OSStatus result = AudioFileGetProperty(fileDescriptor, kAudioFilePropertyAudioDataByteCount, &thePropSize, &outDataSize);
if(result != 0) NSLog(@"cannot find file size");
return (UInt32)outDataSize;
}
- (void)cleanUpOpenAL {
alDeleteSources(1, &sourceID);
alDeleteBuffers(1, &bufferID);
alcDestroyContext(mContext);
alcCloseDevice(mDevice);
}
- (IBAction)playPause:(id)sender {
UIButton *btnPlay = (UIButton *)sender;
if(!isPlaying) {
alSourcePlay(sourceID);
isPlaying = YES;
[btnPlay setTitle:@"Pause" forState:UIControlStateNormal];
} else {
alSourceStop(sourceID);
isPlaying = NO;
[btnPlay setTitle:@"Play" forState:UIControlStateNormal];
}
}
#pragma mark -
- (void)viewDidLoad {
[super viewDidLoad];
isPlaying = NO;
[self initOpenAL];
NSString *fileName = [[NSBundle mainBundle] pathForResource:@"outSound" ofType:@"caf"];
AudioFileID fileID = [self openAudioFile:fileName];
UInt32 fileSize = [self audioFileSize:fileID];
unsigned char *outData = malloc(fileSize);
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);
AudioFileClose(fileID);
if (result != 0) NSLog(@"cannot load effect: %@", fileName);
alGenBuffers(1, &bufferID);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, 8000);
alGenSources(1, &sourceID);
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
alSourcei(sourceID, AL_LOOPING, AL_TRUE);
if (outData) {
free(outData);
outData = NULL;
}
}
- (void)dealloc {
[self cleanUpOpenAL];
[super dealloc];
}
@end
分享到:
相关推荐
OpenAL32.dll 放到windows/system32下
openal-soft, OpenAL软是 OpenAL 3D 音频API的软件实现 OpenAL软master 分支CI状态: OpenAL软是一个lgpl许可,跨平台,软件实现 OpenAL 3D 音频 API 。 它是从开源的Windows 版本派生的,最初来自 openal.org's SVN...
声音程序,可用于Vega prime
openal freealut mingw bin openal freealut mingw bin openal freealut mingw bin
Creative Labs OpenAL vv2.07
OpenAL 开放音频库在VS2005下的配置.
非常简单的OpenAL简易播放器 可以实现WAV文件播放、暂停、停止、调节音量等功能
OpenAL-v2.0.3.
使用OPENAL实现录音及播放功能,对需要进行录音流数据处理的应用是一个很好的demo
OpenAL和OpenGL编码风格很像,跟OpenGL中我们常用的glut类似,OpenAL中也用alut. 实现简单的播放音频功能。
openal安装程序,支持3D声音的免费库
openal32.rar !
OPENAL是一个音效API,负责系统和声卡之间的沟通。
openal 声音播放类,C++,Linux
OpenAL Programmers Guide
缺少这个东西有些游戏将不能正常运行,只有将它放到C盘的系统下
OpenAL文档中文注释版
android开发openal源代码,对于android音频播放输出数据,按一定的方波输出,openal源代码为C,ANDROID通过jni调用底层