AudioViewController.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // AudioViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 24/08/22.
  6. //
  7. import UIKit
  8. import SideMenu
  9. import AVFoundation
  10. class AudioViewController: UIViewController {
  11. @IBOutlet var navigationBar: UINavigationBar!
  12. @IBOutlet var currentTimeLabel: UILabel!
  13. @IBOutlet var totalTimeLabel: UILabel!
  14. @IBOutlet var progressSlider: UISlider!
  15. @IBOutlet var backwardButton: UIButton!
  16. @IBOutlet var playButton: UIButton!
  17. @IBOutlet var forwardButton: UIButton!
  18. var pageName = String()
  19. var fileId = String()
  20. var player:AVPlayer?
  21. var playerItem:AVPlayerItem?
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. // Do any additional setup after loading the view.
  25. navigationBar.topItem?.title = pageName
  26. }
  27. override func viewWillAppear(_ animated: Bool) {
  28. super.viewWillAppear(animated)
  29. let url = URL(string: "http://3.7.239.194:8080/\(fileId).mp3")
  30. let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
  31. player = AVPlayer(playerItem: playerItem)
  32. let playerLayer=AVPlayerLayer(player: player!)
  33. playerLayer.frame = CGRect(x:0, y:0, width:10, height:50)
  34. self.view.layer.addSublayer(playerLayer)
  35. }
  36. /*
  37. // MARK: - Navigation
  38. // In a storyboard-based application, you will often want to do a little preparation before navigation
  39. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  40. // Get the new view controller using segue.destination.
  41. // Pass the selected object to the new view controller.
  42. }
  43. */
  44. @IBAction func backAction(_ sender: UIBarButtonItem) {
  45. self.navigationController?.popViewController(animated: true)
  46. }
  47. @IBAction func playAction(_ sender: UIButton) {
  48. if player?.rate == 0 {
  49. player!.play()
  50. playButton.setImage(UIImage(named: "pause"), for: .normal)
  51. } else {
  52. player!.pause()
  53. playButton.setImage(UIImage(named: "play-button-arrowhead"), for: .normal)
  54. }
  55. }
  56. @IBAction func progressAction(_ sender: UISlider) {
  57. let seconds : Int64 = Int64(sender.value)
  58. let targetTime:CMTime = CMTimeMake(value: seconds, timescale: 1)
  59. player!.seek(to: targetTime)
  60. if player!.rate == 0 {
  61. player?.play()
  62. }
  63. }
  64. }