0

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

4

2 回答 2

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>
于 2012-11-23T12:01:01.160 回答
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];
  }

它对我有用,希望对你也有帮助。

于 2012-11-23T12:46:49.213 回答