0

编辑仍然不确定什么是错的请帮助

嗨,我正在创建 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

好的,这就是所有的代码

在此处输入图像描述

这就是我在应用程序加载并且声音正常时得到的结果

在此处输入图像描述

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

在此处输入图像描述

感谢先进

- (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;
}
4

2 回答 2

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。它将消除保留/释放/自动释放语句的需要。

于 2012-07-24T17:53:55.420 回答
0

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

于 2012-07-24T20:56:25.060 回答