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

Posted

技术标签:

【中文标题】启动屏幕或启动屏幕中的声音【英文标题】:Sound in splash screen or launch screen 【发布时间】:2012-11-23 11:54:13 【问题描述】:

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

【问题讨论】:

你是如何使用 Default.png 来展示你的启动画面的,还是已经为此制作了单独的视图??? @spider1983 我正在使用 Default.png 【参考方案1】:

我猜你可以试试这个:

首先在你的项目中添加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];
  

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

【讨论】:

它适用于 iPhone4s 我们已经更改了 iPad 和 iPhone5 的坐标。【参考方案2】:

输入您的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>

【讨论】:

我试过你的方法,但是。只有在显示启动画面后才发出声音

以上是关于启动屏幕或启动屏幕中的声音的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的屏幕录像无声音或者有杂音

在 KivyMD Python 中更改开始屏幕时,如何在启动时显示或关闭消息?

iOS 13 中的暗模式启动屏幕

Xcode 6 自定义启动屏幕

启动屏幕组件

iOS 启动屏幕故事板与启动图像