Created
September 23, 2012 13:46
-
-
Save 7gano/3771084 to your computer and use it in GitHub Desktop.
kAudioUnitSubType_AudioFilePlayer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <AudioToolbox/AudioToolbox.h> | |
- (void)play | |
{ | |
AUGraph _AUGraph; | |
OSStatus err; | |
AudioUnit remoteIOUnit; | |
AudioUnit audioPlayerUnit; | |
AUNode audioPlayerNode, remoteOutputNode; | |
err = NewAUGraph(&_AUGraph); | |
err = AUGraphOpen(_AUGraph); | |
AudioComponentDescription cd; | |
cd.componentType = kAudioUnitType_Output; | |
cd.componentSubType = kAudioUnitSubType_RemoteIO; | |
cd.componentManufacturer = kAudioUnitManufacturer_Apple; | |
cd.componentFlags = cd.componentFlagsMask = 0; | |
//Node | |
err = AUGraphAddNode(_AUGraph, &cd, &remoteOutputNode); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
//Get Audio Unit | |
err = AUGraphNodeInfo(_AUGraph, | |
remoteOutputNode, | |
NULL, | |
&remoteIOUnit); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
//Create AudioPlayer | |
cd.componentType = kAudioUnitType_Generator; | |
cd.componentSubType = kAudioUnitSubType_AudioFilePlayer; | |
//Node | |
err = AUGraphAddNode(_AUGraph, &cd, &audioPlayerNode); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
//Get Audio Unit | |
err = AUGraphNodeInfo(_AUGraph, | |
audioPlayerNode, | |
NULL, | |
&audioPlayerUnit); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
err = AUGraphConnectNodeInput(_AUGraph, audioPlayerNode, 0, remoteOutputNode, 0); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
err = AUGraphInitialize(_AUGraph); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
err = AUGraphStart(_AUGraph); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
AudioFileID audioFileID; | |
NSURL *url = [[NSBundle mainBundle] URLForResource:@"loop" withExtension:@"wav"]; | |
err = AudioFileOpenURL((__bridge CFURLRef)url, | |
kAudioFileReadPermission, | |
kAudioFileWAVEType, //MP3 | |
&audioFileID); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
AudioFileID audioFileIDs[1]; | |
audioFileIDs[0] = audioFileID; | |
UInt32 size = sizeof(AudioFileID); | |
err = AudioUnitSetProperty(audioPlayerUnit, | |
kAudioUnitProperty_ScheduledFileIDs, | |
kAudioUnitScope_Global, | |
0, | |
audioFileIDs, | |
size); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
ScheduledAudioFileRegion playRegion; | |
playRegion.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid; | |
playRegion.mTimeStamp.mSampleTime = 0; | |
playRegion.mCompletionProc = NULL; | |
playRegion.mCompletionProcUserData = NULL; | |
playRegion.mAudioFile = audioFileID; | |
playRegion.mLoopCount = -1; | |
playRegion.mStartFrame = 0; | |
playRegion.mFramesToPlay = -1; | |
err = AudioUnitSetProperty(audioPlayerUnit, | |
kAudioUnitProperty_ScheduledFileRegion, | |
kAudioUnitScope_Global, | |
0, | |
&playRegion, | |
sizeof(playRegion)); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
// prime | |
UInt32 primeFrames = 0; // default | |
err = AudioUnitSetProperty(audioPlayerUnit, | |
kAudioUnitProperty_ScheduledFilePrime, | |
kAudioUnitScope_Global, | |
0, | |
&primeFrames, | |
sizeof(primeFrames)); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
// set start time (on next render cycle) | |
AudioTimeStamp startTime; | |
startTime.mFlags = kAudioTimeStampSampleTimeValid; | |
startTime.mSampleTime = -1; | |
err = AudioUnitSetProperty(audioPlayerUnit, | |
kAudioUnitProperty_ScheduleStartTimeStamp, | |
kAudioUnitScope_Global, | |
0, | |
&startTime, | |
sizeof(startTime)); | |
if (err) { | |
LOG(@"err = %ld", err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello ! I saw your code,and I'm learning and using AudioPlayerNode. But, I met a lot of problems and couldn't solve. I couldn't find a person to ask for help In my around,So I take the liberty to ask you some questions. I couldn't find relevant data about how to use AVAudioPlayerNode to play music online. If you know , do you mind telling me something about it?