Android Open Source - AudioChoice Main Fragment






From Project

Back to project page AudioChoice.

License

The source code is released under:

Apache License

If you think the Android project AudioChoice 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 com.hwrdprkns.audiochoice;
// w  w w . j a  v a2s .  com
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.SeekBarProgressChange;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@EFragment(R.layout.fragment_main)
public class MainFragment extends Fragment implements View.OnClickListener, IPlayerView {

    private static final String TAG = MainFragment.class.getSimpleName();

    @ViewById(R.id.media_player_proxy) Button playerProxyButton;
    @ViewById(R.id.media_player_stream) Button playerStreamButton;
    @ViewById(R.id.extractor_from_file) Button fileExtractorButton;
    @ViewById(R.id.play_from_file) Button playFromFileButton;
    @ViewById(R.id.toggle_pause_play) Button haltButton;

    @ViewById TextView text_duration;
    @ViewById TextView text_elapsed;
    @ViewById SeekBar player_slider;

    private Handler mHandler = new Handler();
    private boolean mShouldRun = false;
    private final Runnable mUpdateClock = new Runnable() {
        public void run() {
            if(mShouldRun) {
                updateSeekbarAndTimeRemaining(); // Call the method to actually update the clock
                mHandler.postDelayed(mUpdateClock, 1000); // 1 second
            }
        }
    };

    public void startUpdater() {
        mShouldRun = true;
        mHandler.post(mUpdateClock);
    }

    public void stopUpdater() {
        mShouldRun = false;
    }

    @AfterViews
    protected void bindListeners() {
        playerProxyButton.setOnClickListener(this);
        playerStreamButton.setOnClickListener(this);
        fileExtractorButton.setOnClickListener(this);
        playFromFileButton.setOnClickListener(this);
        player_slider.setEnabled(false);
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    protected void updateSeekbarAndTimeRemaining() {
        if (currentPlayer != null) {
            long hours = TimeUnit.MILLISECONDS.toHours(currentPlayer.getCurrentPosition());
            Date date = new Date(currentPlayer.getCurrentPosition());
            DateFormat formatter = new SimpleDateFormat(hours > 0 ? "HH:mm:ss" : "mm:ss");
            text_elapsed.setText(formatter.format(date));

            int percentage = (int) (currentPlayer.getCurrentPosition() * 100f / currentPlayer.getDuration());
            player_slider.setProgress(percentage);
        }
    }

    protected void ditchCurrentPlayer() {
        stopUpdater();
        if (currentPlayer != null) {
            currentPlayer.stop();
            currentPlayer.release();
            currentPlayer = null;
        }
    }

    @Click(R.id.toggle_pause_play)
    protected void togglePlayPauseState() {
        if (currentPlayer != null) {
            if (currentPlayer.isPlaying()){
                currentPlayer.pause();
                stopUpdater();
            } else {
                currentPlayer.start();
                startUpdater();
            }
        }
    }

    private IAudioPlayer currentPlayer;

    @Override public void onClick(View v) {
        ditchCurrentPlayer();
        switch(v.getId()) {
            case R.id.media_player_proxy:
                currentPlayer = new BasicProxyMediaPlayer(getActivity(), this);
                currentPlayer.start();
                break;
            case R.id.media_player_stream:
                currentPlayer = new BasicStreamMediaPlayer(getActivity(), this);
                currentPlayer.start();
                break;
            case R.id.extractor_from_file:
                try {
                    currentPlayer = new BasicMediaExtractorMediaPlayer(getActivity(), this);
                    currentPlayer.start();
                } catch (Exception e) {
                    Log.d(TAG, "Had a problem making the current player", e);
                    if (currentPlayer == null) {
                        currentPlayer.release();
                    }
                    Toast.makeText(getActivity(), "Try again plx", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.play_from_file:
                currentPlayer = new BasicMediaPlayer(getActivity(), this);
                currentPlayer.start();
                break;
        }

        if (currentPlayer != null) {
            startUpdater();
        }
    }

    @SeekBarProgressChange(R.id.player_slider)
    void onProgressChangeOnSeekBar(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser) {
            if (currentPlayer != null) {
                currentPlayer.seekTo((int) (progress / 100f));
            } else {
                Toast.makeText(getActivity(), "Select something to play first!", Toast.LENGTH_LONG).show();
            }
        }
    }

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


    @Override public void setDuration(String duration) {
        text_duration.setText(duration);
    }

    @Override public void setElapsed(String elapsed) {
        text_elapsed.setText(elapsed);
    }

    @Override public void updateProgress(int progressPercentage) {
        player_slider.setProgress(progressPercentage);
    }
}




Java Source Code List

com.hwrdprkns.audiochoice.BasicMediaExtractorMediaPlayer.java
com.hwrdprkns.audiochoice.BasicMediaPlayer.java
com.hwrdprkns.audiochoice.BasicProxyMediaPlayer.java
com.hwrdprkns.audiochoice.BasicStreamMediaPlayer.java
com.hwrdprkns.audiochoice.IAudioPlayer.java
com.hwrdprkns.audiochoice.IPlayerView.java
com.hwrdprkns.audiochoice.MainActivity.java
com.hwrdprkns.audiochoice.MainFragment.java
com.hwrdprkns.audiochoice.server.AudioChoiceLocalServer.java
com.hwrdprkns.audiochoice.server.NanoHTTPD.java