Come posso riprodurre un suono su iPhone utilizzando MonoTouch?
-
13-09-2019 - |
Domanda
Sto cercando qualcosa di simile
PlaySound (uint frequency)
Esiste?
Soluzione
Dal HowTo all'indirizzo: http://wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert
var sound = SystemSound.FromFile (new NSUrl ("File.caf"));
sound.PlaySystemSound ();
Altri suggerimenti
Non so su mono, ma in iPhone SDK non è facile così facile da creare e riprodurre il suono. Altre alternative sono di fornire il suono come un file e riprodurre quello, o creare una matrice che rappresenta una sinusoide, e avvolgerlo in un involucro audio, e passarlo a uno dei tanti API audio.
Se mono dimostra di essere altrettanto limitata, quindi cercare stackoverflow.com per System Servizi del suono e AVAudioPlayer come punto di partenza.
Qui ci sono due modi di giocare un file audio:
SoundEffect.c (sulla base di Apple)
#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
Come si usa:
// load the sound
gameOverSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"buzz" ofType:@"caf"]];
// play the sound
[gameOverSound playvibe];
Questo è utile quando si desidera riprodurre il suono allo stesso volume di impostazione del controllo del volume di iPhone, e non sarà necessario interrompere o mettere in pausa il suono.
Un altro modo è:
+ (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);
}
e utilizzarlo (potete vedere tutti gli extra quando si va con 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];