iphone - 更有效的代码? (AVAudioPlayer)

标签 iphone ios performance avaudioplayer

在下面的电话中,我准备为我的应用程序中的四种不同声音准备一个AVAudioPlayer。如果我不这样做,它会导致应用程序延迟约1.5秒,因为我只是在需要听到它的地方这样做。因此,有什么方法可以使此代码看起来更好或更有效地工作,因为现在我已经在App-Delegate中准备好了这些声音,这会稍微降低启动速度。所以这是代码,让我知道是否有什么方法可以使下面的代码更好:

- (void)readySounds {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"TrackUno" ofType:@"mp3"];
        NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
        AVAudioPlayer *one = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
        [file release];
        self.tr1 = one;
        [one release];
        [tr1 prepareToPlay];
        [tr1 setDelegate:self];

        NSString *path2 = [[NSBundle mainBundle] pathForResource:@"TrackDos" ofType:@"mp3"];
        NSURL *file2 = [[NSURL alloc] initFileURLWithPath:path2];
        AVAudioPlayer *two = [[AVAudioPlayer alloc] initWithContentsOfURL:file2 error:nil];
        [file2 release];
        self.tr2 = two;
        [two release];
        [tr2 prepareToPlay];
        [tr2 setDelegate:self];

        NSString *path3 = [[NSBundle mainBundle] pathForResource:@"Click" ofType:@"mp3"];
        NSURL *file3 = [[NSURL alloc] initFileURLWithPath:path3];
        AVAudioPlayer *three = [[AVAudioPlayer alloc] initWithContentsOfURL:file3 error:nil];
        [file3 release];
        self.clk = three;
        [three release];
        [clk prepareToPlay];
        [clk setDelegate:self];

        NSString *path4 = [[NSBundle mainBundle] pathForResource:@"HSSound" ofType:@"mp3"];
        NSURL *file4 = [[NSURL alloc] initFileURLWithPath:path4];
        AVAudioPlayer *four = [[AVAudioPlayer alloc] initWithContentsOfURL:file4 error:nil];
        [file4 release];
        self.hs = four;
        [four release];
        [hs prepareToPlay];
        [hs setDelegate:self];
    }

最佳答案

如果您只关心初始化时间,那么您可以*在辅助线程上创建它,并改善感知的启动时间。然后它将初始化流和解码器,并从辅助线程从磁盘打开音频文件。

如果回放,流计数,内存使用,CPU,加载时间,解码,过滤,重采样等仍然是因素:您使用的是最高级别的API,则可以降低几个级别,并拥有对每个级别的所有控制权以及所有这些方面。

您可以通过重构使其看起来更好-该方法执行四次相似的序列。

*“可能”,因为我只使用了较低级别的API,所以我不知道AVAudioPlayer是否具有线程约束。

重构示例,其中添加了错误检查功能:

没有编译,但是您知道了:

- (AVAudioPlayer *)newAudioPlayerForFile:(NSString *)fileName extension:(NSString *)extension inBundle:(NSBundle *)bundle
{
  assert(fileName && extension && bundle);
  NSURL * url = [bundle URLForResource:fileName ofType:extension];
  if (nil == url) {
    assert(0 && "could not locate resource");
    return nil;
  }

  NSError * error = 0;
  AVAudioPlayer * spieler = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
  assert(spieler);

  if (nil != error) {
    NSLog(@"Error encountered creating audio player for file (%@): %@", fileName, error);
    [spieler release], spieler = nil;
    return nil;
  }

  [spieler prepareToPlay];
  [spieler setDelegate:self];

  return spieler;
}

- (void)readySounds
{
  NSAutoreleasePool * pool = [NSAutoreleasePool new];
  NSBundle * mainBundle = [NSBundle mainBundle];
  self.tr1 = [[self newAudioPlayerForFile:@"TrackUno" extension:@"mp3" inBundle:mainBundle] autorelease];
  self.tr2 = [[self newAudioPlayerForFile:@"TrackDos" extension:@"mp3" inBundle:mainBundle] autorelease];
  self.clk = [[self newAudioPlayerForFile:@"Click" extension:@"mp3" inBundle:mainBundle] autorelease];
  self.hs = [[self newAudioPlayerForFile:@"HSSound" extension:@"mp3" inBundle:mainBundle] autorelease];
  [pool release], pool = nil;
}

关于iphone - 更有效的代码? (AVAudioPlayer),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7780025/

相关文章:

iPhone代码-在UIButton中使用图像?

iPhone 5.1模拟器(视网膜)看起来很大

iphone - UIScrollView:缩放时禁用滚动

ios - MultipeerConnectivity MCPeerID 不符合 Codable

java - 在具有许多 if 语句的 for 循环中使用 continue 时是否会提高性能?

java - 当每个语句返回某些内容时,使用多个 if 语句而不是 else if

iphone - 这个 Obj-C "unrecognized selector sent to instance"异常表示什么?

ios - swift : Background DispatchQueue thread is too slow

objective-c - iOS 错误 : Assigning to NSMutableString from NSArray object (type 'void' )?

performance - sbt 高 CPU 使用率与 ~run