كيف يمكنني أن ألعب صوتا على iPhone باستخدام Monotouch؟

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

  •  13-09-2019
  •  | 
  •  

سؤال

أنا أبحث عن شيء مثل

PlaySound (uint frequency)

هل تتواجد؟

هل كانت مفيدة؟

المحلول

من Howto في: http://wiki.monotouch.net/howto/sound/play_a_sound_alert.

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

نصائح أخرى

لا أعرف عن أحادي، ولكن في iPhone SDK، ليس من السهل إنشاء الصوت ولعبه. البدائل الأخرى هي تزويد الصوت كملف وتشغيل ذلك، أو إنشاء مجموعة تمثل الجيوب الأنفية، ولفه في مجمع صوتي، وقم بتمريره إلى واحد من العديد من واجهات برمجة التطبيقات الصوتية.

إذا أثبتت أحادية أن تكون محدودة تماما، فابحث في 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