Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.69 KB | None | 0 0
  1.  
  2. import android.graphics.drawable.ColorDrawable;
  3. import android.media.MediaPlayer;
  4. import android.media.audiofx.BassBoost;
  5. import android.media.audiofx.Virtualizer;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.CompoundButton;
  10. import android.widget.ImageView;
  11. import android.widget.Switch;
  12. import android.widget.ToggleButton;
  13.  
  14. import java.io.IOException;
  15.  
  16. public class AudioPlaybackActivity extends AppCompatActivity {
  17.  
  18.     private ImageView boostImg;
  19.     /**
  20.      * Switches (play/pause music)
  21.      */
  22.     private Switch sEasternEmotion, sReggaeFeeling;
  23.  
  24.     /**
  25.      * Media Players (components to control MP3 playback)
  26.      */
  27.     private MediaPlayer mpEasternEmotion, mpReggaeFeeling;
  28.  
  29.     /**
  30.      * Toggle Buttons to enable or disable Bass Boost and Virtualiser
  31.      */
  32.     private ToggleButton tbtnBassBoost, tbtnVirtualizer;
  33.  
  34.     /**
  35.      * BassBoost and Virtualiser components
  36.      */
  37.     private BassBoost bassBoost;
  38.     private Virtualizer virtualizer;
  39.  
  40.     @Override
  41.     protected void onCreate(Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.         setContentView(R.layout.activity_audio_playback);
  44.  
  45.         getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.tuAkzentfarbe1BlauHell)));
  46.  
  47.         InitializeActivity();
  48.     }
  49.  
  50.     /**
  51.      * Initialises widgets and event handlers
  52.      */
  53.     private void InitializeActivity() {
  54.         sEasternEmotion = (Switch) findViewById(R.id.switchEasternEmotion);
  55.         sReggaeFeeling = (Switch) findViewById(R.id.switchReggaeFeeling);
  56.  
  57.         sEasternEmotion.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  58.             @Override
  59.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  60.                 if (mpReggaeFeeling.isPlaying())
  61.                     PauseReggaeFeeling();
  62.                 EasternEmotionToggled();
  63.             }
  64.         });
  65.  
  66.         sReggaeFeeling.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  67.             @Override
  68.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  69.                 if (mpEasternEmotion.isPlaying())
  70.                     PauseEasternEmotion();
  71.                 ReggaeFeelingToggled();
  72.             }
  73.         });
  74.  
  75.         mpEasternEmotion = MediaPlayer.create(this, R.raw.eastern_emotion_terrasound_de);
  76.         mpReggaeFeeling = MediaPlayer.create(this, R.raw.reggae_feeling_terrasound_de);
  77.  
  78.         mpEasternEmotion.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  79.             @Override
  80.             public void onCompletion(MediaPlayer mp) {
  81.                 sEasternEmotion.setChecked(false);
  82.             }
  83.         });
  84.  
  85.         mpReggaeFeeling.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  86.             @Override
  87.             public void onCompletion(MediaPlayer mp) {
  88.                 sReggaeFeeling.setChecked(false);
  89.             }
  90.         });
  91.  
  92.         tbtnBassBoost = (ToggleButton) findViewById(R.id.toggleButtonBassBoost);
  93.         tbtnVirtualizer = (ToggleButton) findViewById(R.id.toggleButtonVirtualizer);
  94.  
  95.         boostImg = (ImageView) findViewById(R.id.imageView3);
  96.  
  97.         tbtnBassBoost.setOnClickListener(new View.OnClickListener() {
  98.             @Override
  99.             public void onClick(View v) {
  100.                 BassBoostClicked();
  101.             }
  102.         });
  103.  
  104.         tbtnVirtualizer.setOnClickListener(new View.OnClickListener() {
  105.             @Override
  106.             public void onClick(View v) {
  107.                 VirtualizerClicked();
  108.             }
  109.         });
  110.  
  111.     }
  112.  
  113.     /**
  114.      * Configures the sound FX setup based on a given session
  115.      * @param sessionID The session to apply FX on
  116.      */
  117.     private void ConfigureSoundEffects(int sessionID) {
  118.         // Disable all prior FX
  119.         if(bassBoost != null) {
  120.             bassBoost.setEnabled(false);
  121.         }
  122.         if(virtualizer != null) {
  123.             virtualizer.setEnabled(false);
  124.         }
  125.  
  126.         // BASS BOOST
  127.         bassBoost = new BassBoost(0, sessionID);
  128.         if(bassBoost.getStrengthSupported()) {
  129.             bassBoost.setStrength((short) 1000);
  130.         }
  131.  
  132.  
  133.         // VIRTUALIZER
  134.         virtualizer = new Virtualizer(0, sessionID);
  135.         virtualizer.setStrength((short) 1000);
  136.  
  137.     }
  138.  
  139.     /**
  140.      * Handle toggling of Eastern Emotion switch
  141.      */
  142.     private void EasternEmotionToggled() {
  143.  
  144.         if (!mpEasternEmotion.isPlaying()) {
  145.             PlaybackEasternEmotion();
  146.  
  147.         }
  148.         else
  149.             PauseEasternEmotion();
  150.  
  151.     }
  152.  
  153.     /**
  154.      * Starts playback of first audio
  155.      */
  156.     private void PlaybackEasternEmotion() {
  157.  
  158.         mpEasternEmotion.start();
  159.         ConfigureSoundEffects(mpEasternEmotion.getAudioSessionId());
  160.  
  161.     }
  162.  
  163.     /**
  164.      * Halts playback of first audio
  165.      */
  166.     private void PauseEasternEmotion() {
  167.  
  168.         mpEasternEmotion.pause();
  169.         sEasternEmotion.setChecked(false);
  170.  
  171.     }
  172.  
  173.     /**
  174.      * Handle toggling of Reggae Feeling switch
  175.      */
  176.     private void ReggaeFeelingToggled() {
  177.  
  178.         if (!mpReggaeFeeling.isPlaying()) {
  179.             PlaybackReggaeFeeling();
  180.  
  181.         }
  182.         else
  183.             PauseReggaeFeeling();
  184.  
  185.     }
  186.  
  187.     /**
  188.      * Starts playback of second audio
  189.      */
  190.     private void PlaybackReggaeFeeling() {
  191.  
  192.         mpReggaeFeeling.start();
  193.         ConfigureSoundEffects(mpReggaeFeeling.getAudioSessionId());
  194.  
  195.     }
  196.  
  197.     /**
  198.      * Halts playback of second audio
  199.      */
  200.     private void PauseReggaeFeeling() {
  201.  
  202.         mpReggaeFeeling.pause();
  203.         sReggaeFeeling.setChecked(false);
  204.  
  205.     }
  206.  
  207.     /**
  208.      * Handle Bass Boost Switch
  209.      */
  210.     private void BassBoostClicked() {
  211.  
  212.         if (bassBoost != null) {
  213.             if (bassBoost.getEnabled()) {
  214.                 bassBoost.setEnabled(false);
  215.                 tbtnBassBoost.setChecked(false);
  216.                 boostImg.setVisibility(View.GONE);
  217.             }
  218.             else {
  219.                 bassBoost.setEnabled(true);
  220.                 tbtnBassBoost.setChecked(true);
  221.                 boostImg.setVisibility(View.VISIBLE);
  222.             }
  223.         }
  224.  
  225.     }
  226.  
  227.     /**
  228.      * Handle Virtualizer Switch
  229.      */
  230.     private void VirtualizerClicked() {
  231.  
  232.         if (virtualizer != null) {
  233.             if (virtualizer.getEnabled()) {
  234.                 virtualizer.setEnabled(false);
  235.                 tbtnVirtualizer.setChecked(false);
  236.             }
  237.             else {
  238.                 virtualizer.setEnabled(true);
  239.                 tbtnVirtualizer.setChecked(true);
  240.             }
  241.         }
  242.  
  243.     }
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement