2012-11-23 66 views
0

我想在显示启动画面时添加声音,当它转到主屏幕时其他声音正在播放?我的问题是我应该在哪里指定用于生成声音的声音代码而闪屏来了?启动画面或启动画面中的声音

+0

你如何使用Default.png显示你的飞溅或为此做了单独的视图? – spider1983

+0

@ spider1983即时通讯使用Default.png – Fazil

回答

0

放入您的application didFinishLaunching,但一定要在.h和.m中实例化它。

像这样的东西应该做你的问题:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
    resourcePath = [resourcePath stringByAppendingString:@"/YOURMUSICNAME.mp3"]; 
    NSLog(@"Path to play: %@", resourcePath); 
    NSError* err; 

    //Initialize our player pointing to the path to our resource 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
       [NSURL fileURLWithPath:resourcePath] error:&err]; 

    if(err){ 
     //bail! 
     NSLog(@"Failed with reason: %@", [err localizedDescription]); 
    } 
    else{ 
     //set our delegate and begin playback 
     player.delegate = self; 
     [player play]; 
     player.numberOfLoops = -1; 
     player.currentTime = 0; 
     player.volume = 1.0; 
    } 
} 

然后,如果你想停止它:

[player stop]; 

或暂停:

[player pause]; 

,并导入它你的头文件:

#import <AVFoundation/AVFoundation.h> 

,并添加到您的头......

@interface ViewController : UIViewController <AVAudioPlayerDelegate> 
+0

我尝试过但是。只有在显示启动画面后才会发出声音 – Fazil

+0

@Fazil请解释它是否是你的主要问题... –

+0

我想播放声音,同时显示启动画面 – Fazil

0

我想你可以试试这个:

首先添加AVFoundation.Framework在您的项目

in AppDelegate.m 

- (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.navigate = [[UINavigationController alloc]initWithRootViewController:_viewController]; 
    self.window.rootViewController = self.navigate; 
    [self.window makeKeyAndVisible]; 

// Add this line to present splash; 
    [self.viewController displayScreen]; 

    return YES; 
} 

然后在你的rootViewController即你的第一个viewController

in .h 

添加这个工作框架

#import <AVFoundation/AVFoundation.h> 

AVAudioPlayer *avSound; //define this 

-(void)displayScreen; 
-(void)removeScreen; 

,然后在您的m添加这两种方法

-(void)displayScreen 
{ 
    UIView *splashView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease]; 
    splashView.backgroundColor = [UIColor colorWithPatternImage:[UIImage 
    imageNamed:@"Default.png"]]; 
    UIViewController *displayViewController=[[UIViewController alloc] init]; 
displayViewController.view = splashView; 
[self presentModalViewController:displayViewController animated:NO]; 

     NSURL *soundURL = [[NSBundle mainBundle] 
    URLForResource:@"soundFile"withExtension:@"mp3/caf or whatever"]; 
     avSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil]; 
    [avSound play];//play sound; 

    [self performSelector:@selector(removeScreen) withObject:nil afterDelay:4.0]; 


} 

-(void)removeScreen 
    { 
    [[self modalViewController] dismissModalViewControllerAnimated:YES]; 

    if ([avSound isPlaying]) 
     [avSound stop];//stop playing sound; 
    } 

然后在

-(void)dealloc 
{ 
     [super dealloc]; 
     [avSound release]; 
    } 

它的工作对我来说松开,希望帮助你也是。

+0

它将适用于iPhone4s我们已经改变了iPad和iPhone5的坐标。 – HDdeveloper