Monotouch를 사용하여 iPhone에서 사운드를 어떻게 재생할 수 있습니까?

StackOverflow https://stackoverflow.com/questions/1468393

  •  13-09-2019
  •  | 
  •  

문제

나는 같은 것을 찾고 있습니다

PlaySound (uint frequency)

존재합니까?

도움이 되었습니까?

해결책

Howto에서 : http://wiki.monotouch.net/howto/sound/play_a_sound_or_alert

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

다른 팁

나는 모노에 대해 잘 모르겠지만 iPhone SDK에서는 사운드를 만들고 재생하기가 쉽지 않습니다. 다른 대안은 사운드를 파일로 제공하고 재생하거나 정현파를 나타내는 배열을 만들고 오디오 래퍼에 싸서 많은 사운드 API 중 하나에 전달하는 것입니다.

Mono가 제한된 것으로 판명되면 Systact Sound Services에 대한 STACKOVERFLOW.COM 및 AVAUDIOPLAYER를 시작점으로 검색하십시오.

사운드 파일을 재생하는 두 가지 방법은 다음과 같습니다.

soundeffect.c (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

사용 방법:

// 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];    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top