简体   繁体   English

iOS:如何方便地存储和播放许多mp3文件

[英]iOS: how to conveniently store and play many mp3 files

I have a program with about 2000 short mp3 files. 我有一个大约2000个短mp3文件的程序。 I am now storing all those file into folder Supporting Files and when I want to play I call this function: 我现在将所有这些文件存储到Supporting Files夹中,当我想播放时我调用此函数:

-(void)playSound:(NSString *)mySoundFileName{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:mySoundFileName ofType:@"mp3"];  
    if ([NSData dataWithContentsOfFile:filePath]) {  
        url = [NSURL fileURLWithPath:filePath];
        audioPlayer = [[AVAudioPlayer alloc]
                           initWithContentsOfURL:url
                           error:nil];
        [audioPlayer play];  
    } 
}

However, the first time I play the sound, it always takes long time to search/load the file. 但是,第一次播放声音时,搜索/加载文件总是需要很长时间。 More specifically, after pressing "play sound" button to play sound, I have to wait for at least 5 seconds until it plays. 更具体地说,在按下“播放声音”按钮播放声音后,我必须等待至少5秒才能播放。 It is OK to play other sound after that, ie, it play almost immediately when I press "play sound" button. 之后可以播放其他声音,也就是说,当我按下“播放声音”按钮时,它几乎立即播放。 Do you have any suggestion to store and play those many files more efficiently? 您是否有任何建议可以更有效地存储和播放这些文件? Thank you very much 非常感谢你

It can sometimes take an undesirable amount of time for AVAudioPlayer to start playing initially. AVAudioPlayer最初可能需要花费不合需要的时间才能开始播放。 A good way to solve this is to make the initial alloc/init before you call play. 解决这个问题的一个好方法是在调用play之前创建初始alloc / init。 This way the player is ready to play before the user presses the play button. 这样,在用户按下播放按钮之前,播放器已准备好播放。 Additionally, calling [player prepareToPlay]; 另外,调用[player prepareToPlay]; before play will help improve performance slightly. 在比赛之前将有助于略微提高表现。

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

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