1
A
回答
8
从HOWTO章节:http://wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert
var sound = SystemSound.FromFile (new NSUrl ("File.caf"));
sound.PlaySystemSound();
3
我不知道单声道,但在iPhone SDK中,创建和播放声音并不容易。其他选择是将声音作为文件提供并播放,或者创建表示正弦曲线的数组,并将其包装在音频包装中,并将其传递给许多声音API中的一个。
如果单声道被证明是有限的,那么搜索stackoverflow.com作为系统声音服务和AVAudioPlayer作为起点。
以下是播放声音文件有两种方式:
SoundEffect.c(基于苹果)
#import "SoundEffect.h"
@implementation SoundEffect
+ (id)soundEffectWithContentsOfFile:(NSString *)aPath {
if (aPath) {
return [[[SoundEffect alloc] initWithContentsOfFile:aPath] autorelease];
}
return nil;
}
- (id)initWithContentsOfFile:(NSString *)path {
self = [super init];
if (self != nil) {
NSURL *aFileURL = [NSURL fileURLWithPath:path isDirectory:NO];
if (aFileURL != nil) {
SystemSoundID aSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
if (error == kAudioServicesNoError) { // success
_soundID = aSoundID;
} else {
NSLog(@"Error %d loading sound at path: %@", error, path);
[self release], self = nil;
}
} else {
NSLog(@"NSURL is nil for path: %@", path);
[self release], self = nil;
}
}
return self;
}
-(void)dealloc {
AudioServicesDisposeSystemSoundID(_soundID);
NSLog(@"Releasing in SoundEffect");
[super dealloc];
// self = nil;
}
-(void)play {
AudioServicesPlaySystemSound(_soundID);
}
-(void)playvibe {
AudioServicesPlayAlertSound(_soundID);
}
+(void)justvibe {
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
@end
SoundEffect.h:
#import <AudioToolbox/AudioServices.h>
@interface SoundEffect : NSObject {
SystemSoundID _soundID;
}
+ (id)soundEffectWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfFile:(NSString *)path;
- (void)play;
- (void)playvibe;
+ (void)justvibe;
@end
如何使用它:
// load the sound
gameOverSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"buzz" ofType:@"caf"]];
// play the sound
[gameOverSound playvibe];
这个当您想要以与iPhone的音量控制设置相同的音量播放声音时非常有用,并且您不需要停止或暂停声音。
另一种方法是:
+ (AVAudioPlayer *) newSoundWithName: (NSString *) name;
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: name ofType: @"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
// if the sound is large and you need to preload it:
[newPlayer prepareToPlay];
return (newPlayer);
}
,并用它(你可以看到所有的演员,当你与AVAudioPlayer去):
timePassingSound = [AVAudioPlayer newSoundWithName:@"ClockTicking"];
[timePassingSound play];
// change the volume
[timePassingSound volume:0.5];
// pause to keep it at the same place in the sound
[timePassingSound pause];
// stop to stop completely, and go to beginning
[timePassingSound stop];
// check to see if sound is still playing
[timePassingSound isPlaying];
+1
如何播放声音文件? – 2009-09-23 22:37:25
相关问题
- 1. MonoTouch:播放声音
- 2. 如何中断正在iPhone上播放的声音(SDK或MonoTouch)?
- 3. 用Monotouch播放声音
- 4. iPhone:如何在iPod上播放声音?
- 5. 如何在iPhone上使用OpenAL播放循环声音
- 6. 在iPhone上播放内置声音
- 7. 尝试在iPhone上播放声音
- 8. 问题播放声音的iPad设备上使用MonoTouch的
- 9. 无法在iPhone上使用JavaScript播放声音,但可以在Android上播放
- 10. iPhone SDK:使用coreAudio播放声音
- 11. Monotouch声音无法正常播放
- 12. 如何播放iPhone轻敲声音?
- 13. 播放声音问题iPhone?
- 14. 在iPad上播放声音,使用AudioToolbox
- 15. 使用JavaScript在mouseover上播放声音
- 16. 如何使用MonoTouch在iPhone上播放视频?
- 17. QR码在iPhone上播放视频但不播放声音
- 18. 在Android上播放声音
- 19. 使用Monotouch在iPhone中播放MJPEG流
- 20. 使用雀歌在iPhone上播放声音
- 21. 使用AudioServices在iPhone上停止播放声音样本
- 22. 使用系统声音播放声音
- 23. 如何在iPhone上录制和播放声音?
- 24. 如何在iPhone上播放铃声
- 25. 在iPhone上播放音频
- 26. 如何使用Qt播放声音
- 27. 如何使用声音播放视频
- 28. 如何使用java播放声音
- 29. 使用JavaScript播放声音
- 30. 播放声音不适用于iPhone
是否可以使用这种方法并仍然遵循手机的音量设置?在我的情况下,iPod touch的音量调低了一些,但sound.PlaySystemSound()以全音量播放... – Ender2050 2014-05-05 01:31:29