どのように私はMonoTouchでを使用してiPhoneのサウンドを再生することができますか?

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

  •  13-09-2019
  •  | 
  •  

質問

私は

のようなものを探しています
PlaySound (uint frequency)

それは存在するか?

役に立ちましたか?

解決

の手引きから: http://wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert

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

他のヒント

私はモノを知りませんが、iPhone SDKには簡単に作成し、サウンドを再生することは容易ではありません。他の代替は、ファイルとしてサウンドを提供して、それを再生する、または正弦波を表す配列を作成し、オーディオラッパーでラップし、多くのサウンドAPIのいずれかにそれを渡すことです。

は、モノが同じように制限されることが判明した場合、その出発点として、システムサウンドサービスとAVAudioPlayerのためstackoverflow.comを検索します。

ここでサウンドファイルを再生するには、2つの方法があります:

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