这是我的第一个问题。我正在尝试制作一款适用于Core Audio的应用。我找到了我正在尝试使用的这个框架http://theamazingaudioengine.com/,到目前为止,我设法在文档中做了第一件事,即播放文件。但是,通过在app的委托中自定义初始化UIViewController,我丢失了所有内容,视图控制器变黑,没有其他元素。
我的UIViewController只有一个按钮,我想用它来开始播放文件,但由于我无权访问它,此时文件会在项目构建时开始播放。
知道我做错了什么吗?
这是我的appDelegate:
@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
我的UIViewController:
@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
答案 0 :(得分:2)
由于您正在使用故事板,因此您应该从应用程序委托中取出所有代码。故事板自动实例化您的初始控制器并将其放在屏幕上。通过alloc init'ing,你只需创建另一个没有任何自定义视图的那个。
要添加音频控制器,您应该在SoundCheckViewController的viewDidLoad方法中添加代码,而不是在init方法中。这是通常的方法,但我不确定你正在使用的框架有什么可能。
答案 1 :(得分:0)
我认为你应该首先初始化你的视图控制器。
- (id)initWithAudioController:(AEAudioController*)audioController {
// THIS LINE IS MISSING IN YOUR CODE
self = [super initWithNibName:@"SoundCheckViewController" bundle:nil];
if ( self ) {
self.audioController = audioController;
...
}
return self;
}