我有两个问题。
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 的 undefined symbol :
"_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
中使用按钮关闭此音乐。我怎样才能做到这一点。例如 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
最好的问候
最佳答案
这是一个链接器错误,您必须针对 AVFoundation 框架进行链接。
要从另一个类/实例/任何你想要的地方停止播放,你可以有多种解决方案:
一个。在要停止播放的类上声明一个属性,并将该属性设置为原始 View Controller 。然后使用该属性访问该 View Controller 并在其上调用一个方法来停止音乐。
两个。在一些中心类中,例如您的应用程序委托(delegate)或主视图 Controller ,添加一个 NSNotificationCenter-observer 并在整个应用程序中简单地使用通知,而不会弄乱属性。
关于iphone - 向应用程序添加背景音乐时构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12132917/