国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

UI音樂播放之入門篇AudioSerVicesPlay

2019-11-14 18:30:32
字體:
來源:轉載
供稿:網(wǎng)友

-1-  AuidoSerVices 常用于提示音的播放,該種音頻具有以下四種特點:

      1>長度一般不超過30秒,不需要對播放過程進行控制

      2>不能循環(huán)播放,不能暫停

      3>不能播放立體聲

      4>不能播放混音

-2-創(chuàng)建提示音頻AuidoSerVices。。的準備工作

 1.添加一個系統(tǒng)類庫。audioToolbox.framework

 2.導入頭文件在viewController中 #import <AudioToolbox/AudioToolbox.h>

 

 

-3-提示音的幾個常用方法:

1.獲得音效文件的路徑

   NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];

2.加載音效文件,得到對應的音效ID

   SystemSoundID soundID = 0;

   AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);

3.播放音效

   AudioServicesPlaySystemSound(soundID);

 4.音效播放的c語言函數(shù) 

 音效播放的函數(shù)都是基于c語言編寫的,所以在使用的過程中要注意與oc方法使用的區(qū)別

加載音效文件

  AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

釋放音效資源

  AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效

  AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效帶點震動

  AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

 

-4-程序簡單實現(xiàn)

#import "RootViewController.h"#import <AudioToolbox/AudioToolbox.h>@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    [self creatUIButton];}-(void)creatUIButton{    UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];     btn.frame=CGRectMake(100, 100, 100, 100);    btn.backgroundColor=[UIColor grayColor];    [btn setTitle:@"播放" forState:UIControlStateNormal];    [btn setTitleColor:[UIColor cyanColor ] forState:UIControlStateNormal];    [btn addTarget:self action:@selector(PRessBtn:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];}-(void)pressBtn:(id)sender{    //1.獲取全路徑 因為加載音效需要使用CFURLRef 來加載音樂,所以使用能和它相互轉換的NSURL 來轉換路徑    NSString * pathStr= [[NSBundle mainBundle]pathForResource:@"sound" ofType:@"wav"];    NSURL * url=[NSURL fileURLWithPath:pathStr];        //2.創(chuàng)建音效ID 一個ID代表一個音效文件    SystemSoundID SID;    //3.將音樂文件與ID綁定   將url 強轉 將sid與之綁定    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &SID);    //4.播放音效    AudioServicesPlayAlertSound(SID);    //5.當我們需要在音效播放結束時取消該音效則需要使用以下方法 函數(shù)名固定    AudioServicesAddSystemSoundCompletion(SID, nil, nil,finishSound, nil);}//該函數(shù)為系統(tǒng)方法,函數(shù)名,參數(shù)固定,不能修改void finishSound(SystemSoundID SID,void * finish){  //6.撤銷SID    AudioServicesDisposeSystemSoundID(SID);}

注意:點擊播放,就可以播放音效文件

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 明溪县| 文化| 舟曲县| 临海市| 霍林郭勒市| 长武县| 额济纳旗| 静海县| 石台县| 大安市| 右玉县| 马尔康县| 庆云县| 芒康县| 南阳市| 甘孜| 仲巴县| 灵丘县| 平原县| 石城县| 吉水县| 疏勒县| 桐梓县| 句容市| 天气| 昌江| 越西县| 宁城县| 宁晋县| 开平市| 克什克腾旗| 清远市| 来安县| 白沙| 临潭县| 德格县| 即墨市| 临清市| 青浦区| 八宿县| 银川市|