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
確定,所以就是所有的代碼
和繼承人我所得到的,當應用程序加載時,聲音正常工作
,這就是我想要的(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;
}
這與聲音沒有任何關係。可能是您初始化ViewController的問題。你可以發佈該代碼嗎? (ViewController.m) – Dima 2012-07-24 17:44:00
此刻我在視圖controller.m或.h中沒有代碼 – Picm 2012-07-24 17:51:44
根本沒有代碼?只是一個空白的文件?必須有一些東西在裏面。 – Dima 2012-07-24 18:12:42