apple documentation과 stack overflow를 활용해서 AVAudioPlayer를 활용해보기
parameter, argument
https://github.com/appbrewery/Xylophone-iOS13
💡 IBAction을 실로폰마다 연결하는게 아니라
아래의 함수에 모두 연결한 뒤
sender에서 변화하는 값을 받아서 사용했다!!!
@IBAction func keyPressed(_ sender: UIButton) {
guard let title = sender.currentTitle else {
return
}
playSound(title: title)
}
💡 코드에 delay딜레이 주는 방법
//0.2초 후의 딜레이 후에 안의 코드 실행
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
//버튼 불투명하게
sender.alpha = 1.0
}
https://stackoverflow.com/questions/32036146/how-to-play-a-sound-using-swift
import UIKit
import AVFoundation // 반입
class ViewController: UIViewController {
var player: AVAudioPlayer! // 플레이어 생성, 파일이나 버퍼로부터 소리를 재생시킨다
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func keyPressed(_ sender: UIButton) {
//sender.titleLabel?.text 아래와 동일
guard let title = sender.currentTitle else {
return
}
playSound(title: title)
//버튼 투명하게
sender.alpha = 0.5
//0.2초 후의 딜레이 후에 안의 코드 실행
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
//버튼 불투명하게
sender.alpha = 1.0
}
}
func playSound(title: String) {
let url = Bundle.main.url(forResource: title, withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
}
}
<What I want my app to do> + <Which programming language> + <Which resource>
Play Sound
Swift
stackoverflow
getting high reputation on StackOverflow counts a lot. 명성 점수가 높으면 면접에 좋다.
https://developer.apple.com/documentation
Application Programming Interface(API) Documentation 보는 방법
아래와 같은 코드가 있을 때
AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
A representation of the code and resources stored in a bundle directory on disk.
AVAudioSession 검색해서 읽어본다.
https://developer.apple.com/documentation/foundation/bundle
class Bundle : NSObject
Core Foundation에서는 NSString과 CFString이 자유롭게 형변환될 수 있었지만, Bundle은 NSObject임에도 불구하고 CFBundle로 형변환될 수 없다.
Toll-Free Bridging
https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2
NSBundle *main = [NSBundle mainBundle];
NSString *resourcePath = [main pathForResource:@"Seagull" ofType:@"jpg"];
주의사항
파일이나 자원 탐색 시 대소문자를 구분한다
https://developer.apple.com/documentation/foundation/bundle/1410786-main
현재 실행 중인 번들 객체를 리턴한다.
Type Property
class var main: Bundle { get }
// Get the app's main bundle
let mainBundle = Bundle.main
init(for aClass: AnyClass)
// Get the bundle containing the specified private class.
let myBundle = Bundle(for: NSClassFromString("MyPrivateClass")!)
import Swift
func calculator() {
let a = Int(readLine()!)!
let b = Int(readLine()!)!
add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)
}
func add(n1: Int, n2: Int) {
print(n1 + n2)
}
func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}
func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}
func divide(n1: Int, n2: Int) {
print(n1 / n2)
}
calculator()
func getMilk (bottles: Int) {
var cost = bottles * 1.5
}
getMilk(bottles: 2)
부의추월차선