-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_audio.swift
More file actions
58 lines (50 loc) · 1.67 KB
/
Copy pathapi_audio.swift
File metadata and controls
58 lines (50 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// api_audio.swift
// HelloSwift
//
// Created by 1 on 8/3/22.
//
import SwiftUI
import AVKit
struct api_audio: View {
@State var BDPlayer: AVAudioPlayer?
var body: some View {
VStack {
Button(action: {
playAudio()
}, label: {
Label("播放Bundle下的音频文件", systemImage: "waveform.circle")
})
.tint(.purple)
.controlSize(.large)
.buttonStyle(.borderedProminent)
.padding()
Text("说明:本页面音频文件,是从 App Bundle 中加载的资源,播放时,请打开声音。\n\n本示例使用AVkit库, 主要代码为:AVAudioPlayer(contentsOf: URL)")
.font(.caption)
.foregroundColor(.gray)
.frame(height: 80)
.padding()
}
.navigationTitle("Audio")
.navigationBarTitleDisplayMode(.inline)
.modifier(navBarViewCodeAndDocs(pageType: "API",pageID: "Audio"))
}
func playAudio() {
// abc.mp3音频文件是项目下Resouces下的文件
let FilePath = Bundle.main.url(forResource: "example_audio.m4a", withExtension: nil, subdirectory: "/Resouces.bundle/media")
if let BDURL = FilePath {
do {
try self.BDPlayer = AVAudioPlayer(contentsOf: BDURL)
self.BDPlayer?.volume = 5
self.BDPlayer?.play()
} catch {
print("Couldn't play audio. Error: \(error)")
}
}
}
}
struct api_audio_Previews: PreviewProvider {
static var previews: some View {
api_audio()
}
}