2016-04-09 18 views
0

我正在製作一個播放音頻文件的Swift iOS應用程序,我正在使用The Amazing Audio Engine 2庫。構建時的Segfault代碼11可能是由Objective-C Swift項目中的Singleton造成的

我決定使用Singleton模式來管理這個任務。在他的Swift示例項目中,該庫的創建者Michael Tyson在Objective-C中完成了他的音頻部分(如果我理解的話,對於具有C函數的內存管理工具)。 你可以在這裏找到他的解釋視頻:https://www.youtube.com/watch?v=OZQT4IGS8mA 所以我跟着他的領導,我在Objective-C編碼我的單身人士。

這裏是我的代碼:

AudioManager.h

#import <Foundation/Foundation.h> 
#import "TheAmazingAudioEngine/TheAmazingAudioEngine.h" 

@class Track; 

@interface AudioManager : NSObject { 
    NSString *test; 
} 

@property (nonatomic, strong) AEAudioUnitOutput *_Nonnull output; 
@property (nonatomic, strong) AERenderer *_Nonnull renderer; 
@property (nonatomic, strong) AEAudioFilePlayerModule *_Nullable currentTrackModule; 
@property (nonatomic) BOOL recording; 
@property (nonatomic) Track *_Nullable currentTrack; 

+ (_Nonnull id)defaultAudioManager; 
- (BOOL)startAudioController:(NSError *_Nullable *_Nullable)error; 
- (void)stopAudioController; 
- (void)prepareTrackModule; 
- (void)playCurrentTrack:(BOOL)loop; 

@end 

AudioManager.m

#import "AudioManager.h" 
#import "Project-Swift.h" 
@import AVFoundation; 

@implementation AudioManager 

//@synthesize someProperty; 

#pragma mark Singleton Methods 

+ (id)defaultAudioManager { 
    static AudioManager *defaultAudioManager = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     defaultAudioManager = [[self alloc] init]; 
    }); 
    return defaultAudioManager; 
} 

- (id)init { 
    if (!(self = [super init])) return nil; 
    //someProperty = @"Default Property Value"; 
    self.renderer = [AERenderer new]; 
    self.output = [[AEAudioUnitOutput alloc] initWithRenderer:self.renderer]; 

    return self; 
} 

- (void)dealloc { 
    // Should never be called, but just here for clarity really. 
} 

- (BOOL)startAudioController:(NSError * _Nullable * _Nullable)error { 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL]; 

    return [self.output start:error]; 
} 

- (void)stopAudioController { 
    return [self.output stop]; 
} 

- (void)prepareTrackModule { 
    self.currentTrackModule = [[AEAudioFilePlayerModule alloc] initWithRenderer:self.renderer 
                URL:[[NSBundle mainBundle] 
                 URLForResource:@"amen"//self.currentTrack.name 
                 withExtension:@"m4a"] 
               error:NULL]; 
} 

- (void)playCurrentTrack:(BOOL)loop { 
    self.currentTrackModule.loop = loop; 
    [self.currentTrackModule playAtTime:0]; 
    __unsafe_unretained typeof(self) weakSelf = self; 
    self.renderer.block = ^(const AERenderContext *context) { 
     AEModuleProcess(weakSelf.currentTrackModule, context); 

     AEBufferStackMixToBufferList(context->stack, 1, 0, YES, context->output); 
    }; 
} 

@end 

AppDelegate.Swift

import UIKit 
import CoreData 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Override point for customization after application launch. 

     do { 
      try AudioManager.defaultAudioManager().startAudioController() 
      //AudioManager.defaultAudioManager().prepareTrackModule() 
      //AudioManager.defaultAudioManager().playCurrentTrack(true) 
     } catch { 
      //Handle Error 
      print("error") 
     } 

     return true 
    } 

    func applicationWillResignActive(application: UIApplication) { 
     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    } 

    func applicationDidEnterBackground(application: UIApplication) { 
     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    } 

    func applicationWillEnterForeground(application: UIApplication) { 
     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
    } 

    func applicationDidBecomeActive(application: UIApplication) { 
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    } 

    func applicationWillTerminate(application: UIApplication) { 
     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
     // Saves changes in the application's managed object context before the application terminates. 
     self.saveContext() 
    } 
} 

如果我評論的線try AudioManager.defaultAudioManager().startAudioController()AppDelegate.swift文件,顯然一切正常,否則,Xcode中打印我這個非非常有幫助的錯誤,當我嘗試編譯:

0 swift     0x000000010411d4eb llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 43 
1 swift     0x000000010411c7d6 llvm::sys::RunSignalHandlers() + 70 
2 swift     0x000000010411db4f SignalHandler(int) + 287 
3 libsystem_platform.dylib 0x00007fff9a64352a _sigtramp + 26 
4 libsystem_platform.dylib 0x0000000000000001 _sigtramp + 1704708849 
5 swift     0x0000000101fba410 (anonymous namespace)::IRGenSILFunction::visitFullApplySite(swift::FullApplySite) + 2736 
6 swift     0x0000000101fa7c4b swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9787 
7 swift     0x0000000101f02fd8 swift::irgen::IRGenModuleDispatcher::emitGlobalTopLevel() + 600 
8 swift     0x0000000101f8ea5e performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1278 
9 swift     0x0000000101f8ef06 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 70 
10 swift     0x0000000101e72a1c performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&) + 15004 
11 swift     0x0000000101e6e41d frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2781 
12 swift     0x0000000101e69e3c main + 1932 
13 libdyld.dylib   0x00007fff996215ad start + 1 
14 libdyld.dylib   0x00000000000000bc start + 1721625360 
Stack dump: 

Command failed due to signal: Segmentation fault: 11

我認爲我缺少的東西Objective- C部分,我不明白,我不是很熟悉這種語言,但我不知道是什麼。 任何想法?

+0

在startAudiocontroller函數它接受一個NSError參數,你沒有傳遞任何值。即使它是可空的,swift也不適用於空值,並且必須使用if let或guard來顯式處理。嘗試解決NSError部分,看看是否可以解決問題。 –

+0

@ArunGupta我不知道我要做什麼,我不能將任何參數傳遞給'appDelegate.swift'中的'startAudioController',而Xcode沒有打印錯誤'無法調用非函數類型的值'(()拋出 - >())!'' –

回答

0

事實上,如果我刪除startAudioController函數的參數,segfault消失。我不知道爲什麼,但它可以是一個臨時解決方案

相關問題