IOS basic sound playback

Source: Internet
Author: User

A basic IOS sound playback Support class. You can check if there are not many comments.
 
 
# Import <Foundation/Foundation. h>
# Import <AudioToolbox/AudioToolbox. h>
# Import <AVFoundation/AVFoundation. h>
 
 
@ Interface Sound_Base: NSObject {

Float volume;
BOOL is_turnON;

AVAudioPlayer * mainMenuMusic_Player;

SystemSoundID btn_soundID;
SystemSoundID err_soundID;

}
 
@ Property (nonatomic) BOOL is_turnON;
@ Property (nonatomic) float volume;
@ Property (nonatomic, retain) AVAudioPlayer * mainMenuMusic_player;
@ Property (nonatomic) SystemSoundID btn_soundID;
@ Property (nonatomic) SystemSoundID err_soundID;
 
 
 
-(Void) setSoundSwitch :( BOOL) sound_state;
-(Void) setUIVolume :( float) f_volume;
-(Void) initSound;
-(Void) playSysSound :( SystemSoundID) sys_sID;
-(Void) stopAllMusic;
 
 
 
 
# Import "Sound_Base.h"
 
@ Implementation Sound_Base
 
 
@ Synthesize mainMenuMusic_player;
@ Synthesize volume;
@ Synthesize is_turnON;
 
@ Synthesize btn_soundID;
@ Synthesize err_soundID;
 
 
 
 
-(Void) dealloc {

If (self. mainMenuMusic_player.playing ){
[Self. mainMenuMusic_player stop];
}
[MainMenuMusic_player release];

If (btn_soundID ){
AudioServicesDisposeSystemSoundID (btn_soundID), btn_soundID = 0;
}

If (err_soundID ){
AudioServicesDisposeSystemSoundID (err_soundID), err_soundID = 0;
}

[Super dealloc];
}
 
 
 
 
-(Void) initSound {

NSString * mainMenuMusic_path = [[NSBundle mainBundle] pathForResource: @ "bgm_0" ofType: @ "aac"];

NSURL * mainMenuMusic_url = [NSURL fileURLWithPath: mainMenuMusic_path];
Self. mainMenuMusic_player = [[AVAudioPlayer alloc] initWithContentsOfURL: mainMenuMusic_url error: nil];
Self. mainMenuMusic_player.numberOfLoops =-1;
[Self. mainMenuMusic_player prepareToPlay];


NSString * btnSound_path = [[NSBundle mainBundle] pathForResource: @ "sys_click" ofType: @ "aiff"];
AudioServicesCreateSystemSoundID (CFURLRef) [NSURL fileURLWithPath: btnSound_path],
& Btn_soundID );

NSString * errSound_path = [[NSBundle mainBundle] pathForResource: @ "sys_error" ofType: @ "aiff"];
AudioServicesCreateSystemSoundID (CFURLRef) [NSURL fileURLWithPath: errSound_path],
& Err_soundID );


// Todo get volume and is silence

Is_turnON = YES;

Float f_volume = 1.0;

[Self setUIVolume: f_volume];

}
 
 
 
 
-(Void) setSoundSwitch :( BOOL) sound_state {

Self. is_turnON = sound_state;

If (self. is_turnON ){
Self. mainMenuMusic_player.volume = self. volume;
} Else {
Self. mainMenuMusic_player.volume = 0;
}
}
 
 
 
-(Void) setUIVolume :( float) f_volume {

Self. volume = f_volume;

If (self. is_turnON ){
Self. mainMenuMusic_player.volume = f_volume;
}
Else {
Self. mainMenuMusic_player.volume = 0;
}

}
 
 
 
-(Void) playSysSound :( SystemSoundID) sys_sID {
If (self. is_turnON ){
AudioServicesPlaySystemSound (sys_sID );
}

}
 
 
-(Void) stopAllMusic {
[Self. mainMenuMusic_player stop];
}
 
 
 
@ End
 
 
After the application is initialized, this type of object is created and its initialization function is called. The sound can be further added. You can call the corresponding function when playing the video.
 
Some functions have not been completed, such as obtaining the volume in the configuration, saving the volume settings, and switching the sound.
 
In addition, AudioServicesPlaySystemSound (sys_sID); the sound does not know how to control the volume ~~

From zhao_yin notepad

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.