First, Brief introduction
In simple terms, audio can be divided into 2 types
(1) Sound effects
Also known as "short audio", usually plays in the program for 1-2 seconds
Enhance the overall user experience with embellishment in the app
(2) Music
For example, the "background music" in the game, the general playing time is longer
Frame: Play audio needs to use avfoundation.framework frame
Second, the sound effects of playback
1. Get the path to the sound file
Nsurl *url = [[NSBundle mainbundle] urlforresource:@ "M_03.wav" withextension:nil];
2. Load the sound file to get the corresponding sound ID
Systemsoundid soundid = 0;
Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &soundid);
3. Play sound
Audioservicesplaysystemsound (Soundid);
Note: The audio file only needs to be loaded 1 times
4. Summary of common functions of sound playback
Loading sound files
Audioservicescreatesystemsoundid (Cfurlref infileurl, Systemsoundid *outsystemsoundid)
Releasing sound resources
Audioservicesdisposesystemsoundid (Systemsoundid Insystemsoundid)
Play sound
Audioservicesplaysystemsound (Systemsoundid Insystemsoundid)
Play sound with a little vibration
Audioservicesplayalertsound (Systemsoundid Insystemsoundid)
Iii. Examples of programs
First, import frameworks that need to be relied upon
Import sound file footage that needs to be played
Description: Converting something in the avfoundation.framework frame to CF requires the use of bridging.
code example:
YYVIEWCONTROLLER.M file
1//2//YYVIEWCONTROLLER.M3//14-Sound playback4//5//Created by Liuchengli on 15-8-8.6//Copyright (c) 2015 All rights reserved.7//8 9#import "YYViewController.h"10#import <AVFoundation/AVFoundation.h>11 12@interface Yyviewcontroller ()13 14@end15 16@implementation Yyviewcontroller17 18-(void) VIEWDIDLOAD19 {[Super viewdidload];21}22 23-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) Event24 {25//1. Getting the full path of the sound fileNsurl *url=[[nsbundle mainbundle]urlforresource:@ "Buyao.wav" withextension:nil];28 29//2. Loading sound files, creating sound effects ID (soundid, one ID for one sound file)Systemsoundid soundid=0;31 Audioservicescreatesystemsoundid ((__bridge cfurlref) URL, &soundid); 32 33//Pass the ID of the sound file that needs to be destroyed to it to destroy34//audioservicesdisposesystemsoundid (soundid);35 36//3. Playing sound files37////Two functions below can be used to play sound files, the first function with a vibration effectAudioservicesplayalertsound (Soundid); 39//audioservicesplaysystemsound (< #SystemSoundID insystemsoundid#>)40}41 42@end
Note: Click on the screen to play the audio file.
iOS Development--audio--playback of sound effects