I've been poking around with AVAudioEngine and I'm having trouble integrating AVAudioUnitEffect classes. For example, with AVAudioUnitDelay...
#implementation ViewController {
AVAudioEngine *engine;
AVAudioPlayerNode *player;
}
...
- (IBAction)playButtonHit:(id)sender {
if (!player){
NSURL *bandsURL = [[NSBundle mainBundle] URLForResource:#"Bands With Managers" withExtension:#"mp3"];
AVAudioFile *file = [[AVAudioFile alloc] initForReading:bandsURL error:nil];
engine = [[AVAudioEngine alloc] init];
player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];
AVAudioUnitDelay *delay = [[AVAudioUnitDelay alloc] init];
delay.wetDryMix = 50;
[engine connect:player to:delay format:file.processingFormat];
[engine connect:delay to:[engine outputNode] format:file.processingFormat];
[player scheduleFile:file atTime:nil completionHandler:nil];
[engine prepare];
[engine startAndReturnError:nil];
}
[player play];
}
When the method is called the app crashes and I get this error: "* Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: [_nodes containsObject: node1] && [_nodes containsObject: node2]'"
I'm modeling this after some of the examples from the "AVAudioEngine in Practice" session from WWDC. I know there's probably something obvious I'm missing but can't figure it out....
You forgot to attach your AvAudioUnitDelay object to your AvAudioEngine nodes before linking them ;)
Here is the working code :
- (IBAction)playMusic:(id)sender {
if (!player){
NSURL *bandsURL = [[NSBundle mainBundle] URLForResource:#"Bands With Managers" withExtension:#"mp3"];
AVAudioFile *file = [[AVAudioFile alloc] initForReading:bandsURL error:nil];
engine = [[AVAudioEngine alloc] init];
player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];
AVAudioUnitDelay *delay = [[AVAudioUnitDelay alloc] init];
delay.wetDryMix = 50;
[engine attachNode:delay];
[engine connect:player to:delay format:file.processingFormat];
[engine connect:delay to:[engine outputNode] format:file.processingFormat];
[player scheduleFile:file atTime:nil completionHandler:nil];
[engine prepare];
[engine startAndReturnError:nil];
}
[player play];
}
It is not a problem of the AVAudioUnitEffect! I tried it with that code
NSError *err = nil;
self.engine = [[AVAudioEngine alloc] init];
AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init];
[self.engine attachNode:player];
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:#"sound" withExtension:#"m4a"];
AVAudioFile *file = [[AVAudioFile alloc] initForReading:fileURL error:&err];
AVAudioMixerNode *mainMixer = [self.engine mainMixerNode];
[self.engine connect:player to:mainMixer format:file.processingFormat];
[player scheduleFile:file atTime:nil completionHandler:nil];
[self.engine startAndReturnError:&err];
if (err != nil) {
NSLog(#"An error occured");
}
[player play];
while self.engine is defined by
#property (nonatomic, strong) AVAudioEngine *engine;
I think that is a bug in AVAudioEngine, because it causes a memory leak: It starts playing the first samples and then it crashes because of heavy memory usage (more than 300 MB in my case of a 16 kB m4a file).
Update 12/07/2014: Apple fixed this issue with iOS 8 Seed 3 (Build 12A4318c)!
Related
I'm looking for a solution to record an audio stream to a file. I can get audio to play but I'm struggling to figure out how to record/save to file what is playing.
A nudge in the right direction would be greatly appreciated.
Player code thus far:
#interface ViewControllerPlayer ()
#end
#implementation ViewControllerPlayer
#synthesize receivedStreamReferenceNumber;
- (void)convertData:(NSData *) data {
NSString *urlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[self loadPlayer:url];
}
- (void) loadPlayer:(NSURL *) url {
audioPlayer = [AVPlayer playerWithURL:url];
[audioPlayer play];
}
- (void) start {
NSLog(#"%#", receivedStreamReferenceNumber);
NSString *urlHalf = #"http://getstreamurl.php?KeyRef=";
NSMutableString *mutableUrlString = [NSMutableString stringWithFormat:#"%#%#", urlHalf, receivedStreamReferenceNumber];
NSURL *url = [NSURL URLWithString: mutableUrlString];
NSData *data = [NSData dataWithContentsOfURL:url];
[self convertData:data];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self start];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
EDIT:
I've incorporated the following code... but i'm struggling to tie it all together. The following should create a file that the recorded stream audio is saved to. I suppose i've got to tell the AVRecorder to listen to the AVPlayer some how? Again -- help will be greatly appreciated:
- (void)viewDidLoad
{
[super viewDidLoad];
[stopButton setEnabled:YES];
[playButton setEnabled:YES];
// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
#"xxx.mp3",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
I have a problem i have been trying to solve for a day and i tired of beating my head aaginst the wall. Anyone know how to do this?
Number 1 Works Great
Works Great
- (void) viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://techslides.com/demos/sample-videos/small.mp4"];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:url];
self.player = avPlayer;
[self.player play];
}
Does not work
- (void) viewDidLoad {
[super viewDidLoad];
CKAsset *asset = record[#"VideoFile"];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:asset.fileURL];
self.player = avPlayer;
[self.player play];
}
Does not work
- (void) viewDidLoad {
[super viewDidLoad];
CKAsset *asset = record[#"VideoFile"];
NSString *fileName = record[#"videoFileKey"];
NSData *data = [NSData dataWithContentsOfURL:asset.fileURL];
NSString *cachesDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [cachesDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp4", fileName]];
[data writeToFile:path atomically:YES];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL *url = [NSURL URLWithString:path];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:url];
self.player = avPlayer;
[self.player play];
}
}
I have a class that provides a Music Player, compiled under ARC. This is the init code:
- (id) init{
self = [super init];
runningVolumeNotification = FALSE;
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
systemVolume = musicPlayer.volume;
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"servo" ofType:#"mp3"];
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[p prepareToPlay];
[p stop];
return self;
}
Instruments reports a 100% leak on the line:
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
I can't figure out where the problem is!
In one of the views of my app there's a button. When pressed it is supposed to begin taking a video, trigger a sound file to start, and hide itself from view while unhiding another button. The second button is supposed to stop the video recording and make it save. Here's the code I have for the video recording, which initially worked with no problems:
in viewDidLoad:
finishButton.hidden = TRUE;
session = [[AVCaptureSession alloc] init];
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
NSError *error;
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self cameraWithPosition:AVCaptureDevicePositionFront] error:&error];
if (videoInput)
{
[session addInput:videoInput];
}
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *audioError = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&audioError];
if (audioInput)
{
[session addInput:audioInput];
}
Float64 TotalSeconds = 35; //Total seconds
int32_t preferredTimeScale = 30; //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);
movieFileOutput.maxRecordedDuration = maxDuration;
movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;
if ([session canAddOutput:movieFileOutput])
[session addOutput:movieFileOutput];
[session setSessionPreset:AVCaptureSessionPresetMedium];
if ([session canSetSessionPreset:AVCaptureSessionPreset640x480]) //Check size based configs are supported before setting them
[session setSessionPreset:AVCaptureSessionPreset640x480];
[self cameraSetOutputProperties];
[session startRunning];
and for the button:
-(IBAction)start:(id)sender
{
startButton.hidden = TRUE;
finishButton.hidden = FALSE;
//Create temporary URL to record to
NSString *outputPath = [[NSString alloc] initWithFormat:#"%#%#", NSTemporaryDirectory(), #"output.mov"];
self.outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
NSError *error;
if ([fileManager removeItemAtPath:outputPath error:&error] == NO)
{
//Error - handle if required
}
}
//Start recording
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
finally, under the last button:
[movieFileOutput stopRecording];
and here's the code to save the video:
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections
error:(NSError *)error
{
NSLog(#"didFinishRecordingToOutputFileAtURL - enter");
BOOL RecordedSuccessfully = YES;
if ([error code] != noErr)
{
// A problem occurred: Find out if the recording was successful.
id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
if (value)
{
RecordedSuccessfully = [value boolValue];
}
}
if (RecordedSuccessfully)
{
//----- RECORDED SUCESSFULLY -----
NSLog(#"didFinishRecordingToOutputFileAtURL - success");
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:outputURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
}
}];
}
}
}
All of this was working just fine. Then I added a few lines so that a song file would play when the start button was pressed.
in viewDidLoad:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Song.aiff", [[NSBundle mainBundle] resourcePath]]];
NSError *audioFileError;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&audioFileError];
player.numberOfLoops = 0;
[self.player prepareToPlay];
and under the start button:
if (player == nil)
NSLog(#"Audio file could not be played");
else
[player play];
Now when the start button is pressed the song plays with no problems, but the video capture is messed up. Before adding the AVAudioPlayer stuff I would get the "didFinishRecordingToOutputFileAtURL - enter" and "didFinishRecordingToOutputFileAtURL - success" logs when I pressed the finish button, and now I get the first log as soon as I press the start button, nothing happens when I press the finish button, and no video is recorded. If I comment out the lines that make the song play then the video capture works just fine again. Any ideas what's going on here?
- (void)setupAudioSession
{
static BOOL audioSessionSetup = NO;
if (audioSessionSetup)
{
return;
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
audioSessionSetup = YES;
}
- (void)playAudio
{
[self setupAudioSession];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"btnClick" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.audioPlayer = newPlayer;
[newPlayer release];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
audioPlayer.volume=1.0;
[audioPlayer play];
}
NOTE: Add the framework: AudioToolbox.framework.
#import <AudioToolbox/AudioServices.h>
I wrote this source program . But I can't call audioPlayerDidFinishPlaying: method.
After playing the sound, which crashes by "exc_bad_access" error after a few seconds.
.h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface SecondViewController : UIViewController<
AVAudioPlayerDelegate>{
AVAudioPlayer *aPlayer;
}
#end
.m file
-(void)playSound{
NSString *soundName = #"red";
NSError *error = nil;
NSURL *soundUrl = [[NSBundle mainBundle] URLForResource:soundName withExtension:#"mp3"];
aPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
if (error != nil) {
NSLog(#"audio_player initialized error :(%#)",[error localizedDescription]);
[aPlayer release];
error=nil;
return;
}
NSLog(#"player Ok!");
aPlayer.delegate = self;
[aPlayer prepareToPlay];
aPlayer.volume=1.0f;
[aPlayer play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player release];
}
This is what I use that works perfectly, it should help you.
-(void)playSound{
NSString *name = [[NSString alloc] initWithFormat:#"red"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:#"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{
NSLog(#"my log");
[data release];
}