iOS programs play audio files

Source: Internet
Author: User

1. Controller code

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "RYAVTool.h"

@interface viewcontroller ()

@property(nonatomic,strong)nsmutabledictionary*allavid;

@end

@implementation Viewcontroller

-(void) viewdidload {

[Super viewdidload];

}

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event

{

self. Allavid= [ryavtool playavwithname:@ "M_06.wav"];

}

memory warning when scavenging cache

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

[self. Allavid enumeratekeysandobjectsusingblock: ^ (ID key, ID obj, BOOL *stop) {

audioservicesdisposesystemsoundid([obj unsignedintvalue]);

}];

}

@end

2. Auxiliary Tool Class Code

#import <Foundation/Foundation.h>

@interface ryavtool: nsobject

+ (nsmutabledictionary*) Playavwithname: (nsstring*) name;

@end

#import "RYAVTool.h"

#import <AVFoundation/AVFoundation.h>

Static nsmutabledictionary *_allid;

Static systemsoundid _currentdata;

@implementation Ryavtool

+ (void) load

{

if (_allid= =Nil) {

_allid =[[nsmutabledictionary alloc]init];

}

}

+ (nsmutabledictionary*) Playavwithname: (nsstring*) name

{

if (!name) {

return _allid;

}

nsstring*path=[[nsbundle mainbundle]pathforresource: Name ofType: Nil];

if (!path) {

return _allid;

}

nsurl *url=[nsurl fileurlwithpath:p ath];

_currentdata=[_allid[name] unsignedintvalue];

if (! _currentdata) {

// Create audio playback files

Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &_currentdata);

// save

_allid[name]=@ (_currentdata);

}

// play

audioservicesplayalertsound(_currentdata);

return _allid;

}

@end

iOS programs play audio files

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.