Airplay support iOS - ios

I'am trying to add airplay for my audio app. Here is my code
let buttonView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
let routerPickerView = AVRoutePickerView(frame: buttonView.bounds)
routerPickerView.tintColor = UIColor.white
routerPickerView.activeTintColor = UIColor.white
buttonView.addSubview(routerPickerView)
self.btnsStack.addArrangedSubview(buttonView)
This works well with my Mac, but when I try to play it with my Samsung Smart TV, there is a loader keeps on spinning. I tried connecting my TV with other apps like Spotify and it works.

Found my solution, so for casting airplay with smart tv one line of code is must
player?.allowsExternalPlayback = false
player is your AVPlayer.

Related

How do I use screen mirroring in iOS apps

I just need to use screen mirroring on enabled device when I click the button on my app screen. There is an airplay button in my app, clicking on which will check for available mirroring devices such as tv or laptop but I don't think it is for screen mirroring. I want to call screen mirroring in my app (I want to use mirroring not casting) on that screen.
I don't know Objective-c well, so please use swift if you use example code. Thanks.
Please check below, this might help you to understand how currently apps are achieving this.
Screen Mirroring iPhone to smart TV
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let routePickerView = AVRoutePickerView(frame: CGRect(x: 0.0, y: 30.0, width: 30.0, height: 30.0))
routePickerView.backgroundColor = UIColor.lightGray
self.view.addSubview(routePickerView)
let avAsset = AVAsset(url: URL(string: "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4")!)
let avPlayerItem = AVPlayerItem(asset: avAsset)
let avPlayer = AVPlayer(playerItem: avPlayerItem)
let avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.frame = CGRect(x: 0.0, y: 40.0, width: self.view.frame.size.width, height: self.view.frame.size.height - 40.0)
self.view.layer.addSublayer(avPlayerLayer)
avPlayer.seek(to: CMTime.zero)
avPlayer.play()
}
This code shows a player and adds the ability to mirror ONLY the player into the Mac screen.
You can check below as well.
how-to-do-screen-mirroring-using-airplay-from-the-application-not-from-control

Screen Mirroring iPhone to smart TV

I want to create an application screen mirror the whole screen to smart TV
I have found some apps on the market that do the trick
This is a sample
https://apps.apple.com/ec/app/gomirror-screen-mirroring/id1484792219
I paid and tried the trial version, then tried to mirror the iPhone screen with a smart Samsung TV, and it worked.
Also I tried something else, I used Reflector Mac application (Desktop app) and used this code
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let routePickerView = AVRoutePickerView(frame: CGRect(x: 0.0, y: 30.0, width: 30.0, height: 30.0))
routePickerView.backgroundColor = UIColor.lightGray
self.view.addSubview(routePickerView)
let avAsset = AVAsset(url: URL(string: "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4")!)
let avPlayerItem = AVPlayerItem(asset: avAsset)
let avPlayer = AVPlayer(playerItem: avPlayerItem)
let avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.frame = CGRect(x: 0.0, y: 40.0, width: self.view.frame.size.width, height: self.view.frame.size.height - 40.0)
self.view.layer.addSublayer(avPlayerLayer)
avPlayer.seek(to: CMTime.zero)
avPlayer.play()
}
this code show a player and add the ability to mirror ONLY the player into Mac screen, But I just was playing around, What I really want to screen mirror the full screen to a Smart TV.
Edit 1:
I found that some applications are recording the screen of the Mobile phone and send it as a video for the TV, and this make some delay between the action on the iPhone and the preview on the TV, so if any one knows how to that programmatically that would be great.

Swift: how to create FDWaveformview Dynamically

I want to show wave of multiple audios in scrollview using swift4 and xcode 9. I am using cocoapos library FDWaveFormView for showing wave of audio file. For this I have to create fdwaveformview dynamically. Fdwaveformview works fine if I create this in story board. But it shows an error when create dynamically in swift class.
Code:
for index in selectedAudios {
audioQueue.append(AVPlayerItem(url: index as! URL))
print("aduio url: \(index)")
let waveForm = FDWaveformView(frame: CGRect(x: 0, y: 0, width: 300, height: 150)) // error
audio_scroll_view.addSubview(waveForm!)
}
Screen Shot Of Error
Error: FDWaveformView initializer is inaccessible due to 'internal' protection level
Solution:
let frame = CGRect(x: 0, y: 0, width: 300, height: 100)
let waveform = FDWaveformView()
waveform.frame = frame
audio_scroll_view.addSubview(waveform)

How can I make my app permanently display volume instead of ringer?

How can I make my app permanently display volume instead of ringer while the user is in my app. Currently when using the application the app displays the ringer sound adjuster.
You can use MPVolumeView .
let volumeViewHolder = UIView(frame: CGRect(x: 20, y: 100, width: view.bounds.size.width - 40, height: 200))
volumeViewHolder.backgroundColor = UIColor.gray
let volumeView = MPVolumeView(frame: volumeViewHolder.bounds)
volumeView.showsRouteButton = true
volumeView.showsVolumeSlider = true
volumeViewHolder.addSubview(volumeView)
view.addSubview(volumeViewHolder)
Its just a demo. You can have your version with frames and styling as per your requirements

WKWebView do not work with AVAudioSession's category

By setting app's AVAudioSession's category to AVAudioSessionCategoryPlayback with option mixWithOthers, will make sounds from video in UIWebView be mixed with third party background music (such as Spotify).
But it do NOT work at all once switch to WKWebView. Code sample:
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
try audioSession.setActive(true)
} catch {
print(error)
}
/*
// Mixed background music & video sound in web works with UIWebView
let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
self.view.addSubview(webview)
webview.loadRequest(URLRequest(url: URL(string: "http://www.ultimedia.com/deliver/generic/iframe/mdtk/01509739/src/8l50lp/ad/yes")!))
*/
// Mixed background music & video sound in web do NOT work with WKWebView
let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
self.view.addSubview(webview)
webview.load(URLRequest(url: URL(string: "http://www.ultimedia.com/deliver/generic/iframe/mdtk/01509739/src/8l50lp/ad/yes")!))
// end of viewDidLoad
Code above tested on Xcode 8.2.1, iOS 10.2.1
Any idea or workaround would be really helpful.
Many thanks.

Resources