2009-09-23 76 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