Cannot play after pausing tvOS (TVML/TVJS) - swift

I am developing a tvml/tvjs application and I have added the following code to allow audio to play in the background (applicationDidBecomeActive):
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(true)
} catch {
print("no audio to assign")
}
I also set the first item in Required Background Modes in info.plist to App plays audio or streams audio/video using AirPlay.
My problem now is that although I can play audio in the background and pause the audio from anywhere in tvOS, I cannot play the audio after pausing. I am looking for the functionality like the Podcast app where I can freely play and pause the current streaming audio.
I cannot seem to find anything in the documentation for TVML/TVJS that points me in the right direction. Thanks!

Related

How to start audio recording when app running on background?

I am working on audio recording. when application running on foreground i have to start audio recording and going to background at that time audio recording working fine.
But my question is that how to start audio recording when i am already in background, My audio recording function fired like this:
I have a Bluetooth LE device with buttons and an iOS app. Those two are paired (Bluetooth LE device and the iPhone which runs the iOS app) and the iOS app is listening for events on the Bluetooth LE device, events like a hit of a button.
Now, when the user hits a button on the Bluetooth LE device, the iOS app captures the event and I am able to run code even if the app is in background, but I am not able to start a voice recording.
I have already enable Background Modes:
Here is my Code for Audio Recording:
func startRecording() {
DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now(), qos: .background) {
let audioFilename = self.getDocumentsDirectory().appendingPathComponent("recording.m4a")
print("record Audio \(audioFilename)")
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
self.audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
self.audioRecorder.delegate = self
self.audioRecorder.record()
} catch {
self.finishRecording(success: false)
}
}
}
I can't find proper solution to do that thing, Please suggest me proper way to do that, Thanks in Advance.
This is not possible to do without an explicit UI interaction for security reasons, or else it would be possible to "spy" on a person (once paired, a person outside of a room could start recording and listen to what happens inside the room).
Workarounds could be:
send a local notification e.g. "The device is ready to record, want to start?". Once you tap this, the app opens in foreground, and should be able to start the recording.
use CallKit (suitable for phone-like use cases). When the user presses "Accept" in the CallKit system UI, it is possible to start recording audio even in background.
Here are my solutions:
Trigger a CallKit call from you app and control your headphone to answer the call.
Use your headphone to record and transfer voice data with bluetooth channel.

Missing sound on Apple Watch after resume from dock

I am using the following sounds (all 32K AAC as mentioned in Apple document but I use mono instead of stereo: https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/AudioandVideo.html) for my watchOS 3 games.
let hitSound = SKAction.playSoundFileNamed("hit.m4a", waitForCompletion:false)
let explosionSound = SKAction.playSoundFileNamed("explosion.m4a", waitForCompletion:false)
let music = SKAudioNode(fileNamed: "delta.m4a")
I use hitSound and explosionSound for short sound effect, and play music on the background, they works fine until the game is quit and resume from dock.
The music is resume and the sound effects are no longer heard. Is there any problem with using SKAction.playSoundFileNamed to play sound?

How to keep AVPlayer video playing when app goes background

I'm trying to keep the video in AVPlayer playing when app goes background, but it always stops. I use Swift. I've found a lot of examples, but nothing works.
I've turned on "App plays audio or streams audio/video using AirPlay" in Info.plist and added this code to the didFinishLaunchingWithOptions in AppDelegate:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch {
/*Error*/
}
What is the proper way to keep video playing?

how can we play audio and video together in ipod/iphone?

HI all
i am playing a video(.mp4 format) without sound (i mean it doesn't have sound it is a mute video ) and in the background i am playing an audio file (.mp3 format) when i play my code through simulator it works fine as i want like when i tap on the video it is just mute but behind i am playing the audio so for user it seems that video has this sound but when i installed my code in device and play video than it doesn't work like so it play video but without sound than how can i play an audio and a video together in the above format ?
actually we are not just playing a single video or audio file it just comes from an array by choosing randomly and same for the audio file so we cann't do this i think so any other idea for it ??
Should we use another format for audio aur video for doing this thing ??
thanks for the help
Balraj verma
The problem is that the default Audio Session does not allow audio mixing.
In the Reference Library (Working with Movie Players) they say you should use a mixable category configuration for your audio session, for example the Ambient category. In the Application Delegate:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryAmbient error: &setCategoryError];
Apple's documentation states that some audio formats are not suited to be played back simultaneously because of hardware restrictions. See Playing Multiple Sounds Simultaneously for more details.
A solution for you would be to use PCM encoded audio which should allow simultaneous playback.
I would like to add that I managed to play two mp3 audio files at the same time on 3G and 3GS iPhones. Which shouldn't be possible according to documentation but worked for me.
You can rather use the an instance of AvAudioPlayer in a new thread. Use the following link to see how does this work
http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/
create an instance of MPMoviePlayer to start playback of videos.
Hope this will work.
You should combine the audio and video using some video editing software like iMovie or Windows Movie Maker. Using the software to "composite" audio and video together is not a good idea and adds additional overhead, synchronization issues, etc.
As far as I know, you can't play AVAudioPlayer and MPMoviePlayer at the same time.

Using AVAudioPlayer and MPMoviePlayerController simultaneously

I need to use AVAudioPlayer and MPMoviePlayerController simultaneously i.e play a movie while the background loop is playing is it possible.
My bg music loop stops working when a movie starts playing , so I tried to stop the bg music loop and start the movie and when movie stops, start playing the bg loop again but this is also is not working.
In fact it is achievable from 3.x onwards. I had once done this in one of my applications and its got nothing to do with any backgrounding or any other new iOS features. You just need to configure the audio sessions properly. If you make sure that both your audio player as well as the video player are using the ambient audio session it is possible.
Prior to starting your AVAudioPlayer to play, set the category of audio session to AVAudioSessionCategoryAmbient. This is the system wide common audio session. This allows other sounds to play along such as the sound from the iPod application.
In case you are using the MPMoviePlayerController from iOS 3.2 or above make sure you set the useApplicationAudioSession to NO. This ensures other sounds to play along. It is the default in earlier versions of iOS.
There is a limitation in the current iPhone SDK (2.x and 3.x) that does not allow any background audio playing while a movie is playing in the MPMoviePlayerController API. Unfortunately, it is not possible to do what you are asking at this time within the iPhone SDK.
I would suggest logging an enhancement request to Apple to offer this in a future version of the SDK.