2009-09-23 7 views

Répondre

8

De l'HowTo à: http://wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert

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

Est-il possible d'utiliser cette approche tout en respectant les paramètres de volume du téléphone? Dans mon cas, un iPod touch a du volume baissé mais sound.PlaySystemSound() joue à plein volume ... – Ender2050

3

Je ne sais pas sur le mono, mais dans le SDK de l'iPhone, il n'est pas facile de créer et de jouer du son. D'autres alternatives sont de fournir le son en tant que fichier et de le lire, ou de créer un tableau représentant une sinusoïde, et de l'inclure dans un wrapper audio, et de le passer à l'une des nombreuses API audio.

Si le mode mono est tout aussi limité, recherchez stackoverflow.com pour System Sound Services et AVAudioPlayer comme points de départ.

Voici deux façons de jouer un fichier audio:

SoundEffect.c (basé sur 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 

Comment l'utiliser:

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

Ceci est utile lorsque vous souhaitez lire le son au même volume que le réglage du volume de l'iPhone et que vous n'avez pas besoin d'arrêter ou de mettre le son en pause.

Une autre façon est:

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

et de l'utiliser (vous pouvez voir tous les extras quand vous allez avec 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

Comment lisez-vous un fichier son? –

Questions connexes