本文介绍了在第一次玩iPhone AVAudioPlayer应用程序冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是AVAudioPlayer从iOS版SDK用于在的tableView行每次点击打短的声音。
我有一个会调用方法playSound每排按钮手动进行@selector:(ID)接收器{}。从接收我得到完善的URL,这样我可以发挥它。

I'm using AVAudioPlayer from iOS SDK for playing short sounds on each click in tableView rows.I have manually made @selector on button in each row that fires up method playSound:(id)receiver {}. From receiver I get sound url so I can play it up.

这个方法看起来像:

- (void)playSound:(id)sender {
    [audioPlayer prepareToPlay];
    UIButton *audioButton = (UIButton *)sender;
    [audioButton setImage:[UIImage imageNamed:@"sound_preview.png"] forState:UIControlStateNormal];
    NSString *soundUrl = [[listOfItems objectForKey:[NSString stringWithFormat:@"%i",currentPlayingIndex]] objectForKey:@"sound_url"];

    //here I get mp3 file from http url via NSRequest in NSData
    NSData *soundData = [sharedAppSettingsController getSoundUrl:defaultDictionaryID uri:soundUrl];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:&error];
    audioPlayer.numberOfLoops = 0;
    if (error) {
        NSLog(@"Error: %@",[error description]);
    }
    else {
        audioPlayer.delegate = self;
        [audioPlayer play];
    }
}

一切正常,除了一些声音的第一出戏的罚款。该应用程序冻结约2秒钟,比播放声音。第二和所有其他声音播放点击声音按钮后,作品恰到好处。

Everything works fine except for the first play of some sound. The application freezes for about 2 seconds and than sound is played. Second and every other sound play works just right after click on sound button.

我不知道为什么有就是大约2秒第一次玩时冻结应用程序启动?

I wonder why there is that about 2 seconds freeze on first play when application starts?

如果你认为我拿错解,请告诉我那个合适的人......

If you think that I take wrong solution please tell me the right one...

问候

推荐答案

从您的code片段, audioPlayer 必须是伊娃,对吧?

From your code snippet, audioPlayer must be an ivar, right?

在方法的顶部,可以调用 - prepareToPlay 在现有的 audioPlayer 实例(可以是零,至少在第一轮)。

At the top of the method, you invoke -prepareToPlay on the existing audioPlayer instance (which could be nil, at least on the first pass).

在后来的方法,更换一个全新的AVAudioPlayer实例,这个现有的音频播放器。在previous - prepareToPlay 被浪费。而你泄漏内存每个新AVAudioPlayer。

Later in the method, you replace this existing audio player with a brand new AVAudioPlayer instance. The previous -prepareToPlay is wasted. And you're leaking memory with each new AVAudioPlayer.

相反缓存声音数据或URL,我会尝试创建AVAudioPlayer对象,每个声音的缓存。在你的 -playSound:方法,得到表行到合适的音频播放器的引用, - 播放

Instead of caching sound data or URLs, I would try creating a cache of AVAudioPlayer objects, one for each sound. In your -playSound: method, get a reference to the appropriate audio player for the table row, and -play.

您可以使用 -tableView:的cellForRowAtIndexPath:作为适当点,以获得AVAudioPlayer实例,该行,也许懒洋洋地创建实例和缓存它有作为

You can use -tableView:cellForRowAtIndexPath: as the appropriate point to get the AVAudioPlayer instance for that row, maybe lazily creating the instance and caching it there as well.

您可以尝试 -tableView:willDisplayCell:forRowAtIndexPath:因为在那里你会调用点 - prepareToPlay 该行的AVAudioPlayer实例。

You can try -tableView:willDisplayCell:forRowAtIndexPath: as the point where you would invoke -prepareToPlay on the row's AVAudioPlayer instance.

或者你可以做prepare在 -tableView:的cellForRowAtIndexPath:。实验,看看哪个效果最好。

Or you could just do the prepare in -tableView:cellForRowAtIndexPath:. Experiment and see which works best.

这篇关于在第一次玩iPhone AVAudioPlayer应用程序冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:57