tuttelikz

Activity_main [v12+]

Oct 24th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.03 KB | None | 0 0
  1. package com.example.iptea.audio22;
  2.  
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.media.AudioFormat;
  6. import android.media.AudioManager;
  7. import android.media.AudioTrack;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.widget.ImageView;
  13. import android.widget.SeekBar;
  14.  
  15. import com.hanks.htextview.base.HTextView;
  16. import com.sdsmdg.harjot.crollerTest.Croller;
  17. import com.skyfishjy.library.RippleBackground;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21.     Thread t; //audio processing thread
  22.     int sr = 44100; //sampling rate
  23.     boolean isRunning = true; //audio on off
  24.     //SeekBar fSlider;
  25.     Croller croller;
  26.     double sliderval;
  27.     public double fr = 440.f;
  28.     public int counter = 0;
  29.     public double max_frequency = 0;
  30.     ImageView finishBut;
  31.     @Override
  32.  
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_main);
  36.  
  37.         final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
  38.         final HTextView animText = (HTextView)findViewById(R.id.animatedText);
  39.         final HTextView animText2 = (HTextView)findViewById(R.id.animatedText2);
  40.  
  41.         finishBut = (ImageView) findViewById(R.id.finishButton);
  42.         ImageView kulak=(ImageView)findViewById(R.id.centerImage);
  43.  
  44.         croller = (Croller) findViewById(R.id.croller);
  45.  
  46.         kulak.setOnTouchListener(new View.OnTouchListener() {
  47.             @Override
  48.             public boolean onTouch(View v, MotionEvent event) {
  49.                 String both;
  50.                 String both2;
  51.  
  52.                 switch (event.getAction()) {
  53.                     case MotionEvent.ACTION_DOWN:
  54.  
  55.                         isRunning = true;
  56.  
  57.                         rippleBackground.startRippleAnimation();
  58.                         t = new Thread() {
  59.                             public void run() {
  60.                                 int buffsize = AudioTrack.getMinBufferSize(sr,
  61.                                         AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  62.                                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  63.                                         sr, AudioFormat.CHANNEL_OUT_MONO,
  64.                                         AudioFormat.ENCODING_PCM_16BIT, buffsize,
  65.                                         AudioTrack.MODE_STREAM);
  66.  
  67.                                 short samples[] = new short[buffsize];  //audio synthesis
  68.                                 int amp = 10000;
  69.  
  70.                                 double twopi = 8.*Math.atan(1.);
  71.                                 double ph = 0.0;
  72.                                 audioTrack.play();      //audio running
  73.  
  74.                                 while(isRunning){   //synthesis loop
  75.  
  76.                                     sliderval = counter*0.5 / croller.getMax();
  77.                                     /*fr =  440 + 440*sliderval;*/
  78.                                     fr =  440 + 440*sliderval;
  79.  
  80.                                     for(int i=0; i < buffsize; i++){
  81.                                         samples[i] = (short) (amp*Math.sin(ph));
  82.                                         ph += twopi*fr/sr;
  83.                                     }
  84.                                     audioTrack.write(samples, 0, buffsize);
  85.  
  86.                                     runOnUiThread(new Runnable() {
  87.                                         @Override
  88.                                         public void run() {
  89.                                             if (( counter % 10 ) == 0) {
  90.                                                 //fSlider.setProgress(counter/89);  // counter/100
  91.                                                 croller.setProgress(counter/89);
  92.                                                 counter = counter + 100;
  93.                                             }
  94.                                         }
  95.                                     });
  96.  
  97.                                     if (fr > 20000) {
  98.                                         counter = 0;
  99.                                     }
  100.                                 }
  101.                                 counter = 0;
  102.  
  103.                                 audioTrack.stop();
  104.                                 audioTrack.release();
  105.                             }
  106.                         };
  107.  
  108.                         t.start();
  109.                         return true;
  110.                     //m, k
  111.                     case MotionEvent.ACTION_UP:
  112.  
  113.                         if (fr > max_frequency) {
  114.                             max_frequency = fr;
  115.                             if (max_frequency < 10000) {
  116.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  117.                                 both2 = "Try again :)";
  118.                             }
  119.                             else if (max_frequency > 10000 && max_frequency <15000) {
  120.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  121.                                 both2 = "Time to see a doctor :)";
  122.                             }
  123.                             else {
  124.                                 both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  125.                                 both2 = "Not bad :)";
  126.                             }
  127.                             animText.animateText(both);
  128.                             animText2.animateText(both2);
  129.                         }
  130.                         isRunning = false;
  131.                         counter = 0;
  132.                         t = null;
  133.                         rippleBackground.stopRippleAnimation();
  134.  
  135.                         return true;
  136.                 }
  137.  
  138.                 return false;
  139.             }
  140.         });
  141.     }
  142.     public void lastpage(View v) {
  143.         //String strname = urname.getText().toString();
  144.         //String stremail = uremail.getText().toString();
  145.  
  146. /*        if (strname.equals("")||stremail.equals("")) {
  147.             Toast.makeText(getBaseContext(), "Enter both name and email", Toast.LENGTH_SHORT).show();
  148.         }else {
  149.             Intent in = new Intent(LoginActivity.this, VoiceActivity.class);
  150.             Bundle bundle = new Bundle();
  151.             bundle.putString("urname",strname);
  152.             bundle.putString("uremail",stremail);
  153.             in.putExtras(bundle);
  154.             startActivity(in);
  155.         }*/
  156.         Intent initi = new Intent(MainActivity.this, ResultActivity.class);
  157. /*        Bundle bundle = new Bundle();
  158.         bundle.putString("urname",strname);
  159.         bundle.putString("uremail",stremail);
  160.         in.putExtras(bundle);*/
  161.         startActivity(initi);
  162.     }
  163. /*    public void onDestroy(){
  164.         super.onDestroy();
  165.         isRunning = false;
  166.         try {
  167.             t.join();
  168.         } catch (InterruptedException e) {
  169.             e.printStackTrace();
  170.         }
  171.         t = null;
  172.     }*/
  173. }
Advertisement
Add Comment
Please, Sign In to add comment