简体   繁体   English

初始屏幕或启动屏幕中的声音

[英]Sound in splash screen or launch screen

我想在显示初始屏幕时添加声音,当转向主屏幕时正在播放其他声音吗?我的问题是,当初始屏幕出现时,我应该在哪里指定用于产生声音的声音代码?

Put in your application didFinishLaunching but be sure to instantiate it in you .h and .m. 放入application didFinishLaunching但请确保在您的.h和.m中实例化它。

Something like this should do your problem: 这样的事情应该可以解决您的问题:

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

Then if you want to stop it: 然后,如果您想停止它:

[player stop];

or pause it : 或暂停它:

[player pause];

and also import it in your header file: 并将其导入您的头文件中:

#import <AVFoundation/AVFoundation.h>

and add this to your header ...... 并将其添加到标题中...

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

i Guess you can try this : 我猜你可以试试看:

First of all Add AVFoundation.Framework in your project 首先在您的项目中添加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;
 }

Then in your rootViewController ie your first viewController 然后在您的rootViewController中,即您的第一个viewController

in .h

Add this Frame work 添加此框架作品

#import <AVFoundation/AVFoundation.h>

 AVAudioPlayer *avSound; //define this

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

and then add these two methods in your .m 然后在您的.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;
  }

then release it in 然后放进去

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

Its working for me,hope helps you too. 它对我有用,希望对您也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM