2012-07-24 97 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

相關問題