Android Open Source - android-continuous-voice Visualizer Fragment






From Project

Back to project page android-continuous-voice.

License

The source code is released under:

Copyright (c) 2015, Marius Fink - Universit?t Hamburg All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project android-continuous-voice listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.uniHamburg.informatik.continuousvoice.views.fragments;
//from   www . ja  v a2s .c  o  m
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
import de.uniHamburg.informatik.continuousvoice.R;
import de.uniHamburg.informatik.continuousvoice.services.sound.IAmplitudeListener;
import de.uniHamburg.informatik.continuousvoice.services.sound.AudioService;
import de.uniHamburg.informatik.continuousvoice.services.sound.IAudioServiceStartStopListener;

public class VisualizerFragment extends Fragment {

    protected static final String TAG = VisualizerFragment.class.getName();
    private static final int PROGRESSBAR_GRANULARITY = 1000;
    private ProgressBar progressBar;
    private ImageView recordingIcon;
    private TextView amplitudeText;
    private Switch audioServiceSwitch;
    private Handler handler = new Handler();
    private AudioService audioService;
    
    public VisualizerFragment(AudioService audioService) {
        this.audioService = audioService;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.visualizer, container, false);

        audioServiceSwitch = (Switch) view.findViewById(R.id.audioServiceSwitch);
        audioServiceSwitch.setChecked(audioService.isRunning());
        //amplitudeText = (TextView) view.findViewById(R.id.amplitudeText);
        progressBar = (ProgressBar) view.findViewById(R.id.soundlevel);
        recordingIcon = (ImageView) view.findViewById(R.id.silenceState);
        progressBar.setMax(PROGRESSBAR_GRANULARITY); //percent

        createListeners();

        return view;
    }

    private void createListeners() {
        audioServiceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (!audioService.isRunning()) {
                        audioService.initialize();
                    }
                } else {
                    if (audioService.isRunning()) {
                        audioService.shutdown();
                        progressBar.setProgress(0);
                    }
                }
            }
        });

        audioService.addAmplitudeListener(new IAmplitudeListener() {
            @Override
            public void onSilence() {
                switchRecordingIcon(AudioService.State.SILENCE);
            }

            @Override
            public void onSpeech() {
                switchRecordingIcon(AudioService.State.SPEECH);
            }

            @Override
            public void onAmplitudeUpdate(double percent) {
                progressBar.setProgress((int) (percent * PROGRESSBAR_GRANULARITY));
            }
        });
        
        audioService.addStartStopListener(new IAudioServiceStartStopListener() {
            @Override
            public void onAudioServiceStateChange() {
                handler.post(new Runnable() {
                    public void run() {
                        audioServiceSwitch.setChecked(audioService.isRunning());
                    }
                });
            }
        });
    }

    private void switchRecordingIcon(AudioService.State state) {
        final int imageId;
        if (state == AudioService.State.SPEECH) {
            imageId = R.drawable.mic;
        } else {
            imageId = R.drawable.mic_muted;
        }
        handler.post(new Runnable() {
            public void run() {
                recordingIcon.setImageDrawable(getResources().getDrawable(imageId));
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

}




Java Source Code List

de.uniHamburg.informatik.continuousvoice.MainActivity.java
de.uniHamburg.informatik.continuousvoice.constants.BroadcastIdentifiers.java
de.uniHamburg.informatik.continuousvoice.constants.ServiceControlConstants.java
de.uniHamburg.informatik.continuousvoice.services.recognition.AbstractRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.IRecognizerControl.java
de.uniHamburg.informatik.continuousvoice.services.recognition.IStatusListener.java
de.uniHamburg.informatik.continuousvoice.services.recognition.ITranscriptionResultListener.java
de.uniHamburg.informatik.continuousvoice.services.recognition.builtIn.AbstractAndroidRecognitionListener.java
de.uniHamburg.informatik.continuousvoice.services.recognition.builtIn.AndroidRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.dummy.DummyRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.nuance.NuanceRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.pocketSphinx.PocketSphinxRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.webService.ATTWebServiceRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.webService.AbstractWebServiceRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.webService.GoogleWebServiceRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.recognition.webService.IspeechWebServiceRecognizer.java
de.uniHamburg.informatik.continuousvoice.services.sound.AudioService.java
de.uniHamburg.informatik.continuousvoice.services.sound.FileMediaRecorder.java
de.uniHamburg.informatik.continuousvoice.services.sound.IAmplitudeListener.java
de.uniHamburg.informatik.continuousvoice.services.sound.IAudioServiceStartStopListener.java
de.uniHamburg.informatik.continuousvoice.services.sound.IRecorder.java
de.uniHamburg.informatik.continuousvoice.settings.GeneralSettings.java
de.uniHamburg.informatik.continuousvoice.settings.Language.java
de.uniHamburg.informatik.continuousvoice.settings.SettingsChangedListener.java
de.uniHamburg.informatik.continuousvoice.views.fragments.RecognizerFragment.java
de.uniHamburg.informatik.continuousvoice.views.fragments.VisualizerFragment.java