Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24140] iOS: Ti.Media.audioSessionCategory not working with Ti.Media.Sound

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2016-11-28T22:14:58.000+0000
Affected Version/sRelease 6.0.0
Fix Version/sRelease 6.0.1
ComponentsiOS
Labelsaudiosessioncategory, background, ios, qe-6.0.1, sound
ReporterHans Knöchel
AssigneeHans Knöchel
Created2016-11-11T17:05:52.000+0000
Updated2019-11-26T19:59:46.000+0000

Description

Thanks to [~designbymind], he reported an issue via mail that indicates a problem with our Ti.Media.AUDIO_SESSION_CATEGORY_* constants. As of TIMOB-23925, we wrapped our Ti.Media constants and in [this line](https://github.com/appcelerator/titanium_mobile/pull/8456/files#diff-e19e566138e39044280d74cfee0d3b4eR217), I wrapped it to only be compiled when the Ti.Media.AudioPlayer API is defined. But as it turns out, you can also use them for the Ti.Media.Sound API, so we just need to add that statement as well.

Attachments

FileDateSize
test_file.mp32016-11-11T19:12:20.000+00001430174

Comments

  1. Hans Knöchel 2016-11-11

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/8600 PR (6_0_X): https://github.com/appcelerator/titanium_mobile/pull/8601 Test-case 1:
       var win = Ti.UI.createWindow({
           backgroundColor: "#fff"
       });
       
       var btn = Ti.UI.createButton({
           title: "Print AUDIO_SESSION_CATEGORY_AMBIENT"
       });
       
       btn.addEventListener("click", function() {
           // Create a Ti.Sound instance so the constant gets compiled
           var sound = Ti.Media.createSound();
       
           // Should print "AVAudioSessionCategoryAmbient"
           Ti.API.info(Ti.Media.AUDIO_SESSION_CATEGORY_AMBIENT);
       });
       
       win.add(btn);
       win.open();
       
    Test-case 2:
        Ti.Media.setAudioSessionCategory(Ti.Media.AUDIO_SESSION_CATEGORY_AMBIENT);
       
       // --- returns undefined on device ---
       // --- returns AudioSessionCategoryAVAudioSessionCategoryAmbient on simulator ---
       Ti.API.info("AudioSessionCategory: " + Ti.Media.getAudioSessionCategory());
       
       var window = Ti.UI.createWindow();
       
       var playSoundButton = Ti.UI.createButton({
           title: "Play Sound"
       });
       
       // --- Sound object creation will stop music player (Spotify/Apply Music) upon [initial] app
       var sound = Ti.Media.createSound({
           url: "test_file.mp3",
           volume: 1,
           preload: false
       });
       
       playSoundButton.addEventListener("click", function() {
           // --- Playing the sound will also stop music player (same as sound creation)
           sound.play();
       });
       
       window.add(playSoundButton);
       window.open();
       
  2. Harry Bryant 2016-11-24

    Tested the various audioSessionCategory constants available, and found the following results:

    Ti.Media.AUDIO_SESSION_CATEGORY_AMBIENT:

    _Also used for for 'play along' style applications, such a virtual piano that a user plays over iPod audio._ *RESULT:* - Spotify Music does not pause when app is opened or when Sound is played. (/) - AudioSessionCategory: AVAudioSessionCategoryAmbient (/)

    Ti.Media.AUDIO_SESSION_CATEGORY_RECORD:

    _Session mode for recording audio; it silences playback audio._ - Spotify Music is paused when app is opened. (/) - Sound does not play. (/) - AudioSessionCategory: AVAudioSessionCategoryRecord (/)

    Ti.Media.AUDIO_SESSION_CATEGORY_SOLO_AMBIENT:

    _When you use this category, audio from built-in applications, such as the iPod, is silenced. Your audio is silenced when the Ring/Silent switch is set to silent or when the screen locks._ *RESULT:* Spotify Music pauses when App is opened or when Sound is played. (/) Audio is silenced when the Ring/Silent switch is set to Silent. (/) Audio is silenced when screen is locked. (/) AudioSessionCategory: AVAudioSessionCategorySoloAmbient (/)

    Ti.Media.AUDIO_SESSION_CATEGORY_PLAYBACK:

    _When using this mode, your application audio continues with the Ring/Silent switch set to silent or when the screen locks. This property normally disallows mixing iPod audio with application audio._ *RESULT:* Spotify Music pauses when App is opened or when sound is played. (/) Playback audio continues when device’s Ring/Silent switch is set to Silent. (/) Playback is silenced when screen is locked (x) Playback is silenced when app is backgrounded. (x) AudioSessionCategory: AVAudioSessionCategoryPlayback (/) *Reopening this issue* as Ti.Media.AUDIO_SESSION_CATEGORY_PLAYBACK does not meet the expected behaviour as described in the documentation. Tested On: iPhone 6 Plus 10.1.1 Device iPhone 5S 9.3.5 Device Mac OS Sierra (10.12.1) Ti SDK: 6.0.1.v20161121005220 Appc Studio: 4.8.0.201611121409 Appc NPM: 4.2.8 App CLI: 6.0.0 Xcode 8.1 Node v4.4.7 *EDIT:* After further investigation it was found that in order to allow background audio for iOS, the following needs to be specified in the of the tiapp.xml:
                       <key>UIBackgroundModes</key>
                   	<array>
                   		<string>audio</string>
                   	</array>
       
    It does not seem that this instruction is present in our documentation for Ti.Media.Sound or Ti.Media.AudioPlayer.
  3. Marian Kucharcik 2019-11-26

    Hi guys, I'm experiencing problems with background audio playing on iOS13.2.3 - on older versions(13.1 and 12.x.x) it was fine and working. Now, I can anything but when my app minimizes, audio stops(the goal is to start playing after local notification is received). Phone makes 0.5seconds sound and then it's silence. When I maximize app(go foreground), music plays(it seems that notification starts player to play, but no sound is comming from speaker when in background, after going foreground player plays not from start, but continues...) Can you please look at it? I tried AUDIO_SESSION_CATEGORY_PLAYBACK and AUDIO_SESSION_CATEGORY_AMBIENT, but no change... Thanks

JSON Source