在iOS 8.2上播放短音频声音的最佳方法

时间:2015-03-11 11:52:41

标签: objective-c iphone xcode audio ios8.2

在iOS 8.2上播放音频的最佳方法是什么?

3 个答案:

答案 0 :(得分:18)

最简单的方法是像#import <AudioToolbox/AudioToolbox.h>那样导入AudioToolbox框架,然后你需要做的就是:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Beep" ofType:@"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);

这对于像beeps e.t.c这样的非常短的声音来说是最好的,因为你不会像对AVAudioPlayer一样控制声音。

答案 1 :(得分:10)

使用AVAudio - 使用AudioToolbox进行更多编码,但也更灵活(如果将来需要它)

0

#import <AVFoundation/AVFoundation.h>

1

//conform to delegate and make a property
@interface ViewController () <AVAudioPlayerDelegate>
@property (nonatomic, strong) AVAudioPlayer *audioplayer; //the player
@end

2

//have a lazy property for the player! where you also tell it to load the sound
#define YourSound @"sound.caf"
- (AVAudioPlayer *)audioplayer {
    if(!_audioplayer) {
        NSURL *audioURL = [[NSBundle mainBundle] URLForResource:YourSound.stringByDeletingPathExtension withExtension:YourSound.pathExtension];
        NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
        NSError *error = nil;
        // assing the audioplayer to a property so ARC won't release it immediately
        _audioplayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
        _audioplayer.delegate = self;
    }
    return _audioplayer;
}

3

//play
- (void)action {
    [self.audioplayer play];
}

答案 2 :(得分:2)

<script src="//harvesthq.github.io/chosen/chosen.jquery.js"></script>
<link href="//harvesthq.github.io/chosen/chosen.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://harvesthq.github.io/chosen/chosen.jquery.js"></script>
<div class="box" style="margin-left: 50px; margin-top: 150px;">
    <select class="chosen-select" style="width: 150px">
        <option>vijay</option>
        <option>sunny</option>
        <option>planet</option>
       <option>airfill</option>
    </select>
</div>
<script>
  $(function(){
    $(".chosen-select").chosen(); 
});
</script>