๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Application

[iOS/Swift] ์ฝ”๋“œ๋กœ ์ง„๋™(Vibrate) ์šธ๋ฆฌ๊ธฐ, ํšจ๊ณผ์Œ ๋„ฃ๊ธฐ - AudioServicesPlaySystemSound

AudioServicesPlaySystemSound ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋กœ ์ง„๋™(Vibrate) ์šธ๋ฆฌ๊ธฐ 


๐Ÿ’ก 1.  AVFoundation์„ import 

import AVFoundation

AVFoundation

: ์‹œ์ฒญ๊ฐ assets ์ž‘์—…, device์นด๋ฉ”๋ผ ์ œ์–ด, ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ ๋ฐ ์‹œ์Šคํ…œ ์˜ค๋””์˜ค์™€์˜ ์ƒํ˜ธ์ž‘์šฉ ๊ตฌ์„ฑ
 

๐Ÿ’ก 2.  AudioServicesPlaySystemSound ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋กœ ์ง„๋™ ๊ตฌํ˜„

 

๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์€ ๊ณณ์— ์•„๋ž˜ ์ฝ”๋“œ ์ž‘์„ฑ

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

 

*์ง„๋™, ํšจ๊ณผ์Œ ๋ฐœ์ƒ ํ•จ์ˆ˜ AudioServicesPlaySystemSound

func AudioServicesPlaySystemSound(_ inSystemSoundID: SystemSoundID)

Parameters

inSystemSoundID

: ์žฌ์ƒํ•  ์‹œ์Šคํ…œ ์‚ฌ์šด๋“œ. ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— AudioServicesCreateSystemSoundID(*:*:) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‹œ์Šคํ…œ ์‚ฌ์šด๋“œ๋ฅผ ์–ป์–ด์•ผ ํ•œ๋‹ค.

Discussion

์ด ๊ธฐ๋Šฅ์€ ์งง์€ ์‚ฌ์šด๋“œ(30์ดˆ ์ดํ•˜)๋ฅผ ์žฌ์ƒํ•œ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.

์ผ๋ถ€ iOS ๊ธฐ๊ธฐ์—์„œ(iPod touch ์ œ์™ธ) ๊ฐ’์— ์ƒ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ง„๋™, ํšจ๊ณผ์Œ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ด๋•Œ, kSystemSoundID_Vibrate์€ iPhone ์—์„œ AudioServicesPlayAlertSound(_:) ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์งง์€ ์ง„๋™์„ ํ˜ธ์ถœํ•œ๋‹ค. iPod touch ์—์„œ๋Š” ์•„๋ฌด ์ž‘์—…๋„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ด์™ธ, ํšจ๊ณผ์Œ ์ƒ์ˆ˜๊ฐ’ ํ™•์ธ >> https://iphonedev.wiki/index.php/AudioServices

https://iphonedev.wiki/index.php/AudioServices


๐Ÿ’ก ์ ์šฉํ•˜๊ธฐ 

// [์„ค๋ช… : ๋””๋ฐ”์ด์Šค ์ง„๋™ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ ๋ฉ”์†Œ๋“œ]
// [ํ•„์š” import : import AVFoundation]
// [์‚ฌ์šฉ ๋ฐฉ๋ฒ• : UIDevice.vibrate()]

import AVFoundation

//๋ฒ„ํŠผ ํด๋ฆญ์‹œ ๋‹จ๋ฐœ์„ฑ ์ง„๋™ ๋ฐœ์ƒ
@IBAction func vibrateButtonTapped(_ sender: Any) {
    UIDevice.vibrate()
}

// MARK: - [extension ์ •์˜ ์‹ค์‹œ : UIDevice]
extension UIDevice {

    static func vibrate() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    }
}
728x90
๋ฐ˜์‘ํ˜•