Advertisement
Guest User

DroidScriptSoundRecorderCode

a guest
Oct 30th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. //Create global variables.
  2. var destFile = "/sdcard/rec.wav"
  3. var timer = 0;
  4.  
  5. //Called when application is started.
  6. function OnStart()
  7. {
  8. //Create a layout with objects vertically centered.
  9. lay = app.CreateLayout( "linear", "VCenter,FillXY" );
  10.  
  11. //Create a text control to show data log.
  12. txt = app.CreateText( "Level: 0%", 0.8 );
  13. txt.SetTextColor( "#222222" );
  14. txt.SetTextShadow( 1, 0,1, "#88ffffff" );
  15. txt.SetTextSize( 24 );
  16. lay.AddChild( txt );
  17.  
  18. //Create a 'Start' button.
  19. btnStart = app.CreateButton( "Start", 0.6, -1);
  20. btnStart.SetMargins( 0, 0.05, 0, 0 );
  21. btnStart.SetOnTouch( btnStart_OnTouch );
  22. lay.AddChild( btnStart );
  23.  
  24. //Create a 'Pause' button.
  25. btnPause = app.CreateButton( "Pause", 0.6, -1);
  26. btnPause.SetMargins( 0, 0.05, 0, 0 );
  27. btnPause.SetOnTouch( btnPause_OnTouch );
  28. lay.AddChild( btnPause );
  29.  
  30. //Create a 'Stop' button.
  31. btnStop = app.CreateButton( "Stop", 0.6, -1);
  32. btnStop.SetMargins( 0, 0.05, 0, 0 );
  33. btnStop.SetOnTouch( btnStop_OnTouch );
  34. lay.AddChild( btnStop );
  35.  
  36. //Create a 'Play' button.
  37. btnPlay = app.CreateButton( "Play File", 0.6, -1);
  38. btnPlay.SetMargins( 0, 0.1, 0, 0 );
  39. btnPlay.SetOnTouch( btnPlay_OnTouch );
  40. lay.AddChild( btnPlay );
  41.  
  42. //Add layout to app.
  43. app.AddLayout( lay );
  44. notify1 = app.CreateNotification();
  45.  
  46.  
  47. //Create Audio Recorder and set dest file.
  48. rec = app.CreateAudioRecorder();
  49. rec.SetFile( destFile );
  50.  
  51. //Create media player.
  52. player = app.CreateMediaPlayer();
  53. player.SetOnReady( player_OnReady );
  54.  
  55. //Switch off debug so we don't fill the log.
  56. app.SetDebugEnabled( false );
  57. }
  58.  
  59. //Called when user touches our 'Start' button.
  60. function btnStart_OnTouch()
  61. {
  62. rec.Start();
  63. txt.SetTextColor( "#aa2222" );
  64. clearInterval( timer );
  65. timer = setInterval( GetLevel, 250 );
  66. notify1.SetMessage( "You have a notification!", "SG SoundRecorder", "Recording..." );
  67. notify1.SetLights( "#00ffff", 500, 500 );
  68. notify1.Notify();
  69. }
  70. //Called when user touches our 'Pause' button.
  71. function btnPause_OnTouch()
  72. {
  73. rec.Pause();
  74. txt.SetTextColor( "#222222" );
  75. notify1.SetMessage( "You have a notification!", "SG SoundRecorder", "Recording Paused!" );
  76. notify1.SetLights( "#00ffff", 500, 500 );
  77. notify1.Notify();
  78. }
  79.  
  80. //Called when user touches our 'Stop' button.
  81. function btnStop_OnTouch()
  82. {
  83. rec.Stop();
  84. txt.SetTextColor( "#222222" );
  85. txt.SetText( "Level: 0%" );
  86. clearInterval( timer );
  87. notify1.Cancel();
  88. app.ShowProgressBar( "Saving...", 0 );
  89. setTimeout( "Update(10)", 1000 );
  90. setTimeout( "Update(30)", 2000 );
  91. setTimeout( "Update(40)", 3000 );
  92. setTimeout( "Update(60)", 4000 );
  93. setTimeout( "Update(100)", 5000 );
  94. setTimeout( "Hide()", 6000 );
  95. }
  96.  
  97. //Get the current RMS signal level.
  98. //(A max of about 0.7 for sine waves)
  99. function GetLevel()
  100. {
  101. var level = rec.GetRMS();
  102. txt.SetText( "Level: " + Math.round(100*level/0.72) + "%" );
  103. }
  104.  
  105. //Called when user touches our 'Play' button.
  106. function btnPlay_OnTouch()
  107. {
  108. player.SetFile( destFile );
  109. notify1.SetMessage( "You have a notification!", "SG SoundRecorder", "Playing your Record..." );
  110. notify1.SetLights( "#00ffff", 500, 500 );
  111. notify1.Notify();
  112. }
  113.  
  114. //Called when the WAV file is loaded.
  115. function player_OnReady()
  116. {
  117. player.Play();
  118. }
  119.  
  120. function Update( progress )
  121. {
  122. app.UpdateProgressBar( progress );
  123. }
  124.  
  125. //Hide the progress bar.
  126. function Hide()
  127. {
  128. app.HideProgressBar();
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement