[英]Swift how to pause audio
所以我正在創建一個簡單的廣播電台應用程序。 我編寫了以下代碼並獲得了播放/暫停按鈕切換文本。 當func在ViewDidLoad中時,我只是不知道如何訪問按鈕中的播放器。 我確定我需要設置全局變量嗎?
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()
}
只需在 class 中聲明播放器,以便其他功能可以訪問它。
並在您的 viewDidApper() 中,更改:
let player = AVPlayer(url: url)
至
self.player = AVPlayer(url: url)
然后您可以從 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()
}
}
使用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() }()
}
使用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.