Skip to content

Instantly share code, notes, and snippets.

@yec
Created February 4, 2012 05:23
Show Gist options
  • Save yec/1735575 to your computer and use it in GitHub Desktop.
Save yec/1735575 to your computer and use it in GitHub Desktop.
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
var SR = {};
var BUTTONS_COUNT = 48;
var buttons = [];
var sounds = {};
//
// create base UI tab and root window
//
var boardWindow = Titanium.UI.createWindow({
backgroundColor:'#fff',
layout: 'horizontal'
});
for (var i = 0; i < BUTTONS_COUNT; i++) {
var button = Titanium.UI.createButton({
height: 50,
width: 50,
});
button.addEventListener('click', function(e) {
Ti.API.log(i);
if (sounds[i]) {
sound = Titanium.Media.createSound({sound:sounds[i]});
sound.play();
} else {
SR.createSoundRecord(i).open();
}
});
buttons.push(button);
boardWindow.add(button);
}
boardWindow.open();
(function () {
SR.createSoundRecord = function (index) {
var win = Ti.UI.createWindow({
title: 'Record',
backgroundColor: 'white',
barColor: '#83389b',
});
Titanium.Media.audioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAY_AND_RECORD;
var recording = Ti.Media.createAudioRecorder();
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 = '';
//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();
sounds[index] = file;
//Codestrong.attachmentType = Thread.ATTACHMENT_TYPE_AUDIO;
//Codestrong.attachment = Ti.Filesystem.getFile(file.path).read();
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
});
var save = Titanium.UI.createButton({
title:'Save',
width:200,
height:40,
top:140
});
save.addEventListener('click', function(){
win.close();
});
win.add(b2);
win.add(save);
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);
return win;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment