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

2つのターゲットがあります-構築しているターゲットにすべてのリソースが含まれていますか?

于 2012-07-24T20:56:25.060 に答える