内存管理 - 加载音效 - 可可X $ C $Ç [英] Memory Management - Loading Sound Effects - Cocoa Xcode

查看:123
本文介绍了内存管理 - 加载音效 - 可可X $ C $Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,它已经引起了我的注意,我应该优化加载和使用中,我针对iOS开发的小游戏声音的方式。

So it has been brought to my attention that I should optimise the way I load and use sounds in a small game that I am developing for iOS.

我打开一个嘣的声音和我每次点击屏幕,使精灵跳跃(比如马里奥)播放。
我希望能够每次播放声音,即使声音已经由previous跳打...

I load a "boing" sound and play it every time I tap the screen, making the sprite jump (like mario). I want to be able to play the sound every time, even if the sound is already playing from the previous jump...

下面是我目前使用的2种方式:

Below are the 2 ways I'm currently using:

1日实施:

   //load the music from file
   -(void)LoadMusic{
          jumpSound = [[NSBundle mainBundle] pathForResource:@"boing" ofType:@"mp3"];
   }

   //call in viewDidLoad
   - (void)viewDidLoad{
          [self LoadMusic];
          ...
          [super viewDidLoad];
   }

   //play sound when called
   -(void)playSound{
          jumpAffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
          [jumpAffect play];
   }

   //tap/touch to jump (& play sound)
   -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
          [self playSound];
          jumpUp = 16;
   }

第二次执行:,除了我通过到下一个(所以同样的声音影响甚至可以称为5次,循环加载相同的文件,这是类似的,如果它已经在previous会话)。

2nd implementation:, this is similar except I load the same file 5 times, and loop through to the next one (so the same sound affect can be called even if it's already in previous session).

   int soundStage = 1;

   //load the music from file
   -(void)LoadMusic{
           jumpSound = [[NSBundle mainBundle] pathForResource:@"Boing" ofType:@"mp3"];

           jumpAffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
           jumpAffect.delegate = self;

           jumpAffect2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
           jumpAffect2.delegate = self;

           jumpAffect3 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
           jumpAffect3.delegate = self;

           jumpAffect4 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
           jumpAffect4.delegate = self;

           jumpAffect5 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
           jumpAffect5.delegate = self;
   }

   //call in viewDidLoad
   - (void)viewDidLoad{
          [self LoadMusic];
          ...
          [super viewDidLoad];
   }

  //tap/touch to jump (& play sound)
   -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{

          jumpUp = 16;

         if(soundStage == 1){
             [jumpAffect play];
             soundStage = 2;
         }
         else if(soundStage == 2){
             [jumpAffect2 play];
             soundStage = 3;
         }
         else if(soundStage == 3){
             [jumpAffect3 play];
             soundStage = 4;
         }
         else if(soundStage == 4){
             [jumpAffect4 play];
             soundStage = 5;
         }
         else if(soundStage == 5){
             [jumpAffect5 play];
             soundStage = 1;
    }

我不知道这为什么是更好的办法?我希望能避免内存泄漏和只是能当屏幕被窃听,不断有相同的声音播放优化。谢谢你。

I'm wondering which why is the better way? I'm hoping to avoid memory leaks and just optimise it by being able to continuously have the same sound play when the screen is tapped. Thanks.

推荐答案

AVAudioPlayer同时播放多个声音,每个音频播放器一个声音。所以,更好的是装载/播放快捷声音的touchesBegan方法中不保持基准值:

AVAudioPlayer plays multiple sounds simultaneously, one sound per audio player. So better is to load/play your quick sound inside the touchesBegan method without keeping the reference:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
    NSString *jumpSound = [[NSBundle mainBundle] pathForResource:@"boing" ofType:@"mp3"];
    AVAudioPlayer *jumpAffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
    [jumpAffect prepareToPlay];
    [jumpAffect play];
    jumpUp = 16;
}

这篇关于内存管理 - 加载音效 - 可可X $ C $Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆