在启动画面或启动画面中发出声音

时间:2012-11-23 11:54:14

标签: iphone ios avaudioplayer

我想在显示启动画面的同时添加声音,当它转到主屏幕时正在播放其他声音?我的问题是,在启动画面时,我应该在哪里指定产生声音的声音代码?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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];
  }

它为我工作,希望也帮助你。