1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

音を鳴らす Swift

Posted at

■音や音楽を流す方法

使うもの

import AVFoundation

■使うクラス
このクラスのメソッドで音を鳴らしたり止めたりする
インスタンス化する

var player = AVAudioPlayer()

♪Bundleクラスを使って音源を指定する。

let Path = Bundle.main.bundleURL.appendingPathComponent("音源の名前")

♪指定した(Path)の音をAVAudioPlayerに入れ込む。

player = AVAudioPlayer(contentsOf:Path)

♪play()メソッドで鳴らす

player.play()

//ちなみに止めるはこれ
player.stop()

■しかしこのままではエラーになる。
do-catch文でエラーの場合を記述するとなおる。

do{

 player = try AVAudioPlayer(contentsOf:Path)
 player.play()

 }catch{

 print("エラー")
 }

これで大丈夫です簡単にまとめました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?