Frage

Ich bin auf der Suche nach so etwas wie

PlaySound (uint frequency)

Gibt es sie?

War es hilfreich?

Lösung

Aus dem HowTo an: http://wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert

var sound = SystemSound.FromFile (new NSUrl ("File.caf"));  
sound.PlaySystemSound (); 

Andere Tipps

Ich weiß nicht, über Mono, aber im iPhone SDK ist es nicht einfach, so einfach Sound zu kreieren und zu spielen. Andere Alternativen sind den Ton als Datei zur Verfügung zu stellen und das spielen, oder ein Array erstellen Sinuskurve darstellt, und es in einem Audio-Wrapper wickeln, und gibt sie an eine von vielen Sound-APIs.

Wenn Mono nur als begrenzt erweisen, dann sucht stackoverflow.com für System Sound Service und AVAudioPlayer als Ausgangspunkte.

Hier sind zwei Möglichkeiten, um eine Audiodatei zu spielen:

SoundEffect.c (basierend auf Apples)

#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

Wie man es verwendet:

// load the sound
    gameOverSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"buzz" ofType:@"caf"]];
// play the sound
    [gameOverSound playvibe];

Dies ist nützlich, wenn Sie den Ton wollen mit der gleichen Lautstärke wie die iPhone-Lautstärkeeinstellung spielen, und Sie werden den Ton nicht stoppen müssen oder pausieren.

Eine andere Möglichkeit ist:

+ (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);
}

und verwenden Sie es (Sie können alle Extras sehen, wenn Sie mit AVAudioPlayer gehen):

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];    
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top