description
Is it possible to activate the audio recorder when exiting the App? (pressed the home button for exiting the app)
I have tried with the following test code but it's not working.
var win = Ti.UI.createWindow({
backgroundColor : 'green',
layout : "vertical"
});
win.open();
function onResume() {
recorder.stop();
Ti.Media.audioSessionCategory = Ti.Media.AUDIO_SESSION_CATEGORY_PLAYBACK;
Ti.API.info("onReasume");
}
function onPause() {
Ti.Media.audioSessionCategory = Ti.Media.AUDIO_SESSION_CATEGORY_PLAY_AND_RECORD;
recorder.start();
Ti.API.info("onPause");
}
var recorder = Ti.Media.createAudioRecorder();
Ti.App.addEventListener("resumed", onResume);
Ti.App.addEventListener("pause", onPause);
If I press the home button then reopen it, several times, it shows this error.
[ERROR] : Script Error {
[ERROR] : column = 15;
[ERROR] : line = 63;
[ERROR] : message = "+[NSBlock boundBridge:withKrollObject:]: unrecognized selector sent to class 0x1a91ccd58";
[ERROR] : sourceURL = "file:///var/containers/Bundle/Application/03E1B455-31D7-4FB0-8509-CF99D1F972D8/commonJSApp.app/app.js";
[ERROR] : stack = "[native code]\nonResume@file:///var/containers/Bundle/Application/03E1B455-31D7-4FB0-8509-CF99D1F972D8/commonJSApp.app/app.js:63:15";
[ERROR] : }
Thanks.
The error indicates a problem in line 64, but the test-case only has 31 lines. Please check! *EDIT*: I am able to reproduce this issue. Putting it in the current sprint and 6.0.3 release version for now. If possible, it would be very helpful to know if this used to work with 5.5.1.GA. *EDIT 2*: The error did not happen to me when using the latest 6.0.3 build together with the following tiapp.xml properties:
I also attached my example project that's based on your code. It works fine now and the red status bar appears to indicate the background recording to the user (that's native).
Hans can you run it multiple times... sometimes it works and sometimes it doesn't. Run it 5 times (rebuild every time and exit the app from the task manager) on/off liveview (important that you try both) if you never get a "Could not activate session" message then it's solved. otherwise please inform me
Then it's solved :-) I received the message before, but after doing the above changes, it works now. You did test the changes and checked my example project right? And you can get the latest 6.0.3 build from [here](http://builds.appcelerator.com/#6_0_X) (release this / next week).
hans I checked it again with sdk version 6.0.3.v20170314141715 and I am still getting [INFO] : Could not activate session when trying to activate the audio recorder when exiting the app can you please look into that?
You ensured to included the above keys into your ios section of the tiapp.xml? Then I'd like to request the tiapp.xml to clarify.
yes! of course I included them. you may not get the bug because this behavior is incosistent (most times it works sometimes it doesn't) here is the tiapp.xml so you
When I said try multiple times I meant it.
have you experienced the bug yet?
Any updates on it?
Hey Tamir, I am still not getting the error any more when using the background-modes like in the example project. I also cleaned up the event-handling of the app.js to only register the events when the audio permissions are granted properly. Try this please:
And for better debugging, please go to
~/Library/Application Support/Titanium/mobilesdk/osx/6.0.3.GA/iphone/Classes/TiMediaAudioSession.m
and change the line 45 toDebugLog(@"Could not activate session: %@ (%ld)", error.localizedDescription, error.code);
, so we can see what's the actual error and code. Thx! P.S.: Also note that you have to know that when you force-quit the app and open it again, it will of course create a new recorder instance to record, so you should really be careful with adding the global events. I am not sure how this is natively done, but you could probably store the current audio session ID and check if that one is currently running before creating a new recorder. I hope that's clear to understand.I was just able to reproduce the error and received the internal error code
561015905
, which means that the audio session could not be activated from the background. Digging more into it, it looks like you cannot start audio sessions from the background (especially not when the app gets closed). See [this](https://forums.developer.apple.com/thread/38917) and [this](https://developer.apple.com/reference/avfoundation/avaudiosessionerrorcode/avaudiosessionerrorcodecannotstartplaying) link for more technical details from Apple directly. So I have to correct myself: You can record when the app is paused (not terminated), but only when you start the recording in the foreground already. The background modes are for that state, but also primarily for *playing* audio, which also works when the app is terminated. I hope that helps a bit. Please feel free to get in touch with Apple for more detailed API behaviors, thanks!thanks Hans, good to know that. I have question that isn't entirely on subject. Do you have any idea about the kind of background mode sleep cycle is using? (it's not audio recording I checked by disabling the microphone for the app and this still works. plus when I use the microphone in the background the lock screen looks differently than what it looks like when sleep cycle is in the background.) If not, do you have any idea about who can I talk to about that? thanks in advance, Tamir.
Hey Tamir, that's a very general iOS question. I'd ask a new question on StackOverflow, there are audio-related experts that will be able to give you a solid answer on that, I'd need to do some more research to do the same. Thanks man!
Closing this issue as the problem is not our bug.