简体   繁体   English

Swift 如何暂停音频

[英]Swift how to pause audio

So I am creating a simple radio station app.所以我正在创建一个简单的广播电台应用程序。 I have coded the following and got the play / pause button switching text.我编写了以下代码并获得了播放/暂停按钮切换文本。 I just can't work out how to access the player in the button when the func is inside ViewDidLoad.当func在ViewDidLoad中时,我只是不知道如何访问按钮中的播放器。 I am sure I need to set global variables?我确定我需要设置全局变量吗?

override func viewDidAppear(_ animated: Bool) {
    guard let url = URL(string: "http://stream.radiomedia.com.au:8003/stream") else {
        return
    }

    // Create a new AVPlayer and associate it with the player view
    let player = AVPlayer(url: url)
    player.allowsExternalPlayback = true
    player.usesExternalPlaybackWhileExternalScreenIsActive = true

    // Create a new AVPlayerViewController and pass it a reference to the player.
    let playerViewController = AVPlayerViewController()
    playerViewController.contentOverlayView?.backgroundColor = UIColor.white
    playerViewController.view.frame = CGRect (x:100, y:100, width:200, height:200)
    playerViewController.player = player

    self.addChild(playerViewController)
    self.view.addSubview(playerViewController.view)
    playerViewController.didMove(toParent: self)
    playerViewController.player = player
    player.play()
}

@IBAction func playVideo(_ sender: Any) {
    if(playButton == false){
        playButton = true;
        (sender as! UIButton).setTitle("Pause", for: [])
        player.pause() // does not work
    }else
    {
        playButton = false;
        (sender as! UIButton).setTitle("Play", for: [])
        player.play() // does not work 
    }
}
class YourVC: UIViewController {
var player = AVPlayer()
}

just declare the player inside your class so that other functions can access it.只需在 class 中声明播放器,以便其他功能可以访问它。

and in your viewDidApper(), change:并在您的 viewDidApper() 中,更改:

let player = AVPlayer(url: url)

to

self.player = AVPlayer(url: url)

then you can call player.pause() and player.play() from anywhere inside your Class.然后您可以从 Class 中的任何位置调用player.pause()player.play()

import UIKit
import AVFoundation

class AudioPlayerVC: UIViewController {

var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
    super.viewDidLoad()

    var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("wakeUp", ofType: ".mp3"))
    var error:NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
    audioPlayer.prepareToPlay()
    audioPlayer.play()
}
@IBAction func onPressPlayPaush(_ sender: UIButton) {

    if(sender == false){
        sender = true
        audioPlayer.pause() 
    }else
    {
        sender = false
        audioPlayer.play() 
    }
}

Using AVAudioPlayer :使用AVAudioPlayer

import UIKit
import AVFoundation

class AudioPlayerVC: UIViewController {

    var audioPlayer:AVAudioPlayer?

    override func viewDidLoad() {
      super.viewDidLoad()

      audioPlayer = try? AVAudioPlayer(contentsOf: URL(string: "http://stream.radiomedia.com.au:8003/stream")!)
      audioPlayer?.prepareToPlay()
      audioPlayer?.play()
    }
}
@IBAction func onPressPlayPaush(_ sender: UIButton) {
    audioPlayer != nil && audioPlayer!.isPlaying ? audioPlayer?.pause() : { audioPlayer?.play() }()
}

Using AVPlayer :使用AVPlayer

import UIKit
import AVFoundation

class AudioPlayerVC: UIViewController {

    var audioPlayer:AVPlayer!

    override func viewDidLoad() {
      super.viewDidLoad()

        audioPlayer = AVPlayer(url: URL(string: "http://stream.radiomedia.com.au:8003/stream")!)
        audioPlayer!.play()
    }
}
@IBAction func onPressPlayPaush(_ sender: UIButton) {
        audioPlayer!.timeControlStatus == .playing ||
        audioPlayer!.timeControlStatus == .waitingToPlayAtSpecifiedRate
            ? audioPlayer?.pause() : { audioPlayer?.play() }()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM