zoukankan      html  css  js  c++  java
  • [Swift通天遁地]八、媒体与动画-(1)实现音频的播放和停止

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10353743.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    目录:[Swift]通天遁地Swift

    本文将演示实现对音频播放的控制。

    首先确保在项目中,已经安装了所需的第三方类库,点击查看安装的配置文件。

    1 platform :ios, '8.0'
    2 use_frameworks!
    3 
    4 target 'DemoApp' do
    5     source 'https://github.com/CocoaPods/Specs.git'
    6     pod 'CryptoSwift', :git => "https://github.com/krzyzanowskim/CryptoSwift", :branch => "master"
    7 end

    根据配置文件中的相关设置,安装第三方类库。

    完成安装之后,双击打开项目文件【DemoApp.xcworkspace】

    往项目中导入了一个音频文件。

    在左侧的项目导航区,打开视图控制器的代码文件【ViewController.swift】

     1 import UIKit
     2 //在当前的类文件中,引入已经安装的第三方类库。
     3 import AudioPlayer
     4 
     5 class ViewController: UIViewController {
     6     //添加一个音频播放器,作为当前类的一个属性。
     7     var audioPlayer:AudioPlayer!
     8     override func viewDidLoad() {
     9         super.viewDidLoad()
    10         // Do any additional setup after loading the view, typically from a nib.
    11         // Initialize
    12         
    13         //添加一个按钮,当用户点击该按钮时,开始播放该文件。
    14         let play = UIButton(frame: CGRect(x: 40, y: 80,  240, height: 40))
    15         //设置按钮在正常状态下的标题文字
    16         play.setTitle("Play", for: .normal)
    17         //设置按钮的背景颜色为橙色
    18         play.backgroundColor = UIColor.orange
    19         //给播放按钮控件绑定点击事件
    20         play.addTarget(self, action: #selector(ViewController.playMusic(_:)), for: UIControl.Event.touchUpInside)
    21         
    22         //添加第二个按钮,当用户点击该按钮时,停止音频文件的播放
    23         let stop = UIButton(frame: CGRect(x: 40, y: 180,  240, height: 40))
    24         //设置按钮在正常状态下的标题文字
    25         stop.setTitle("Stop", for: .normal)
    26         //设置按钮的背景颜色为橙色
    27         stop.backgroundColor = UIColor.orange
    28          //给停止按钮控件绑定点击事件
    29         stop.addTarget(self, action: #selector(ViewController.stopMusic(_:)), for: UIControl.Event.touchUpInside)
    30         
    31         //设置根视图的背景颜色
    32         self.view.backgroundColor = UIColor.orange
    33         //将两个按钮依次添加到根视图
    34         self.view.addSubview(play)
    35         self.view.addSubview(stop)
    36         
    37         //添加一个异常捕捉语句,用来初始化音频播放对象
    38         do
    39         {
    40             //读取项目中的音频文件,并初始化一个音频播放器
    41             try audioPlayer = AudioPlayer(fileName: "music.mp3")
    42             
    43             //给音频播放器添加一个播放结束事件监听器,
    44             audioPlayer.completionHandler = {(_ didFinish: Bool) -> Void in
    45                 //当播放结束时,在控制台输出提示语句。
    46                 print("---The music finishes playing, or is stopped.")
    47             }
    48         }
    49         catch
    50         {
    51             print("---Sound initialization failed")
    52         }
    53     }
    54     
    55     //添加一个方法,用来响应播放按钮的点击事件
    56     @objc func playMusic(_ button:UIButton)
    57     {
    58         //设置音频播放器音量的大小,范围从0.0到1.0
    59         audioPlayer.volume = 0.8
    60         //设置音频播放的循环次数为3
    61         audioPlayer.numberOfLoops = 3
    62         //调用音频播放器的播放方法,开始播放指定的音频文件。
    63         audioPlayer.play()
    64     }
    65     
    66     //添加一个方法,用来响应停止按钮的点击事件
    67     @objc func stopMusic(_ button:UIButton)
    68     {
    69         //当音频播放器处于播放状态时
    70         if audioPlayer.isPlaying
    71         {
    72             //在控制台输出播放的时长
    73             print(audioPlayer.duration)
    74             //输出播放器当前的时刻
    75             print(audioPlayer.currentTime)
    76             //在控制台输出音频文件的名称
    77             print(audioPlayer.name ?? "")
    78            //以及音频文件在项目中的路径
    79             print(audioPlayer.url ?? "")
    80             
    81             //调用播放器的淡出方法,在1秒钟内,逐渐停止音频的播放
    82             audioPlayer.fadeOut(duration: 1.0)
    83             
    84             //或者立即停止音乐的播放,
    85             //直接调用音频播放器的停止方法。
    86             audioPlayer.stop()
    87         }
    88     }
    89     
    90     override func didReceiveMemoryWarning() {
    91         super.didReceiveMemoryWarning()
    92         // Dispose of any resources that can be recreated.
    93     }
    94 }

  • 相关阅读:
    Linux cooked-mode capture 格式转换
    lo口环路问题分析
    感知器与多层感知机
    梯度下降
    Unity UGUI Canvas 使用Slider制作角色血条
    NGUI 通过按压Button控制角色持续行走
    python基础--函数(二)
    python基础--函数(一)
    python基础--推导式
    python基础--公共操作
  • 原文地址:https://www.cnblogs.com/strengthen/p/10353743.html
Copyright © 2011-2022 走看看