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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28985942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:48:50  来源:igfitidea点击:

Best method to play an short audio sound on iOS 8.2

objective-ciphonexcodeaudioios8.2

提问by Kyle

What is the best way to play an audio on iOS 8.2?

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

回答by Rob Sanders

The easiest way is to import the AudioToolbox framework like so #import <AudioToolbox/AudioToolbox.h>then all you need to do is this:

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

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

This is best for really short sounds like beeps e.t.c because you wont have much control over the sounds like you would with AVAudioPlayer.

这最适合非常短的声音,如哔哔声等,因为您不会像使用AVAudioPlayer.

回答by Daij-Djan

Use AVAudio - more to code that using the AudioToolbox but it is also more flexible (if you need it in the future)

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

0.

0.

#import <AVFoundation/AVFoundation.h>

1.

1.

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

2.

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.

3.

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

回答by iOS Lifee

#Import Avfoundation.Framework

AVAudioPlayer *_audioPlayer;  //Declare Globally

Nsstring *path =[Nsstring stringwithformat:@"%@/Beep.mp3",[[Nsbundle mainBundle] resourcePath]];         //Give Your Local Path

Nsurl *soundUrl= [Nsurl FileUrlWithPath:path];

_audioPlayer=[[AvAudioPlayer alloc] initwithContents ofurl:soundUrl error:nil];

_audioPlayer.volume=1.0;  //Give Volume

_audioPlayer.numberOfLoops=loop;  //Give number of loop for repeating sound

_audioPlayer.enableRate = Yes;

_audioPlayer.rate=2.0f;  //Give for fast playing sound. default value is 1.0f;

[_audioPlayer Play];