1 //2 //YSvoiceTool.h3 //Audio4 //5 //Created by Ys on 15/12/18.6 //Copyright (c) 2015 Ys. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten One @interfaceYsvoicetool:nsobject A - /** - * Play Sound the * - * @param filename - */ -+(void) Playsoundwithname: (NSString *) FileName; + - + /** A * Destroy Sound at * - * @param filename - */ -+(void) Disposesoundwithname: (NSString *) FileName; - - @end
1 //2 //YSVOICETOOL.M3 //Audio4 //5 //Created by Ys on 15/12/18.6 //Copyright (c) 2015 Ys. All rights reserved.7 //8 9 #import "YSvoiceTool.h"Ten #import<AVFoundation/AVFoundation.h> One A @implementationYsvoicetool - - //dictionary: FileName as key, soundid as value the //Store all audio IDs - StaticNsmutabledictionary *_soundsdict; - -+(void) Initialize + { -_soundsdict =[Nsmutabledictionary dictionary]; + } A at+(void) Playsoundwithname: (NSString *) FileName - { - if(!fileName) { - return; - } - //1. Remove the Soundid from the dictionary inSystemsoundid Soundid =[_soundsdict[filename] unsignedlongvalue]; - if(!soundid) {//Create to //Loading sound files +Nsurl *url =[Nsurl urlwithstring:[[nsbundle mainbundle]pathforresource:filename Oftype:nil]; - if(!URL) { the return; * } $ //Create a sound IDPanax NotoginsengAudioservicescreatesystemsoundid (__bridge cfurlref) URL, &soundid); - //put in a dictionary the_soundsdict[filename] =@ (soundid); + } A //2. Play the Audioservicesplaysystemsound (soundid); + - } $ $ -+(void) Disposesoundwithname: (NSString *) FileName - { the if(!fileName) { - return;Wuyi } theSystemsoundid Soundid =[_soundsdict[filename] unsignedlongvalue]; - if(soundid) { Wu //Destroy Sound ID - Audioservicesdisposesystemsoundid (soundid); About //Remove from dictionary $ [_soundsdict removeobjectforkey:filename]; - } - - } A + @end
Packaging methods for tool classes 1