2009-09-23 119 views

回答

8

从HOWTO章节:http://wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert

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

是否可以使用这种方法并仍然遵循手机的音量设置?在我的情况下,iPod touch的音量调低了一些,但sound.PlaySystemSound()以全音量播放... – Ender2050 2014-05-05 01:31:29

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