tuttelikz

MainActivity.java [v13+] without Circular Seekbar

Oct 1st, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.94 KB | None | 0 0
  1. package com.example.iptea.audio22;
  2.  
  3. import android.media.AudioFormat;
  4. import android.media.AudioManager;
  5. import android.media.AudioTrack;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.widget.ImageView;
  11. import android.widget.SeekBar;
  12.  
  13. import com.hanks.htextview.base.HTextView;
  14. import com.skyfishjy.library.RippleBackground;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18.     Thread t; //audio processing thread
  19.     int sr = 44100; //sampling rate
  20.     boolean isRunning = true; //audio on off
  21.     SeekBar fSlider;
  22.     double sliderval;
  23.     public double fr = 440.f;
  24.     public int counter = 0;
  25.     public double max_frequency = 0;
  26.     @Override
  27.  
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.  
  32.         final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
  33.         final HTextView animText = (HTextView)findViewById(R.id.animatedText);
  34.  
  35.         ImageView kulak=(ImageView)findViewById(R.id.centerImage);
  36.         kulak.setOnTouchListener(new View.OnTouchListener() {
  37.             @Override
  38.             public boolean onTouch(View v, MotionEvent event) {
  39.                 String both;
  40.  
  41.                 switch (event.getAction()) {
  42.                     case MotionEvent.ACTION_DOWN:
  43.  
  44.                         isRunning = true;
  45.                         fSlider = (SeekBar) findViewById(R.id.frequency);
  46.  
  47.                         rippleBackground.startRippleAnimation();
  48.                         t = new Thread() {
  49.                             public void run() {
  50.                                 int buffsize = AudioTrack.getMinBufferSize(sr,
  51.                                         AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  52.                                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  53.                                         sr, AudioFormat.CHANNEL_OUT_MONO,
  54.                                         AudioFormat.ENCODING_PCM_16BIT, buffsize,
  55.                                         AudioTrack.MODE_STREAM);
  56.  
  57.                                 short samples[] = new short[buffsize];  //audio synthesis
  58.                                 int amp = 10000;
  59.  
  60.                                 double twopi = 8.*Math.atan(1.);
  61.                                 double ph = 0.0;
  62.                                 audioTrack.play();      //audio running
  63.  
  64.                                 while(isRunning){   //synthesis loop
  65.  
  66.                                     sliderval = counter*0.5 / fSlider.getMax();
  67.                                     fr =  440 + 440*sliderval;
  68.  
  69.                                     for(int i=0; i < buffsize; i++){
  70.                                         samples[i] = (short) (amp*Math.sin(ph));
  71.                                         ph += twopi*fr/sr;
  72.                                     }
  73.                                     audioTrack.write(samples, 0, buffsize);
  74.  
  75.                                     runOnUiThread(new Runnable() {
  76.                                         @Override
  77.                                         public void run() {
  78.                                             if (( counter % 10 ) == 0) {
  79.                                                 fSlider.setProgress(counter/89);  // counter/100
  80.                                                 counter = counter + 100;
  81.                                             }
  82.                                         }
  83.                                     });
  84.  
  85.                                     if (fr > 20000) {
  86.                                         counter = 0;
  87.                                     }
  88.                                 }
  89.                                 counter = 0;
  90.  
  91.                                 audioTrack.stop();
  92.                                 audioTrack.release();
  93.                             }
  94.                         };
  95.  
  96.                         t.start();
  97.                         return true;
  98.  
  99.                     case MotionEvent.ACTION_UP:
  100.  
  101.                         if (fr > max_frequency) {
  102.                             max_frequency = fr;
  103.                             both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
  104.                             animText.animateText(both);
  105.                         }
  106.                         isRunning = false;
  107.                         counter = 0;
  108.                         t = null;
  109.                         rippleBackground.stopRippleAnimation();
  110.  
  111.                         return true;
  112.                 }
  113.  
  114.                 return false;
  115.             }
  116.         });
  117.     }
  118.  
  119.     public void onDestroy(){
  120.         super.onDestroy();
  121.         isRunning = false;
  122.         try {
  123.             t.join();
  124.         } catch (InterruptedException e) {
  125.             e.printStackTrace();
  126.         }
  127.         t = null;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment