Use of iOS short sound

Source: Internet
Author: User

1. Get the universal short sound ID
#import <Foundation/Foundation.h> @interface mjaudiotool:nsobject/** *  Play sound * *  @param filename filename */+ (void) PlaySound: (NSString *) filename;/** *  destroy sound effects * *  @param filename Audio file name */+ (void) Disposesound: (NSString *) filename; @end

#import "MJAudioTool.h" #import <AVFoundation/AVFoundation.h> @implementation mjaudiotool//dictionary: filename as key, Soundid as value//store all audio idstatic nsmutabledictionary *_soundiddict;+ (void) initialize{_soundiddict = [ Nsmutabledictionary dictionary];}        + (void) PlaySound: (NSString *) filename{if (!filename) return;    1. Remove Soundid systemsoundid soundid from the dictionary = [_soundiddict[filename] unsignedlongvalue]; if (!soundid) {//create//Load audio file Nsurl *url = [[NSBundle mainbundle] Urlforresource:filename Withextension:ni                L];                if (!url) return;                Create the sound Effects ID audioservicescreatesystemsoundid ((__bridge cfurlref) URL, &soundid);    Put dictionary _soundiddict[filename] = @ (Soundid); }//2. Play Audioservicesplaysystemsound (Soundid);}        + (void) Disposesound: (NSString *) filename{if (!filename) return;    Systemsoundid Soundid = [_soundiddict[filename] unsignedlongvalue]; if (Soundid) {//Destroy sound ID AuDioservicesdisposesystemsoundid (Soundid);    Remove [_soundiddict removeobjectforkey:filename] from the dictionary; }} @end

2. Use of common methods

[Mjaudiotool playsound:@ "Buyao.wav"];

Use of iOS short sound

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.