2012-08-26 34 views
0

我有两个问题。构建失败,同时向应用添加背景音乐

1-我使用以下代码在我的iPhone应用程序中添加背景音乐。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
     // Override point for customization after application launch. 
     sleep(5); 
     //this variable can be named differently 

     NSString *path = [[NSBundle mainBundle] pathForResource:@"%@/background_music" ofType:@"wav"]; 


    // the file is stored in MyApp/SharedResources folder where as delegate is placed at MyApp/   
      backgroundSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] 

error:nil]; 

     backgroundSound.delegate = self; 

     [backgroundSound prepareToPlay]; 

     [backgroundSound play]; 

     backgroundSound.numberOfLoops = -1; 


     return YES; 
    } 

    @interface AppDelegate : UIResponder <UIApplicationDelegate>{ 
     AVAudioPlayer *backgroundSound; 

    } 

通过运行此代码我得到建立i386硬件架构failed.Undefined符号:

"_OBJC_CLASS_$_AVAudioPlayer", referenced from: 
     objc-class-ref in AppDelegate.o 
ld: symbol(s) not found for architecture i386 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我的第二个问题..

想我必须把这种音乐关在另一个viewController用一个按钮。我怎样才能做到这一点。 E.G SettingsViewController

* thread #1: tid = 0x2c03, 0x90870c5a libSystem.dylib`__kill + 10, stop reason = signal SIGABRT 
    frame #0: 0x90870c5a libSystem.dylib`__kill + 10 
    frame #1: 0x90870c4c libSystem.dylib`kill$UNIX2003 + 32 
    frame #2: 0x909035a5 libSystem.dylib`raise + 26 
    frame #3: 0x909196e4 libSystem.dylib`abort + 93 
    frame #4: 0x90895b1b libSystem.dylib`_Unwind_Resume + 59 
    frame #5: 0x016c8e39 CoreFoundation`CFRunLoopRunSpecific + 345 
    frame #6: 0x016c8ccb CoreFoundation`CFRunLoopRunInMode + 123 
    frame #7: 0x0019e2a7 UIKit`-[UIApplication _run] + 576 
    frame #8: 0x0019fa9b UIKit`UIApplicationMain + 1175 
    frame #9: 0x00002a62 myApp`main + 130 at main.m:16 
    frame #10: 0x000029d5 myApp`start + 53 

问候

+0

您需要将AVFoundation添加到链接框架的列表中。 – Till

+0

@不错哦,我的愚蠢......你也可以回答我的第二个问题。它的一种重要:) –

回答

2

这是一个链接错误,你必须针对AVFoundation框架进行链接。

从另一个类/实例/停止播放,无论你想,你可以有多种解决方案:

一个。在要停止播放的类上声明属性,并将该属性设置为原始视图控制器。然后使用该属性访问该视图控制器并调用其上的方法来停止音乐。

二。在一些中心类中,如应用程序委托或主视图控制器,添加一个NSNotificationCenter观察器,并在整个应用程序中使用通知,而不会混淆属性。

+1

+1正确答案:D – Till

+0

@Till谢谢! :)即使看起来很明显,也可以随意添加你的正确评论作为答案 - 这很好,它有助于OP,它会给你代表。 – 2012-08-26 19:10:44

+0

所有的好,只是逗逗 - 你应得的。 – Till