简体   繁体   English

如何使用MonoTouch在iPhone上播放声音?

[英]How can I play a sound on the iPhone using MonoTouch?

I am looking for something like 我正在寻找类似的东西

PlaySound (uint frequency)

Does it exist? 是否存在?

From the HowTo at: http://wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert 从HowTo处: http : //wiki.monotouch.net/HowTo/Sound/Play_a_Sound_or_Alert

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

I don't know about mono, but in the iPhone SDK it isn't easy that easy to create and play sound. 我不了解单声道,但是在iPhone SDK中,创建和播放声音并不容易。 Other alternatives are to provide the sound as a file and play that, or create an array representing a sinusoid, and wrap it in a audio wrapper, and pass it to one of many sound APIs. 其他替代方法是将声音作为文件提供并播放,或者创建代表正弦曲线的数组,然后将其包装在音频包装器中,然后将其传递给许多声音API之一。

If mono proves to be just as limited, then search stackoverflow.com for System Sound Services and AVAudioPlayer as starting points. 如果证明单声道有限,那么请在stackoverflow.com上搜索System Sound Services和AVAudioPlayer作为起点。

Here are two ways to play a sound file: 这是播放声音文件的两种方法:

SoundEffect.c (based on Apple's) 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: 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

How to use it: 如何使用它:

// load the sound
    gameOverSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"buzz" ofType:@"caf"]];
// play the sound
    [gameOverSound playvibe];

This is useful for when you want to play sound at the same volume as the iPhone's volume control setting, and you won't need to stop or pause the sound. 当您想要以与iPhone的音量控制设置相同的音量播放声音,并且您无需停止或暂停声音时,此功能非常有用。

Another way is: 另一种方法是:

+ (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);
}

and use it (you can see all the extras when you go with AVAudioPlayer): 并使用它(使用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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM