- made theme 2 banner dynamic, if ad loads then only it will be shown
- made radio ad dynamic, now ad will show up only if ad is received - added a retry count to fm, it will try 4 times to connect to the server if not then showing the reload btn
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
import UIKit
|
||||
import AVFoundation
|
||||
import GoogleMobileAds
|
||||
import MediaPlayer
|
||||
|
||||
class WokaFMVM{
|
||||
|
||||
@@ -18,7 +19,10 @@ class WokaFMVM{
|
||||
|
||||
var startTimeStamp = Date()
|
||||
var bottomBannerView = GADBannerView()
|
||||
|
||||
|
||||
// if retry count is 4 reset it to 0
|
||||
var retryCount = 0
|
||||
|
||||
func initView(){
|
||||
startTimeStamp = Date()
|
||||
vc.mainView.roundCorners(radius: 10, corners: [.topLeft, .topRight])
|
||||
@@ -43,18 +47,94 @@ class WokaFMVM{
|
||||
}
|
||||
self.vc.dismiss(animated: true)
|
||||
}
|
||||
setGoogleAd()
|
||||
|
||||
|
||||
// updateNowPlayingInfo()
|
||||
}
|
||||
|
||||
// MARK: - SetupAd
|
||||
|
||||
func stopMPNowPlayin(){
|
||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
|
||||
}
|
||||
|
||||
func setupNowPlayingInfo() {
|
||||
let nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
|
||||
var nowPlayingInfo: [String: Any] = [:]
|
||||
|
||||
// Set the media title, artist, and album name
|
||||
nowPlayingInfo[MPMediaItemPropertyTitle] = "Live FM"
|
||||
nowPlayingInfo[MPMediaItemPropertyArtist] = "WOKA"
|
||||
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = "Album Name"
|
||||
|
||||
// Optional: Set the artwork
|
||||
if let artworkImage = UIImage(named: "WokaLogo") {
|
||||
let artwork = MPMediaItemArtwork(boundsSize: artworkImage.size) { size in
|
||||
return artworkImage
|
||||
}
|
||||
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
|
||||
}
|
||||
|
||||
// Set the duration and current playback position
|
||||
if let currentItem = player.currentItem {
|
||||
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = currentItem.asset.duration.seconds
|
||||
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
|
||||
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
|
||||
}
|
||||
|
||||
// Assign the nowPlayingInfo to the center
|
||||
nowPlayingInfoCenter.nowPlayingInfo = nowPlayingInfo
|
||||
}
|
||||
|
||||
func setupRemoteCommandCenter() {
|
||||
let commandCenter = MPRemoteCommandCenter.shared()
|
||||
|
||||
// Enable play command
|
||||
commandCenter.playCommand.addTarget { [unowned self] event in
|
||||
if player.rate == 0.0 {
|
||||
player.play()
|
||||
return .success
|
||||
}
|
||||
return .commandFailed
|
||||
}
|
||||
|
||||
// Enable pause command
|
||||
commandCenter.pauseCommand.addTarget { [unowned self] event in
|
||||
if player.rate == 1.0 {
|
||||
player.pause()
|
||||
return .success
|
||||
}
|
||||
return .commandFailed
|
||||
}
|
||||
|
||||
// Enable next and previous track commands if needed
|
||||
commandCenter.nextTrackCommand.isEnabled = false
|
||||
commandCenter.previousTrackCommand.isEnabled = false
|
||||
}
|
||||
|
||||
func updateNowPlayingInfo() {
|
||||
if let currentItem = player.currentItem {
|
||||
var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo ?? [:]
|
||||
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
|
||||
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
|
||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func setGoogleAd(){
|
||||
/*
|
||||
Show google ads with dispatch queue.
|
||||
*/
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: { [weak self] in
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.8, execute: { [weak self] in
|
||||
guard let self else{return}
|
||||
AdReusable.sharedInstance.setupBannerAd(bannerView: self.bottomBannerView, in: vc.adView, adUnitID: K.GoogleAdIDs.themeTwo, viewController: self.vc)
|
||||
vc.adView.isHidden = false
|
||||
// vc.adView.isHidden = false
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Setup AV & Player
|
||||
|
||||
func setupAudioSession() {
|
||||
@@ -79,6 +159,7 @@ class WokaFMVM{
|
||||
playerItem = AVPlayerItem(url: url)
|
||||
player = AVPlayer(playerItem: playerItem)
|
||||
player.play()
|
||||
// setGoogleAd()
|
||||
}
|
||||
|
||||
func retryConnect(){
|
||||
|
||||
Reference in New Issue
Block a user