Simple openal example

Source: Internet
Author: User

First, you need to import audiotoolbox. Framework and 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_IPHONEOSStatus result = AudioFileOpenURL((CFURLRef)afUrl, kAudioFileReadPermission, 0, &outAFID);#elseOSStatus result = AudioFileOpenURL((CFURLRef)afUrl, fsRdPerm, 0, &outAFID);#endifif (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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.