iOS Development outreach-Sound playback

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.