The sound effects are implemented through the Avfoundation framework, through functions rather than methods, and therefore need to be bridged and so on, in the following steps.
For sound playback, the first thing to get the sound of the URL ( only local audio ), and then converted to the sound ID (unique), through the ID play the sound.
"How to play sound"
① Import Framework Master header file
#import <AVFoundation/AVFoundation.h>
② through the bundle to get local sound, and then call the Audioservicescreatesystemsoundid function to get the sound effect id,id 0 means invalid, as a basis for lazy loading
@interface Viewcontroller () @property (nonatomic, assign) Systemsoundid Soundid; @end @implementation viewcontroller-( Systemsoundid) soundid{ if (!_soundid) { // Audioservicesplayalertsound (< #SystemSoundID insystemsoundid#>)//play + vibrate // Audioservicesplaysystemsound (< #SystemSoundID insystemsoundid#>)// Only play does not vibrate //first to create a sound ID, only play local sound, audio-related through C implementation, need bridging. Nsurl *url = [[NSBundle mainbundle] urlforresource:@ "Xxx.wav" withextension:nil]; Systemsoundid Soundid; Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &soundid); _soundid = Soundid; } return _soundid; }
③ call Audioservicesplaysystemsound incoming ID to play sound
Audioservicesplaysystemsound (Self.soundid);
"Extraction of Tool classes"
The tool class implements the pass-through file name auto-play sound effect through the class method, and contains lazy loading, and when a memory warning is received, it is also possible to invoke a method that deletes a single sound and all sound effects, storing the sound ID through a dictionary.
sgaudiotool.h// audio playback////Created by one on 7/28/15.// Copyright (c) soulghost. All rights reserved.//#import <foundation/foundation.h>/* iOS default playable audio format hardware decoding: AAC ALAC he-aac MP3 CAF (Hardware decoding supports only one audio at a time) software decoding: AAC ALAC iLBC IMA4 Linea PCM MP3 CAF u-law and A-law *///system comes with audio conversion//afconvert//to AAC afconvert-f adts-d AAC buyao.wav@interface sgaudiotool:nsobject+ (void) Playaudiowithname: (NSString *) audioname;+ (void) Disposeaudiowithaudioname: (NSString *) audioname;+ (void) Disposeall; @end
sgaudiotool.m//Audio playback////Created by one on 7/28/15.//Copyright (c) soulghost. All rights reserved.//#import "SGAudioTool.h" #import <AVFoundation/AVFoundation.h> @implementation Sgaudiotoolstatic nsmutabledictionary *_soundids;+ (void) initialize{//The Public initialization class method that is called when the class is first used. _soundids = [Nsmutabledictionary dictionary]; }+ (void) Playaudiowithname: (NSString *) audioname{//Determine if the file name is empty if (Audioname = nil) {NSLog (@ "file name cannot be empty"); Return } systemsoundid Soundid = [_soundids[audioname] unsignedintvalue]; Nil uintvalue=0, which is not recorded id=0 if (!soundid) {Nsurl *url = [[NSBundle mainbundle] Urlforresource:audioname wi Thextension:nil]; Determines if the URL is valid if (url = = nil) {NSLog (@ "File not Found"); Return } audioservicescreatesystemsoundid ((__bridge cfurlref) (URL), &soundid); _soundids[audioname] = @ (Soundid); } audioservicesplaysystemsound (Soundid); }+ (void) DisposeaudiowithaUdioname: (NSString *) audioname{if (audioname = = nil) {return; } systemsoundid Soundid = [_soundids[audioname] unsignedintvalue]; if (Soundid) {audioservicesdisposesystemsoundid (soundid); [_soundids Removeobjectforkey:audioname]; }}+ (void) disposeall{for (NSString *audioname in _soundids) {NSLog (@ "%@", audioname); Systemsoundid Soundid = [_soundids[audioname] unsignedintvalue]; Audioservicesdisposesystemsoundid (Soundid); } [_soundids removeallobjects]; } @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(95) How to play sound and how to make a tool