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.M3 //14-Sound playback4 //5 //Created by Apple on 14-8-8.6 //Copyright (c) 2014 Yangyong. All rights reserved.7 //8 9 #import "YYViewController.h"Ten #import<AVFoundation/AVFoundation.h> One A @interfaceYyviewcontroller () - - @end the - @implementationYyviewcontroller - -- (void) Viewdidload + { - [Super Viewdidload]; + } A at-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { - //1. Get the full path of the sound file - -Nsurl *url=[[nsbundle Mainbundle]urlforresource:@"Buyao.wav"Withextension:nil]; - in //2. Load the sound file, create the sound effect ID (soundid, an ID corresponding to a sound file) -Systemsoundid soundid=0; toAudioservicescreatesystemsoundid (__bridge cfurlref) URL, &soundid); + - //Pass the ID of the sound file that needs to be destroyed to it to destroy the //Audioservicesdisposesystemsoundid (soundid); * $ //3. Play sound filesPanax Notoginseng //the following two functions can be used to play a sound file, the first function with a vibration effect - Audioservicesplayalertsound (soundid); the //Audioservicesplaysystemsound (< #SystemSoundID insystemsoundid#>) + } A the @end
Note : Click on the screen to play the audio file.