iOS Development outreach-Sound playback
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.M 3//14-Audio Play 4//5//Created by Apple on 14-8-8. 6//Copyright (c) 2014 Yangyong. All rights reserved. 7//8 9 #import "YYViewController.h" #import <avfoundation/avfoundation.h>11 @interface Yyviewcontroller () 13 @end15 @implementation YYViewController17-(void) VIEWDIDLOAD19 {[Super viewdidload];21}22-(void) TOU Chesbegan: (Nsset *) touches withevent: (uievent *) Event24 {25//1. Get the full path of the sound file. Nsurl *url=[[nsbundle Mainbund le]urlforresource:@ "Buyao.wav" withextension:nil];28 29//2. Load the sound file, create the sound effect ID (soundid, one ID corresponds to a sound file)-Systemsoundi D soundid=0;31 Audioservicescreatesystemsoundid ((__bridge cfurlref) URL, &soundid); 32 33//Pass the ID of the sound file that needs to be destroyed to It can destroy the//audioservicesdisposesystemsoundid (Soundid); 35 36//3. Play audio files 37//The two functions below can be used to play audio files, the first function with a vibration effect 3 8 Audioservicesplayalertsound (Soundid),//audioservicesplaysystemsound (< #SystemSoundID insystemsoundid#>) 40}41 @end
Note: Click on the screen to play the audio file.
iOS Development outreach-Sound playback