2012-07-24 52 views
0

EDITED 仍然不知道什么是错,请帮助播放声音同时应用程序运行

您好我创建和iOS的应用程序,并试图运行我已经输入了我的代码的时候,使其播放声音应用程序的委托.H,.M并播放声音不错,但问题是它会黑屏,当我ViewController.xib有一个蓝色的背景继承人继承人的代码,我

AppDelegate.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
@class ViewController; 

@interface AppDelegate : NSObject <UIApplicationDelegate, AVAudioPlayerDelegate> { 
    UIWindow *window; 
    ViewController *viewController; 
    AVAudioPlayer *_backgroundMusicPlayer; 
    BOOL _backgroundMusicPlaying; 
    BOOL _backgroundMusicInterrupted; 
    UInt32 _otherMusicIsPlaying; 
} 


@property (nonatomic, retain) IBOutlet UIWindow *window; 

@property (nonatomic, retain) IBOutlet ViewController *viewController; 

- (void)tryPlayMusic; 

AppDelegate.m

#import "AppDelegate.h" 

#import "ViewController.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize viewController = _viewController; 

- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    // Set up the audio session 
    // See handy chart on pg. 55 of the Audio Session Programming Guide for what the categories mean 
    // Not absolutely required in this example, but good to get into the habit of doing 
    // See pg. 11 of Audio Session Programming Guide for "Why a Default Session Usually Isn't What You Want" 
    NSError *setCategoryError = nil; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError]; 

    // Create audio player with background music 
    NSString *ticktockPath = [[NSBundle mainBundle] pathForResource:@"ticktock" ofType:@"wav"]; 
    NSURL *ticktockURL = [NSURL fileURLWithPath:ticktockPath]; 
    NSError *error; 
    _backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:ticktockURL error:&error]; 
    [_backgroundMusicPlayer setDelegate:self]; // We need this so we can restart after interruptions 
    [_backgroundMusicPlayer setNumberOfLoops:-1]; // Negative number means loop forever 

    // Override point for customization after app launch  
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 

- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player { 
    _backgroundMusicInterrupted = YES; 
    _backgroundMusicPlaying = NO; 
} 

- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player { 
    if (_backgroundMusicInterrupted) { 
     [self tryPlayMusic]; 
     _backgroundMusicInterrupted = NO; 
    } 
} 

- (void)applicationDidBecomeActive:(NSNotification *)notification { 
    [self tryPlayMusic]; 
} 

- (void)tryPlayMusic { 


    // Play the music if no other music is playing and we aren't playing already 
    if (_otherMusicIsPlaying != 1 && !_backgroundMusicPlaying) { 
     [_backgroundMusicPlayer prepareToPlay]; 
     [_backgroundMusicPlayer play]; 
     _backgroundMusicPlaying = YES; 
    } 
} 

- (void)dealloc { 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 


@end 

确定,所以就是所有的代码

enter image description here

和继承人我所得到的,当应用程序加载时,声音正常工作

enter image description here

,这就是我想要的(ViewController.xib)

enter image description here

感谢先进

- (void)applicationDidFinishLaunching:(UIApplication *)application {  


    NSError *setCategoryError = nil; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError]; 
    self.viewController = [[ViewController alloc] init]; 
    // Create audio player with background music 
    NSString *ticktockPath = [[NSBundle mainBundle] pathForResource:@"ticktock" ofType:@"wav"]; 
    NSURL *ticktockURL = [NSURL fileURLWithPath:ticktockPath]; 
    NSError *error; 
    _backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:ticktockURL error:&error]; 
    [_backgroundMusicPlayer setDelegate:self]; // We need this so we can restart after interruptions 
    [_backgroundMusicPlayer setNumberOfLoops:-1]; // Negative number means loop forever 

    // Override point for customization after app launch  
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 

新代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

这与声音没有任何关系。可能是您初始化ViewController的问题。你可以发布该代码吗? (ViewController.m) – Dima 2012-07-24 17:44:00

+0

此刻我在视图controller.m或.h中没有代码 – Picm 2012-07-24 17:51:44

+0

根本没有代码?只是一个空白的文件?必须有一些东西在里面。 – Dima 2012-07-24 18:12:42

回答

1

你永远不初始化您的视图控制器。

你这样做某处之前

[window addSubview:viewController.view]; 

你需要做的

self.viewController = [[ViewController alloc] init]; 

我看到你声明的属性作为一个IBOutlet ..它实际上是迷上了在Interface Builder的东西吗?


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

    NSError *setCategoryError = nil; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError]; 
    // Create audio player with background music 
    NSString *ticktockPath = [[NSBundle mainBundle] pathForResource:@"ticktock" ofType:@"wav"]; 
    NSURL *ticktockURL = [NSURL fileURLWithPath:ticktockPath]; 
    NSError *error; 
    _backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:ticktockURL error:&error]; 
    [_backgroundMusicPlayer setDelegate:self]; // We need this so we can restart after interruptions 
    [_backgroundMusicPlayer setNumberOfLoops:-1]; // Negative number means loop forever 

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

复制功能,只需删除这个- (void)applicationDidFinishLaunching:(UIApplication *)application完全。

您还应该考虑将您的项目转换为使用ARC。它将消除保留/释放/自动释放语句的需要。

+0

时,它只是正常的代码我在哪里有这个应用程序委托或视图控制器 – Picm 2012-07-24 17:55:49

+0

在'(void)applicationDidFinishLaunching:(UIApplication *)application'函数中。 – Dima 2012-07-24 17:56:48

+0

对不起,我迷路了,所以我把他们俩? – Picm 2012-07-24 18:07:25

0

你有两个目标 - 所有的资源都包含在你正在构建的目标中吗?

+0

嗨那里我是新的Xcode抱歉你是什么意思的目标? – Picm 2012-07-24 21:02:33

+0

您的项目有两个目标,可能是“玩”和“玩测试”。如果您选择资源并打开右侧工具栏,并在“标识和类型”窗格下方查看,则应该有一个“目标成员资格”窗格。应该检查“玩”的框 – HoratioCain 2012-07-24 21:37:18

相关问题