Xcode에서 음악을 재생해 보았습니다.
Development Environment
Language
Swift 3.0
Step
1- Add your music file to Assets.xcassets
2- import AVFoundation to activate the library to use
import AVFoundation
3- Declare a parameter of AVAudioPlayer
var player: AVAudioPlayer?
4- Create main function that will play a music file in Xcode
func playSound() {
guard let sound = NSDataAsset(name: "前前前世") else {
print("The music file that you mentioned is not found in asset folder")
return
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeMPEGLayer3)
player!.play()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
5- Done
Source Code
ViewController.swiftimport UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
playSound()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
/* Function */
extension ViewController{
func playSound() {
guard let sound = NSDataAsset(name: "前前前世") else {
print("The music file that you mentioned is not found in asset folder")
return
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeMPEGLayer3)
// You can print name of music that you are gonna play.
print (sound.name)
player!.play()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
}
Conclusion
이제 음악 재생 앱을 만들 수 있습니다!
Reference
이 문제에 관하여(Xcode에서 음악을 재생해 보았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/law/items/dfae874393f51db39c3e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
1- Add your music file to Assets.xcassets
2- import AVFoundation to activate the library to use
import AVFoundation
3- Declare a parameter of AVAudioPlayer
var player: AVAudioPlayer?
4- Create main function that will play a music file in Xcode
func playSound() {
guard let sound = NSDataAsset(name: "前前前世") else {
print("The music file that you mentioned is not found in asset folder")
return
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeMPEGLayer3)
player!.play()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
5- Done
Source Code
ViewController.swiftimport UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
playSound()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
/* Function */
extension ViewController{
func playSound() {
guard let sound = NSDataAsset(name: "前前前世") else {
print("The music file that you mentioned is not found in asset folder")
return
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeMPEGLayer3)
// You can print name of music that you are gonna play.
print (sound.name)
player!.play()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
}
Conclusion
이제 음악 재생 앱을 만들 수 있습니다!
Reference
이 문제에 관하여(Xcode에서 음악을 재생해 보았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/law/items/dfae874393f51db39c3e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
playSound()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
/* Function */
extension ViewController{
func playSound() {
guard let sound = NSDataAsset(name: "前前前世") else {
print("The music file that you mentioned is not found in asset folder")
return
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeMPEGLayer3)
// You can print name of music that you are gonna play.
print (sound.name)
player!.play()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
}
이제 음악 재생 앱을 만들 수 있습니다!
Reference
이 문제에 관하여(Xcode에서 음악을 재생해 보았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/law/items/dfae874393f51db39c3e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)