Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-10481] iOS: Audio recording functionality only works up-to SDK v2.0.2 GA

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionInvalid
Resolution Date2012-08-20T11:17:49.000+0000
Affected Version/sRelease 2.1.0, Release 2.1.1
Fix Version/sn/a
ComponentsiOS
Labelsapi
ReporterNikhil Sharma
AssigneeSabil Rahim
Created2012-08-16T18:18:22.000+0000
Updated2017-03-09T23:10:49.000+0000

Description

Audio recording functionality doesn't work after the Ti SDK 2.0.2.GA.

Repo Steps

1. Run the below code in your app.js 2. Open the application and you can't record the audio.
var win = Titanium.UI.createWindow();

Titanium.Media.audioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAY_AND_RECORD;
var recording = Ti.Media.createAudioRecorder();
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({sound: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();

Comments

  1. Blain Hamon 2012-08-17

    The support link hints to an exception raised by the audio driver within simulator. Is this the case in the TIMOB? If so, this may be an Apple bug in simulator. https://discussions.apple.com/thread/3819439?start=0&tstart=0 http://pastebin.com/jqfPcA8g http://www.openradar.me/10555404
  2. Neeraj Gupta 2012-08-17

    @Nikhil - Please confirm if the customer encounters this issue with the simulator or the device. This is an Apple bug if it occurs only on simulator.
  3. Sabil Rahim 2012-08-20

    It seems like the recording works fine on both simulator and on device . Though the test case is wrong, when trying to playback the recorded file they are trying to create a media sound file by passing sound property the recorded file. The sound property doesnot exist in our platform and instead url property should be used. try using the following code .
       //At line 178
       sound = Titanium.Media.createSound({url:file});
       
    Tested on: Xcode 4.4 iOS Sim . 4.3 and 5.0 device : iPhone 4S running 5.1 TiSDK : 2.2.0(master||commit:f51fd8cafaa84541470d1687c0e1aa9620289c8c) as on 08.20.2012 Marking ticket as invalid.
  4. Lee Morris 2017-03-09

    Closing ticket as invalid.

JSON Source