Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24476] iOS: Audio recording when exiting the app.

GitHub Issuen/a
TypeStory
PriorityHigh
StatusClosed
ResolutionNot Our Bug
Resolution Date2017-03-25T15:49:43.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
Labelsn/a
ReporterMotiur Rahman
AssigneeHans Knöchel
Created2017-03-11T17:03:19.000+0000
Updated2017-05-31T22:24:56.000+0000

Description

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.

Attachments

FileDateSize
test_media.zip2017-03-12T18:02:49.000+00002545123

Comments

  1. Hans Knöchel 2017-03-12

    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:
       <key>NSMicrophoneUsageDescription</key>
       <string>Can we use your microphone?</string>
       <key>UIBackgroundModes</key>
       <array>
           <string>audio</string>
       </array>
       
    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).
  2. Tamir Ishay Sharbat 2017-03-13

    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
  3. Hans Knöchel 2017-03-13

    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).
  4. Tamir Ishay Sharbat 2017-03-15

    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?
  5. Hans Knöchel 2017-03-15

    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.
  6. Tamir Ishay Sharbat 2017-03-15

    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
       <?xml version="1.0" encoding="UTF-8"?>
       <ti:app xmlns:ti="http://ti.appcelerator.org">
           <id>com.tamir.wakeapp</id>
           <name>wakeapp</name>
           <version>1.0</version>
           <publisher>tamir</publisher>
           <url>undefined</url>
           <description>undefined</description>
           <copyright>2016 by tamir</copyright>
           <icon>appicon.png</icon>
           <fullscreen>false</fullscreen>
           <navbar-hidden>false</navbar-hidden>
           <analytics>true</analytics>
           <guid>996fbe63-xxxx-xxxx-xxxx-49ba79a8eee9</guid>
           <property name="ti.ui.defaultunit" type="string">dp</property>
           <ios>
               <enable-launch-screen-storyboard>true</enable-launch-screen-storyboard>
               <plist>
                   <dict>
                       <key>NSLocationAlwaysUsageDescription</key>
                       <string>we need to keep track of your location to wake you up when you arrive at your destination</string>
                       <key>NSMicrophoneUsageDescription</key>
                       <string>because</string>
                       <key>NSAppleMusicUsageDescription</key>
                       <string>enable if you wish to use songs from your music library</string>
                       <key>UISupportedInterfaceOrientations</key>
                       <array>
                           <string>UIInterfaceOrientationPortrait</string>
                       </array>
                       <key>UIBackgroundModes</key>
                       <array>
                           <string>audio</string>
                       </array>
                       <key>UIRequiresPersistentWiFi</key>
                       <false/>
                       <key>UIPrerenderedIcon</key>
                       <false/>
                       <key>UIStatusBarHidden</key>
                       <false/>
                       <key>UIStatusBarStyle</key>
                       <string>UIStatusBarStyleDefault</string>
                   </dict>
               </plist>
           </ios>
           <android xmlns:android="http://schemas.android.com/apk/res/android"/>
           <mobileweb>
               <precache/>
               <splash>
                   <enabled>true</enabled>
                   <inline-css-images>true</inline-css-images>
               </splash>
               <theme>default</theme>
           </mobileweb>
           <modules>
               <module platform="commonjs">ti.cloud</module>
           </modules>
           <deployment-targets>
               <target device="android">false</target>
               <target device="ipad">false</target>
               <target device="iphone">true</target>
               <target device="mobileweb">false</target>
               <target device="windows">false</target>
           </deployment-targets>
           <sdk-version>6.0.3.v20170314141715</sdk-version>
           <plugins>
               <plugin version="1.0">ti.alloy</plugin>
           </plugins>
       </ti:app>
       
       
    When I said try multiple times I meant it.
  7. Tamir Ishay Sharbat 2017-03-17

    have you experienced the bug yet?
  8. Motiur Rahman 2017-03-24

    Any updates on it?
  9. Hans Knöchel 2017-03-24

    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:
       var recorder = null;
       
       var win = Ti.UI.createWindow({
       	backgroundColor : 'green',
       	layout : "vertical"
       });
       
       win.addEventListener("open", function() {
       	if (!Ti.Media.hasAudioPermissions()) {
       		Ti.Media.requestAudioPermissions(function(e) {
       			if (!e.success) {
       				alert("Audio Recording Permissions not granted. Skipping!");
       				return;
       			}		
       			
       			Ti.API.info('Audio Recorder Permissions now granted!');
       			initializeAudioEvents();
       		});
       	} else {
       		Ti.API.info('Audio Recorder Permissions already granted!');
       		initializeAudioEvents();
       	}
       });
        
       win.open();
       
       function initializeAudioEvents() {
       	recorder = Ti.Media.createAudioRecorder();
       
       	Ti.App.addEventListener("resumed", onResume);
       	Ti.App.addEventListener("pause", onPause);
       }
        
       function onResume() {
       	if (!recorder) {
       		return;
       	}
        
       	recorder.stop();
        
       	Ti.Media.audioSessionCategory = Ti.Media.AUDIO_SESSION_CATEGORY_PLAYBACK;
       	
       	Ti.API.warn("category: " + Ti.Media.audioSessionCategory);
       	Ti.API.warn("playback: " + Ti.Media.AUDIO_SESSION_CATEGORY_PLAYBACK);
       	
       	Ti.API.info("onResume");
        
       }
        
       function onPause() {
       	if (!recorder) {
       		return;
       	}
        
       	Ti.Media.audioSessionCategory = Ti.Media.AUDIO_SESSION_CATEGORY_PLAY_AND_RECORD;
        
       	Ti.API.warn("category: " + Ti.Media.audioSessionCategory);
           Ti.API.warn("play_and_record: " + Ti.Media.AUDIO_SESSION_CATEGORY_PLAY_AND_RECORD);
           
       	recorder.start();
       	Ti.API.info("onPause");
       }
       
    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 to DebugLog(@"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.
  10. Hans Knöchel 2017-03-24

    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!
  11. Tamir Ishay Sharbat 2017-03-25

    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.
  12. Hans Knöchel 2017-03-25

    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!
  13. Lee Morris 2017-05-31

    Closing this issue as the problem is not our bug.

JSON Source