First, access to sound services
Add frame Audiotoolbox and sound files to play, plus import the framework's interface files in the class that implements the sound service:
#import <AudioToolbox/AudioToolbox.h>
Playing system sounds requires two functions that are audioservicescreatesystemsoundid and audioservicesplaysystemsound, and you need to declare a variable of type Systemsoundid type. That represents the sound file to use.
Copy Code code as follows:
-(Ibaction) Playsyssound: (ID) Sender {
Systemsoundid SourceID;
The method that invokes the NSBundle class Mainbundle returns a NSBundle object that corresponds to the directory where the current program executable binaries belong
NSString *soundfile = [[NSBundle mainbundle] pathforresource:@ "SoundEffect" oftype:@ "wav"];
A Cfurlref object that points to the file location and a pointer to the SYSTEMSOUNDID variable to set
Audioservicescreatesystemsoundid ((cfurlref) [Nsurl Fileurlwithpath:soundfile], &soundid);
Audioservicesplaysystemsound (Soundid);
}
second, reminding Sound and vibration
1. Reminder tone
and the System sound difference:
If the handset is in mute state, then the reminder tone will automatically trigger the vibration;
The function required to play a reminder is audioservicesplayalertsound rather than audioservicesplaysystemsound.
2, Vibration
Just call the Audioservicesplaysystemsound () method and pass in the Ksystemsoundid_vibrate constant.
If the device doesn't support vibrations (like the ipad 2), it doesn't matter, it just doesn't vibrate.
Third, Avfoundation Framwork
For compressed audio files, or for audio files exceeding 30 seconds, you can use the Avaudioplayer class.
1, Avaudioplayer also need to know the path of audio files;
2, this class corresponds to the Avaudioplayerdelegate has two delegate methods:
1), audiodidfinishplaying:successfully: When the audio playback is completed after the trigger;
2, Audioplayerendinterruption: When the program is interrupted by the application outside, the return to the application of the trigger.
Iv. MediaPlayer Framwork
You can use MPMoviePlayerController to play movie files (as if you can play only the H.264, MPEG-4 Part2 video formats), and you can play videos on the internet.
V. Invoke and customize instance instances of sound effects
the demand is roughly divided into three kinds:
1. Vibration
2. System sound effects (no need to provide audio files)
3. Custom sound effects (audio files required)
Encapsulation of my tool class:
Copy Code code as follows:
//
WQPlaySound.h
Wqsound
//
Created by Read Alizarin on 12-7-20.
Copyright (c) 2012 __mycompanyname__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface Wqplaysound:nsobject
{
Systemsoundid Soundid;
}
/**
* @brief for playback vibration effect initialization
*
* @return Self
*/
-(ID) initforplayingvibrate;
/**
* @brief initialization for playback system sound (no audio file is required)
*
* @param resourcename System sound Name
* @param type system sound effects types
*
* @return Self
*/
-(ID) Initforplayingsystemsoundeffectwith: (NSString *) resourcename ofType: (NSString *) type;
/**
* @brief initialize for playback of specific audio files (audio files are required)
*
* @param filename audio filename (added to the project)
*
* @return Self
*/
-(ID) Initforplayingsoundeffectwith: (NSString *) filename;
/**
* @brief Play Sound
*/
-(void) play;
@end
Copy Code code as follows:
//
Wqplaysound.m
Wqsound
//
Created by Read Alizarin on 12-7-20.
Copyright (c) 2012 __mycompanyname__. All rights reserved.
//
#import "WQPlaySound.h"
@implementation Wqplaysound
-(ID) initforplayingvibrate
{
self = [super init];
if (self) {
Soundid = ksystemsoundid_vibrate;
}
return self;
}
-(ID) Initforplayingsystemsoundeffectwith: (NSString *) resourcename ofType: (NSString *) type
{
self = [super init];
if (self) {
NSString *path = [[NSBundle bundlewithidentifier:@ "Com.apple.UIKit"] pathforresource:resourcename Oftype:type];
if (path) {
Systemsoundid Thesoundid;
Osstatus error = Audioservicescreatesystemsoundid (__bridge cfurlref) [Nsurl Fileurlwithpath:path], &theSoundID);
if (Error = = Kaudioservicesnoerror) {
Soundid = Thesoundid;
}else {
NSLog (@ "Failed to create Sound");
}
}
}
return self;
}
-(ID) Initforplayingsoundeffectwith: (NSString *) filename
{
self = [super init];
if (self) {
Nsurl *fileurl = [[NSBundle mainbundle] Urlforresource:filename Withextension:nil];
if (FileURL!= nil)
{
Systemsoundid Thesoundid;
Osstatus error = Audioservicescreatesystemsoundid ((__bridge cfurlref) FileURL, &thesoundid);
if (Error = = Kaudioservicesnoerror) {
Soundid = Thesoundid;
}else {
NSLog (@ "Failed to create Sound");
}
}
}
return self;
}
-(void) play
{
Audioservicesplaysystemsound (Soundid);
}
-(void) dealloc
{
Audioservicesdisposesystemsoundid (Soundid);
}
@end
To invoke a method step:
1. Join Audiotoolbox.framework to the project
2. Call the Wqplaysound tool class
2.1 Vibrations
Copy Code code as follows:
Wqplaysound *sound = [[Wqplaysound alloc]initforplayingvibrate];
[Sound play];
2.2 System sound effects, taking tock as an example
Copy Code code as follows:
Wqplaysound *sound = [[Wqplaysound alloc]initforplayingsystemsoundeffectwith:@ "tock" ofType:@ "AIFF"];
[Sound play];
2.3 Custom sound effects, add tap.aif audio files to the project
Copy Code code as follows:
Wqplaysound *sound = [[Wqplaysound alloc]initforplayingsoundeffectwith:@ "Tap.aif"];
[Sound play];