Skip to content

iosClassForBeginner/sampleSoundsApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

第20回: 1時間でiPhoneアプリを作ろう

音楽再生アプリ

当アカウントへ訪れていただき、誠にありがとうございます。第20回アプリ教室では、音楽再生アプリを作ります。自分のペースで勉強したい、勉強前に予習したい、内容を復習したい場合、ご利用ください。

アプリ教室に興味ある方、歓迎します。

Meetup
http://www.meetup.com/ios-dev-in-namba/

別途アプリ教室(有料)も開いております

http://learning-ios-dev.esy.es/

問い合わせ

株式会社ジーライブ http://geelive-inc.com

アプリ作成手順

0, 開発準備

0-1. xcodeで新規プロジェクトを立ち上げる image

0-2. 音楽ファイルをダウンロード

0-3. 音楽ファイルをプロジェクトにドロップ image

1, Storyboardで、アプリのデザイン

1-1. main.storyboardを選択し、UI部品から下記を配置します。(ドラッグ&ドロップ)

  • UIButton 再生ボタン
  • UIButton 停止ボタン image

1-2. Storyboardの下記UI部品を、ViewController.swiftに紐づけます(control押しながらドラッグ)

  • UIButton 再生ボタン (actionで紐付ける)
  • UIButton 停止ボタン (actionで紐付ける)

image

2, ViewController.swiftにコード記述

  • 以下コードブロックを記入
import UIKit
import AVFoundation // オーディオプレイヤー使うとき、インポートしましょう

class ViewController: UIViewController {
    
    var audioPlayer: AVAudioPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()
        initAudioPlayer()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // audioPlayer初期化
    func initAudioPlayer()
    {
        let filePath = Bundle.main.path(forResource: "sound", ofType: "mp3")
        let audioPath = URL(fileURLWithPath: filePath!)
        audioPlayer = try? AVAudioPlayer(contentsOf: audioPath)
        audioPlayer?.prepareToPlay()
    }

    @IBAction func play(_ sender: UIButton) {
        audioPlayer?.play()
    }

    @IBAction func stop(_ sender: UIButton) {
        audioPlayer?.stop()
    }
}

About

音楽再生アプリのサンプル

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages