Does it work with storyboards and how?

edited April 2013

Hi,

I'm trying to get through the first pages of the documentation and so far I managed to play a file. I wanted to give a button to my ViewController to start/stop playing the file. However, my ViewController comes out black, with no elements, when the project builds.

I asked on another forum and I got an answered that if I use storyboards I should take the initializer code out of my appDelegate, that this only initializes a new, empty, ViewController.

My questions are:
1. Where should I put the code for initializing the audioController and the viewController?
2. What to do in order to maintain the layout I'm creating in storyboards?
3. Is it possible at all to work with storyboards with the Amazing Audio Engine? And if so, is it documented?

I'm pretty new at coding and I'm very confused about making all this work, so thanks in advance.
K.

This is my code so far:

My app delegate:

@implementation SoundCheckAppDelegate

@synthesize window = _window;
@synthesize audioController = _audioController;
@synthesize viewController = _viewController;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

    // Create an instance of the audio controller, set it up and start it running
    self.audioController = [[[AEAudioController alloc] initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription] inputEnabled:YES] autorelease];
    _audioController.preferredBufferDuration = 0.005;
    [_audioController start:NULL];

    // Create and display view controller
    self.viewController = [[SoundCheckViewController alloc] initWithAudioController:_audioController];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
    }

@end

and my view controller:

@interface SoundCheckViewController ()

@property (nonatomic, strong) AEAudioController *audioController;
@property (nonatomic, strong) AEAudioFilePlayer *loop;

@end

@implementation SoundCheckViewController

  • (id)initWithAudioController:(AEAudioController*)audioController {
    self.audioController = audioController;

    NSError *error;

    NSURL *file = [[NSBundle mainBundle] URLForResource:@Southern Rock Drums withExtension:@m4a];
    self.loop = [AEAudioFilePlayer audioFilePlayerWithURL:file
    audioController:_audioController
    error:&error];
    if(error)
    NSLog(@couldn't start loop);

    _loop.removeUponFinish = YES;
    _loop.loop = YES;
    _loop.completionBlock = ^{
    self.loop = nil;
    };

    [_audioController addChannels:[NSArray arrayWithObject:_loop]];

    return self;
    }

@end

Comments

  • got it to work by moving all the code from appDelegate and calling [self initWithAudioController:self.audioController]; in SoundCheckViewController's viewDidLoad

  • Hey kanstraktar, can you share your working version as well? I'm struggling with the sam problem...

    Thanks!

  • In case it helps anyone else, this worked:

    in the ViewController's viewDidLoad:

     // Create an instance of the audio controller, set it up and start it running
        self.audioController = [[AEAudioController alloc] initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription] inputEnabled:NO];
        _audioController.preferredBufferDuration = 0.005;
        [_audioController start:NULL];
        
        [self initWithAudioController:self.audioController];

    Then in initWithAudioController (in viewController):

    self.audioController = audioController;

    followed by whatever you want to do.

  • It's not a good idea to do this kind of setup in viewDidLoad. This method should be used to set up the view only. It can be called multiple times in some cases (for example a memory warning could release the view if it's not currently visible, and then it would load again when it becomes visible).

  • Thanks, j_...it's been kind of trial-and-error so I'm not surprised I'm making less-than-stellar choices.

    How would you suggest structuring an integration of TAAE with storyboards?

  • I've never used storyboards, but why can't you put it in the app delegate?

  • Ignorance, basically. Obj-C is messing with my head. I'll see if I can figure out something more sensible tomorrow or at least post a concrete question.

  • hey,
    sorry for the late reply, i haven't logged in since i've given up on this. hope this will help:

    in my viewcontroller.h, i had this:

    @property (strong, nonatomic) AEAudioController *audioController;

    • (id)initWithAudioController:(AEAudioController*)audioController;

    then, in my implemetation, a property:

    @property (nonatomic, strong) AEAudioFilePlayer *blackKeysLoop;

    and this:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Create an instance of the audio controller, set it up and start it running
        self.audioController = [[AEAudioController alloc]
                                initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription]
                                inputEnabled:YES]; // don't forget to autorelease if you don't use ARC!
        
        
        // Create and display view controller
        [self initWithAudioController:self.audioController];
        
    }
    
    - (id)initWithAudioController:(AEAudioController *)audioController
    {
        self.audioController = audioController;
        
        // Check that the engine has successfully started.
        NSError *error = NULL;
        BOOL result = [self.audioController start:&error];
        if ( !result ) {
            // Report error
            NSLog(@The Amazing Audio Engine didn't start!);
        } else {
            NSLog(@The Amazing Audio Engine started perfectly!);
        }
        
        [self createFilePlayer];
     
        
        return self;
    }
    
    - (void)createFilePlayer
    {
        NSURL *file = [[NSBundle mainBundle] URLForResource:@black keys withExtension:@mp3];
        self.blackKeysLoop = [AEAudioFilePlayer audioFilePlayerWithURL:file
                                              audioController:_audioController
                                                        error:NULL];
        _blackKeysLoop.volume = 1.0;
        _blackKeysLoop.channelIsMuted = NO;
        _blackKeysLoop.loop = YES;
        
        [_audioController addChannels:[NSArray arrayWithObject:_blackKeysLoop]];
    }
    

    hope this works. for me it was too much trying to work with audio, as i have no clue of C and i'm just getting started with Objective C, so...

    cheers

  • this should work if you followed all the instructions here http://theamazingaudioengine.com/doc/_getting-_started.html up until it goes into the AEAudioController

  • j_liljedahl — I can do the setup and start of the audio engine in the app delegate no problem. How do I then access that instance from within a view controller?

  • So these two SO questions combined answer mine.

    http://stackoverflow.com/questions/7952576/xcode-4-2-tabbed-application-adding-new-tab-view

    http://stackoverflow.com/questions/5082738/ios-calling-app-delegate-method-from-viewcontroller

    j_liljehdahl, does this seem right?

    1) Follow the tutorial up to the starting up of the engine.

    2)
    HomeViewController.h

    #import 
    #import "VideoPlaybackAppDelegate.h"
    
    @interface HomeViewController : UIViewController
    {
        VideoPlaybackAppDelegate *appDelegate;
    }
    @end
    

    HomeViewController.m

    #import "HomeViewController.h"
    #import "TheAmazingAudioEngine.h"
    
    @interface HomeViewController ()
    {
    }
    @property (nonatomic, retain) AEAudioFilePlayer *audioFilePlayer;
    @end
    
    @implementation HomeViewController
    
    ...
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
            // Do any additional setup after loading the view.
        appDelegate = (VideoPlaybackAppDelegate *)[[UIApplication sharedApplication] delegate];
        NSError *error = NULL;
        NSURL *file = [[NSBundle mainBundle] URLForResource:@testclicktrack withExtension:@m4a];
        self.audioFilePlayer = [AEAudioFilePlayer audioFilePlayerWithURL:file audioController:appDelegate.audioController error:&error];
        if(error != NULL)
        {
            NSLog(@Error: %@", &error);
        }
        _audioFilePlayer.channelIsMuted = NO;
        _audioFilePlayer.loop = YES;
        _audioFilePlayer.volume = 1.0;
        [appDelegate.audioController addChannels:[NSArray arrayWithObject:_audioFilePlayer]];
    }
    

    It works, but I just want to make sure I'm not doing anything stupid :)

Sign In or Register to comment.