2009-09-23 4 views

답변

8

하우투에서에서 : 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

3

모노에 대해서는 잘 모르지만 iPhone SDK에서는 사운드를 쉽게 만들고 재생하는 것이 쉽지 않습니다. 다른 대안으로는 사운드를 파일로 제공하여 재생하거나 사인 곡선을 나타내는 배열을 만들어 오디오 래퍼에 넣고 여러 사운드 API 중 하나로 전달하는 것입니다.

모노가 제한적일 뿐이라면, System Sound Services 및 AVAudioPlayer의 stackoverflow.com을 시작점으로 검색하십시오.

는 여기에 사운드 파일을 재생하는 방법은 두 가지입니다 : (애플 기준)

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

어떻게 사운드 파일을 재생하나요? –

관련 문제