Android Open Source - rfcx-client-stream-android Rain Forest Activity






From Project

Back to project page rfcx-client-stream-android.

License

The source code is released under:

Apache License

If you think the Android project rfcx-client-stream-android 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 org.rfcx.android.stream;
//from  w w  w .  j  ava2 s  .c o  m
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import me.cafecode.lib.dotindicatorlib.DotIndicator;
import org.rfcx.android.stream.view.VisualizerView;

public class RainForestActivity extends BaseActivity {

    private static final String TAG = "RainForestActivity";

    private ViewPager mPager;
    private DotIndicator mIndicator;
    private VisualizerView mVisualizerView;
    private ImageButton mStreamPlayButton;
    private TextView mStreamSateText;
    private ProgressBar mStreamProgressBar;

    private MediaPlayer mediaPlayer;
    private Visualizer visualizer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!getIntent().getBooleanExtra("auth", false)) {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        }

        setContentView(R.layout.activity_rain_forest);

        mPager = (ViewPager) findViewById(R.id.pager);
        mIndicator = (DotIndicator) findViewById(R.id.indicator);
        mVisualizerView = (VisualizerView) findViewById(R.id.visualizer_view);
        mStreamProgressBar = (ProgressBar) findViewById(R.id.progress);
        mStreamSateText = (TextView) findViewById(R.id.stream_state_text);
        mStreamPlayButton = (ImageButton) findViewById(R.id.play_button);

        mPager.setAdapter(new DescriptPagerAdapter(getSupportFragmentManager()));
        mIndicator.setViewPager(mPager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_rain_forest, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class DescriptPagerAdapter extends FragmentStatePagerAdapter {

        public DescriptPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = ForestDetailFragment.newInstance("The Congolian forests covers southeastern Cameroon, eastern Gabon, the northern and central Republic of the Congo, the northern and central Democratic Republic of the Congo and portions of southern and southwestern Central African Republic. To the north and south, the forests transition to drier forest-savanna mosaic, a mosaic of drier forests, savannas, and grasslands. To the west, the Congolian forests transition to the coastal Lower Guinean forests, which extend from western Gabon and Cameroon into southern Nigeria and Benin; these forests zones share many similarities. ");
            return fragment;
        }

    }

    public void onPlayStreamAudioClick(View v) {
        if (mediaPlayer == null) {
            initStreamAudio();
        } else if (!isStreamPlaying()) {
            playStreamAudio();
        } else {
            pauseStreamAudio();
        }
    }

    public void onVisualizerClick(View v) {

        /*if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            pauseStreamAudio();
        }*/

        if (mStreamPlayButton.getVisibility() == View.GONE) {
            // Visibility
            setPlayButtonVisibility();
        } else {
            // Gone
            setPlayButtonInvisibility();
        }
    }

    private void setPlayButtonVisibility() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // Update play button image
               /* if (isSreamPlaying()) {
                    mStreamPlayButton.setImageResource(R.drawable.play_button);
                } else {
                    mStreamPlayButton.setImageResource(R.drawable.pause_button);
                }*/

                ObjectAnimator animator = ObjectAnimator.ofFloat(mStreamPlayButton, View.ALPHA, 0, 1);
                animator.setDuration(500);
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {}

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mStreamPlayButton.setVisibility(View.VISIBLE);
                        if (isStreamPlaying()) {
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    setPlayButtonInvisibility();
                                }
                            }, 1000);
                        }
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {}

                    @Override
                    public void onAnimationRepeat(Animator animation) {}
                });
                animator.start();

            }
        });
    }

    private void setPlayButtonInvisibility() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ObjectAnimator animator = ObjectAnimator.ofFloat(mStreamPlayButton, View.ALPHA, 1, 0);
                animator.setDuration(500);
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mStreamPlayButton.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
                animator.start();
            }
        });
    }

    private void initVisualizer() {
        visualizer = new Visualizer(mediaPlayer.getAudioSessionId());

        visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
        visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
            @Override
            public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {
                mVisualizerView.updateVisualizer(waveform);
            }

            @Override
            public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {

            }
        }, Visualizer.getMaxCaptureRate() / 2, true, false);

        visualizer.setEnabled(true);
    }

    private void initStreamAudio() {
        if (mediaPlayer != null) {
            return;
        }
        // Init media player
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource("http://srv6.karadio.biz:20474");

            initVisualizer();

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    visualizer.setEnabled(false);
                }
            });

            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // Update UI after prepare stream audio
                            mStreamSateText.setText("Live");
                            mStreamProgressBar.setVisibility(View.GONE);
                            // Play
                            playStreamAudio();
                        }
                    });

                }
            });

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // Update UI before prepare stream audio
                    mStreamPlayButton.setVisibility(View.GONE);
                    mStreamProgressBar.setVisibility(View.VISIBLE);
                    mStreamSateText.setVisibility(View.VISIBLE);
                    mStreamSateText.setText("Connecting...");
                }
            });

            // Prepare stream audio
            mediaPlayer.prepareAsync();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean isStreamPlaying() {
        if (mediaPlayer == null)
            return false;
        return mediaPlayer.isPlaying();
    }

    private void playStreamAudio() {
        if (mediaPlayer != null) {
            mStreamPlayButton.setImageResource(R.drawable.pause_button);
            setPlayButtonInvisibility();
            mediaPlayer.start();
        }
    }

    private void pauseStreamAudio() {
        if (mediaPlayer != null) {
            mStreamPlayButton.setVisibility(View.VISIBLE);
            mStreamPlayButton.setImageResource(R.drawable.play_button);
            mediaPlayer.pause();
        } else {
            Toast.makeText(this, "Paused", Toast.LENGTH_SHORT).show();
        }
    }

    private void stopStreamAudio() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }

    private void releaseStreamAudio() {
        if (isFinishing() && mediaPlayer != null) {
            mediaPlayer.stop();
            visualizer.release();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    protected void dayTheme() {
        super.dayTheme();
    }

    @Override
    protected void nightTheme() {
        super.nightTheme();
    }

    @Override
    protected void onStop() {
        super.onStop();
        releaseStreamAudio();
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseStreamAudio();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseStreamAudio();
    }

}




Java Source Code List

me.cafecode.lib.dotindicatorlib.ApplicationTest.java
me.cafecode.lib.dotindicatorlib.DotIndicator.java
org.rfcx.android.stream.ApplicationTest.java
org.rfcx.android.stream.BaseActivity.java
org.rfcx.android.stream.ForestDetailFragment.java
org.rfcx.android.stream.LoginActivity.java
org.rfcx.android.stream.RainForestActivity.java
org.rfcx.android.stream.view.SignUpFragment.java
org.rfcx.android.stream.view.VisualizerView.java