IOS基本声音播放类
来源:互联网 发布:mac ps导入字体 编辑:程序博客网 时间:2024/05/21 12:24
一个基本的IOS声音播放支持类,注释不多对付看吧
#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
在应用程序初始化后创建该类对象并调用其初始化函数,声音也可继续扩展添加。需要播放时调用相应的函数即可。
部分功能还没有完成,如获取配置中的音量大小,保存音量设置,声音开关等。
另外 AudioServicesPlaySystemSound(sys_sID); 来播放是声音不知道如何控制音量~~
转载请注明来自:http://blog.csdn.net/zhao_yin
- IOS基本声音播放类
- IOS基本声音播放类
- IOS-播放声音,中断声音
- iOS播放系统声音
- iOS-播放系统声音
- iOS播放系统声音
- ios 播放声音
- ios播放声音
- iOS播放系统声音
- ios 播放系统声音
- iOS 播放简短声音
- iOS 程序播放声音
- iOS播放声音
- iOS 声音播放
- ios 播放系统声音
- ios 播放声音 播放震动
- iOS 播放系统声音&系统声音大全
- ios 播放声音和震动
- VB工程--实例63--秒表计时器
- dedecms和ucenter整合时出现Error:Unknown column 'app3' in 'where clause'的解决办法
- POJ 3666 Making the Grade
- Sftp和ftp over ssh2的区别
- base-touch
- IOS基本声音播放类
- 反向代理简介
- 实验4-1 逻辑量的编码和关系操作符
- 在gac中添加dll程序集
- 数据结构学习笔记 --- 前言
- 分享SFTP简单的使用方法
- 实验4-2 逻辑表达式
- 某个操作挂起-避免安装SQL,Server时出现的“某个操作挂起”需要重启的提示
- LINUX主要目录及内容