应用程序运行时播放声音

时间:2012-07-24 17:23:44

标签: ios cocoa-touch avfoundation

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

2 个答案:

答案 0 :(得分: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。它将删除保留/释放/自动释放语句的需要。

答案 1 :(得分:0)

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