[TIMOB-14396] iOS7: AudioToolbox changes
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | High |
| Status | Closed |
| Resolution | Fixed |
| Resolution Date | 2013-09-13T22:06:43.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | 2013 Sprint 18, 2013 Sprint 18 API, Release 3.1.3, Release 3.2.0 |
| Components | iOS |
| Labels | ios7, module_media, qe-closed-3.1.3, qe-testadded |
| Reporter | Shannon Hicks |
| Assignee | Sabil Rahim |
| Created | 2013-06-26T19:14:02.000+0000 |
| Updated | 2013-09-13T22:06:56.000+0000 |
Description
There are changes to AudioToolbox with iOS 7. I noticed this because an app I'm building with Ti 3.1.1 and iOS SDK 6 is not properly playing audio when I set:
Ti.Media.audioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAY_AND_RECORD;
var sound = Ti.Media.createSound({
url:soundFile.getNativePath(),
allowBackground:false,
});
sound.play();
Also, with iOS 7 you now must request Mic access, so we'll need new methods or properties to deal with this.
Testing Code
var win = Titanium.UI.createWindow(); Ti.Media.requestAuthorization(function(e){ if (e.success) { startRecorder(); } else { alert('Access to Recorder is not allowed'); var label = Ti.UI.createLabel({ text:'Permission to use recorder is denied.' }); win.add(label); } }); function startRecorder(){ Titanium.Media.audioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAY_AND_RECORD; var recording = Ti.Media.createAudioRecorder(); // default compression is Ti.Media.AUDIO_FORMAT_LINEAR_PCM // default format is Ti.Media.AUDIO_FILEFORMAT_CAF // this will give us a wave file with µLaw compression which // is a generally small size and suitable for telephony recording // for high end quality, you'll want LINEAR PCM - however, that // will result in uncompressed audio and will be very large in size recording.compression = Ti.Media.AUDIO_FORMAT_ULAW; recording.format = Ti.Media.AUDIO_FILEFORMAT_WAVE; Ti.Media.addEventListener('recordinginput', function(e) { Ti.API.info('Input availability changed: '+e.available); if (!e.available && recording.recording) { b1.fireEvent('click', {}); } }); var file; var timer; var sound; var label = Titanium.UI.createLabel({ text:'', top:150, color:'#999', textAlign:'center', width:'auto', height:'auto' }); win.add(label); function lineTypeToStr() { var type = Ti.Media.audioLineType; switch(type) { case Ti.Media.AUDIO_HEADSET_INOUT: return "headset"; case Ti.Media.AUDIO_RECEIVER_AND_MIC: return "receiver/mic"; case Ti.Media.AUDIO_HEADPHONES_AND_MIC: return "headphones/mic"; case Ti.Media.AUDIO_HEADPHONES: return "headphones"; case Ti.Media.AUDIO_LINEOUT: return "lineout"; case Ti.Media.AUDIO_SPEAKER: return "speaker"; case Ti.Media.AUDIO_MICROPHONE: return "microphone"; case Ti.Media.AUDIO_MUTED: return "silence switch on"; case Ti.Media.AUDIO_UNAVAILABLE: return "unavailable"; case Ti.Media.AUDIO_UNKNOWN: return "unknown"; } } var linetype = Titanium.UI.createLabel({ text: "audio line type: "+lineTypeToStr(), bottom:20, color:'#999', textAlign:'center', width:'auto', height:'auto' }); win.add(linetype); var volume = Titanium.UI.createLabel({ text: "volume: "+Ti.Media.volume, bottom:50, color:'#999', textAlign:'center', width:'auto', height:'auto' }); win.add(volume); Ti.Media.addEventListener('linechange',function(e) { linetype.text = "audio line type: "+lineTypeToStr(); }); Ti.Media.addEventListener('volume',function(e) { volume.text = "volume: "+e.volume; }); var duration = 0; function showLevels() { var peak = Ti.Media.peakMicrophonePower; var avg = Ti.Media.averageMicrophonePower; duration++; label.text = 'duration: '+duration+' seconds\npeak power: '+peak+'\navg power: '+avg; } var b1 = Titanium.UI.createButton({ title:'Start Recording', width:200, height:40, top:20 }); b1.addEventListener('click', function() { if (recording.recording) { file = recording.stop(); b1.title = "Start Recording"; b2.show(); pause.hide(); clearInterval(timer); Ti.Media.stopMicrophoneMonitor(); } else { if (!Ti.Media.canRecord) { Ti.UI.createAlertDialog({ title:'Error!', message:'No audio recording hardware is currently connected.' }).show(); return; } b1.title = "Stop Recording"; recording.start(); b2.hide(); pause.show(); Ti.Media.startMicrophoneMonitor(); duration = 0; timer = setInterval(showLevels,1000); } }); win.add(b1); var pause = Titanium.UI.createButton({ title:'Pause recording', width:200, height:40, top:80 }); win.add(pause); pause.hide(); pause.addEventListener('click', function() { if (recording.paused) { pause.title = 'Pause recording'; recording.resume(); timer = setInterval(showLevels,1000); } else { pause.title = 'Unpause recording'; recording.pause(); clearInterval(timer); } }); var b2 = Titanium.UI.createButton({ title:'Playback Recording', width:200, height:40, top:80 }); win.add(b2); b2.hide(); b2.addEventListener('click', function() { if (sound && sound.playing) { sound.stop(); sound.release(); sound = null; b2.title = 'Playback Recording'; } else { Ti.API.info("recording file size: "+file.size); sound = Titanium.Media.createSound({url:file}); sound.addEventListener('complete', function() { b2.title = 'Playback Recording'; }); sound.play(); b2.title = 'Stop Playback'; } }); var switchLabel = Titanium.UI.createLabel({ text:'Hi-fidelity:', width:'auto', height:'auto', textAlign:'center', color:'#999', bottom:115 }); var switcher = Titanium.UI.createSwitch({ value:false, bottom:80 }); switcher.addEventListener('change',function(e) { if (!switcher.value) { recording.compression = Ti.Media.AUDIO_FORMAT_ULAW; } else { recording.compression = Ti.Media.AUDIO_FORMAT_LINEAR_PCM; } }); win.add(switchLabel); win.add(switcher); } win.open();https://github.com/appcelerator/titanium_mobile/pull/4635 https://github.com/appcelerator/titanium_mobile/pull/4634
PR's merged
Doc PR's master - https://github.com/appcelerator/titanium_mobile/pull/4671 3_1_X - https://github.com/appcelerator/titanium_mobile/pull/4672
Tested the above code with: Appcelerator Studio, build: 3.1.3.201309132456 Titanium SDK, build:3.1.3.v20130912171547 Mac OS 10.8.4 Xcode 5 CLI: 3.1.2 Alloy: 1.2.2-beta Devices: iPad3 iOS7 GM seed iPhone5 iOS7 GM seed Verified the request to access the microphone, voice recording and playback.
reopened to correct comment
Verified fixed and working as expected. TiSDK 3.1.3.v20130913121549 CLI 3.1.2 GA TiStudio 3.1.3.201309132456 Tested on iPad Mini iOS7 Closing.