1. Controller code
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "RYAVTool.h"
@interface viewcontroller ()
@property(nonatomic,strong)nsmutabledictionary*allavid;
@end
@implementation Viewcontroller
-(void) viewdidload {
[Super viewdidload];
}
-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event
{
self. Allavid= [ryavtool playavwithname:@ "M_06.wav"];
}
memory warning when scavenging cache
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
[self. Allavid enumeratekeysandobjectsusingblock: ^ (ID key, ID obj, BOOL *stop) {
audioservicesdisposesystemsoundid([obj unsignedintvalue]);
}];
}
@end
2. Auxiliary Tool Class Code
#import <Foundation/Foundation.h>
@interface ryavtool: nsobject
+ (nsmutabledictionary*) Playavwithname: (nsstring*) name;
@end
#import "RYAVTool.h"
#import <AVFoundation/AVFoundation.h>
Static nsmutabledictionary *_allid;
Static systemsoundid _currentdata;
@implementation Ryavtool
+ (void) load
{
if (_allid= =Nil) {
_allid =[[nsmutabledictionary alloc]init];
}
}
+ (nsmutabledictionary*) Playavwithname: (nsstring*) name
{
if (!name) {
return _allid;
}
nsstring*path=[[nsbundle mainbundle]pathforresource: Name ofType: Nil];
if (!path) {
return _allid;
}
nsurl *url=[nsurl fileurlwithpath:p ath];
_currentdata=[_allid[name] unsignedintvalue];
if (! _currentdata) {
// Create audio playback files
Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &_currentdata);
// save
_allid[name]=@ (_currentdata);
}
// play
audioservicesplayalertsound(_currentdata);
return _allid;
}
@end
iOS programs play audio files