IOS基本聲音播放類

來源:互聯網
上載者:User

一個基本的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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.