Created
October 19, 2015 00:28
-
-
Save addieljuarez/8ab8852abf2660c07669 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Cloud = require('ti.cloud'); | |
var currentSessionMode = Ti.Media.audioSessionCategory; | |
Ti.Media.audioSessionCategory = Ti.Media.AUDIO_SESSION_CATEGORY_PLAY_AND_RECORD; | |
var record = false; | |
var file; | |
var idSound; | |
var window = Ti.UI.createWindow({ | |
backgroundColor:'white', | |
layout:'vertical', | |
}); | |
var labelLogin = Ti.UI.createLabel({ | |
top:40, | |
color:'#000', | |
textAlign:Ti.UI.TEXT_ALIGNMENT_CENTER, | |
text:'not login', | |
}); | |
window.add(labelLogin); | |
var buttonRecordSound = Ti.UI.createButton({ | |
top:20, | |
title:'record', | |
height:50, | |
width:200, | |
borderColor:'#000', | |
borderRadius:15, | |
}); | |
window.add(buttonRecordSound); | |
var progressBarUploadSound = Ti.UI.createProgressBar({ | |
top:20, | |
left:10, | |
right:10, | |
height:50, | |
min:0, | |
max:100, | |
value:0, | |
message:'Uploading Sound', | |
color:'#888', | |
}); | |
window.add(progressBarUploadSound); | |
progressBarUploadSound.show(); | |
var buttonSound = Ti.UI.createButton({ | |
top:20, | |
width:200, | |
height:50, | |
title:'sound play', | |
borderColor:'#000', | |
borderRadius:15, | |
touchEnabled:false, | |
}); | |
window.add(buttonSound); | |
var progressbarDownload = Ti.UI.createProgressBar({ | |
top:20, | |
left:10, | |
right:10, | |
height:50, | |
min:0, | |
max:100, | |
value:0, | |
message:'Download Sound', | |
color:'#888', | |
}); | |
window.add(progressbarDownload); | |
progressbarDownload.show(); | |
window.open(); | |
Cloud.Users.login({ | |
login: 'yourUser', //created in Cloud | |
password: 'yourPassword' //created in Cloud | |
}, function (e) { | |
if (e.success) { | |
var user = e.users[0]; | |
labelLogin.setText('login ' + user.first_name + ' ' + user.last_name); | |
} else { | |
alert('Error:\n' + | |
((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
var recordSound = Ti.Media.createAudioRecorder({ | |
compression:Ti.Media.AUDIO_FORMAT_ULAW, | |
format:Ti.Media.AUDIO_FILEFORMAT_WAVE, | |
}); | |
buttonRecordSound.addEventListener('click', function(e){ | |
if(!record){ | |
recordSound.start(); | |
Ti.Media.startMicrophoneMonitor(); | |
buttonRecordSound.setTitle('stop and upolad'); | |
record = true; | |
}else{ | |
file = recordSound.stop(); | |
Ti.Media.stopMicrophoneMonitor(); | |
buttonRecordSound.setTitle('record'); | |
buttonRecordSound.touchEnabled = false; | |
record = false; | |
Cloud.Objects.create({ | |
classname: 'sounds', | |
fields: { | |
make: Ti.Utils.base64encode(file.toBlob()).toString(), | |
date: new Date(), | |
} | |
}, function (e) { | |
if (e.success) { | |
var sound = e.sounds[0]; | |
idSound = sound.id; | |
buttonRecordSound.touchEnabled = true; | |
progressBarUploadSound.value = e.progress; | |
buttonSound.touchEnabled = true; | |
alert('upload complete'); | |
} else { | |
buttonRecordSound.touchEnabled = true; | |
alert('Error:\n' + | |
((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} | |
}); | |
var sound1 = Ti.Media.createSound(); | |
buttonSound.addEventListener('click', function(e){ | |
Cloud.Objects.query({ | |
classname: 'sounds', | |
page: 1, | |
per_page: 10, | |
where: { | |
id: idSound | |
} | |
}, function (e) { | |
if (e.success) { | |
for (var i = 0; i < e.sounds.length; i++) { | |
var sound = e.sounds[i]; | |
progressbarDownload.value = e.progress; | |
var sonido = Ti.Utils.base64decode(sound.make); | |
var f = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'recording'); | |
if (f.exists()) { | |
f.deleteFile(); | |
} | |
f.write(sonido); | |
sound1.url = f.nativePath; | |
sound1.play(); | |
} | |
} else { | |
alert('Error:\n' + | |
((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
}); | |
sound1.addEventListener('complete', function(e){ | |
alert('ending sound'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment