The Sound of Multimedia

Source: Internet
Author: User
Tags file url

QQ Chat There will be a message when the sound, and now use Audiotoolbox to achieve the effect of the beep.

Note:Some versions of Xcode can not play with audiotoolbox sound may be some bugs, I test for a long time, on his Mac, on my Mac can not, and finally Oliver help to fix

Audiotoolbox.framework is a C-based framework that uses it to play the sound effect by registering short audio with the system sound service. The System Sound Service is a simple, low-level voice playback service, but it also has some limitations:

1. Audio playback time cannot exceed 30s

2. The data must be in PCM or IMA4 format

3. Audio files must be packaged as one of the. CAF,. AIF,. wav (Note that this is an official document, the actual test found some. mp3 can also be played)

The following are the steps to play the sound using the system voice Service:

1. Call the Audioservicescreatesystemsoundid (Cfurlref infileurl, systemsoundid* outsystemsoundid) function to get the system sound ID.

2. If you need to listen for playback to complete operation, use Audioservicesaddsystemsoundcompletion (Systemsoundid insystemsoundid,
Cfrunloopref Inrunloop, Cfstringref inrunloopmode, Audioservicessystemsoundcompletionproc inCompletionRoutine, void* Inclientdata) method to register the callback function.

3. Call Audioservicesplaysystemsound (Systemsoundid insystemsoundid) or Audioservicesplayalertsound (SystemSoundID Insystemsoundid) method to play the sound (the latter with a vibration effect).

viewcontroller.m//audiotoolbox////Created by city--online on 15/5/5.//Copyright (c) 2015 CYW. All rights reserved.//#import "ViewController.h" #import <AudioToolbox/AudioToolbox.h> @interface    Viewcontroller () @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    [Self playsoundeffect]; }/** * Playback Completion callback function * * @param soundid system sound ID * @param data passed Clientdata callback */void soundcompletecallback (Systemsoundid so Undid,void * clientdata) {NSLog (@ "Play complete ...");}    -(void) playsoundeffect{nsstring *audiofile=[[nsbundle Mainbundle] pathforresource:@ "1" oftype:@ "wav"];    NSLog (@ "%@", audioFile);    Nsurl *fileurl=[nsurl Fileurlwithpath:audiofile];    NSLog (@ "%@", FILEURL);    1. Get the system sound ID systemsoundid soundid=0; /** * Infileurl: Audio file URL * outsystemsoundid: Sound ID (this function adds a sound file to the System Audio service and returns a long shape ID) */audioservicescreatesystem    Soundid (__bridge cfurlref) (FILEURL), &soundid); If you need to perform certain operations after playback, you can call the following method to register a playback completion callback function AudioservicesaDdsystemsoundcompletion (soundid, NULL, NULL, soundcompletecallback, NULL); 2. Play audio Audioservicesplaysystemsound (Soundid),//play sound//Audioservicesplayalertsound (Soundid);//play Sound and vibrate}-(void) d    idreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end

The Sound of Multimedia

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.