Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 向应用程序添加背景音乐时生成失败_Iphone_Objective C_Ios_Xcode_Ipad - Fatal编程技术网

Iphone 向应用程序添加背景音乐时生成失败

Iphone 向应用程序添加背景音乐时生成失败,iphone,objective-c,ios,xcode,ipad,Iphone,Objective C,Ios,Xcode,Ipad,我有两个问题 1-我正在使用以下代码在我的iphone应用程序中添加背景音乐 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. sleep(5);

我有两个问题

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;

    }
我的第二个问题

假设我必须用一个按钮在另一个
viewController
中关闭此音乐。我该怎么做呢。例如
设置可视控制器

* 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框架进行链接

要停止从其他类/实例/任意位置播放,可以使用多种解决方案:

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


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

您需要将AVFoundation添加到链接框架列表中。@oh great my Dumble。。。你能回答我的第二个问题吗。这有点重要:)@谢谢!:)请随意添加您的正确评论作为答案,即使它看起来很明显(嗯,确实如此)-这很好,它有助于OP,它给了您代表@H2CO3我添加了框架,现在构建工作正常,但是我的应用程序给出了程序接收信号SIGABRT的错误。你能重新检查我的代码并告诉我出了什么问题吗?@umar-Where?你有跟踪记录吗?(对不起,我是读心术的初学者……)嗯,没有堆栈跟踪。它只是在返回的UIApplicationMain(argc、argv、nil、NSStringFromClass([AppDelegate类])上显示绿线;上面说error@umar请在LLDB控制台中键入
bt
* 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