思路解析
- 创建播放音乐对象
- 音乐文件路径
- 进行播放
一、App icon-添加icon
二、进行按钮事件的关联
1.页面开发
附件代码
//
// ViewController.swift
// Ahong-App
//
// Created by oliva on 2020/2/2.
// Copyright © 2020 oliva. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController,AVAudioPlayerDelegate {
// 创建音频播放对象
var audioPlayer:AVAudioPlayer!
// var 变量
// let 常量
var randomDiceIndex1:Int = 0;
// 显示图片
@IBOutlet weak var diceImageView1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("显示图片了");
}
// 点击的事件 关联
// 点击的时候,播放声音
@IBAction func click(_ sender: UIButton) {
// 点击的时候,让它发出声音
// 得到声音的路径
let soundURL = Bundle.main.url(forResource: "note2",withExtension:"mp3");
do{
// 给音频对象赋值
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!);
// 进行播放
audioPlayer.play();
}catch{
print(error);
}
// print(123);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}