-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioPlayerViewController.swift
104 lines (85 loc) · 3.19 KB
/
AudioPlayerViewController.swift
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// AudioPlayerViewController.swift
//
// Created by Speedyfriend67 on 15.07.24
//
import UIKit
import AVFoundation
class AudioPlayerViewController: UIViewController {
var audioPlayer: AVAudioPlayer?
var audioURL: URL?
var timer: Timer?
let playPauseButton = UIButton(type: .system)
let currentTimeLabel = UILabel()
let durationLabel = UILabel()
let progressSlider = UISlider()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
setupPlayer()
}
func setupUI() {
playPauseButton.setTitle("Play", for: .normal)
playPauseButton.addTarget(self, action: #selector(playPauseTapped), for: .touchUpInside)
currentTimeLabel.text = "00:00"
durationLabel.text = "00:00"
progressSlider.addTarget(self, action: #selector(sliderValueChanged), for: .valueChanged)
let stackView = UIStackView(arrangedSubviews: [playPauseButton, currentTimeLabel, progressSlider, durationLabel])
stackView.axis = .vertical
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
])
}
func setupPlayer() {
guard let url = audioURL else { return }
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.delegate = self
durationLabel.text = formatTime(audioPlayer?.duration ?? 0)
progressSlider.maximumValue = Float(audioPlayer?.duration ?? 0)
} catch {
print("Failed to initialize player: \(error)")
}
}
@objc func playPauseTapped() {
guard let player = audioPlayer else { return }
if player.isPlaying {
player.pause()
playPauseButton.setTitle("Play", for: .normal)
timer?.invalidate()
} else {
player.play()
playPauseButton.setTitle("Pause", for: .normal)
startTimer()
}
}
@objc func sliderValueChanged() {
audioPlayer?.currentTime = TimeInterval(progressSlider.value)
}
func startTimer() {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
self.updateUI()
}
}
func updateUI() {
currentTimeLabel.text = formatTime(audioPlayer?.currentTime ?? 0)
progressSlider.value = Float(audioPlayer?.currentTime ?? 0)
}
func formatTime(_ time: TimeInterval) -> String {
let minutes = Int(time) / 60
let seconds = Int(time) % 60
return String(format: "%02d:%02d", minutes, seconds)
}
}
extension AudioPlayerViewController: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
playPauseButton.setTitle("Play", for: .normal)
timer?.invalidate()
}
}