iOS音效播放代碼

jopen 10年前發布 | 3K 次閱讀 C/C++ IOS

//

// YYViewController.m

// 14-音效播放

//

// Created by apple on 14-8-8.

// Copyright (c) 2014年 yangyong. All rights reserved.

//

import "YYViewController.h"

import <AVFoundation/AVFoundation.h>

@interface YYViewController ()

@end

@implementation YYViewController

  • (void)viewDidLoad

{

[super viewDidLoad];

}

-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event

{

//1.獲得音效文件的全路徑



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



//2.加載音效文件,創建音效ID(SoundID,一個ID對應一個音效文件)

SystemSoundID soundID=0;

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



//把需要銷毀的音效文件的ID傳遞給它既可銷毀

//AudioServicesDisposeSystemSoundID(soundID);



//3.播放音效文件

//下面的兩個函數都可以用來播放音效文件,第一個函數伴隨有震動效果

AudioServicesPlayAlertSound(soundID);

//AudioServicesPlaySystemSound(<#SystemSoundID inSystemSoundID#>)

}

@end </pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!