Skip to content

Commit

Permalink
Refactored Visualizer visuals, fixed media playback bug, and constrai…
Browse files Browse the repository at this point in the history
…ned visualizer to frame layout bounds.
  • Loading branch information
matthew-compton committed Jul 29, 2014
1 parent 15bfda8 commit 291db12
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
25 changes: 15 additions & 10 deletions app/src/main/java/com/ambergleam/visualizer/VisualizerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -17,7 +18,7 @@
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

Expand All @@ -32,8 +33,9 @@ public class VisualizerFragment extends Fragment {
private MediaPlayer mMediaPlayer;
private Visualizer mVisualizer;

private LinearLayout mScreen;
private FrameLayout mFrame;
private VisualizerView mVisualizerView;
private RelativeLayout mRelativeLayout;

private TextView mTitleTextView;
private TextView mCurrentTimeTextView;
Expand All @@ -52,9 +54,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);

mRelativeLayout = (RelativeLayout) view.findViewById(R.id.frame);
mRelativeLayout.setVisibility(View.INVISIBLE);
mScreen = (LinearLayout) view.findViewById(R.id.screen);
mScreen.setVisibility(View.INVISIBLE);

mFrame = (FrameLayout) view.findViewById(R.id.frame);
mTitleTextView = (TextView) view.findViewById(R.id.title);
mCurrentTimeTextView = (TextView) view.findViewById(R.id.time_current);
mDurationTimeTextView = (TextView) view.findViewById(R.id.time_total);
Expand Down Expand Up @@ -172,7 +175,7 @@ private void setAudio(String name) {
}

private void updateUI(String name) {
mRelativeLayout.setVisibility(View.VISIBLE);
mScreen.setVisibility(View.VISIBLE);
mTitleTextView.setText(StringFormat.getFileNameFormatted(name));
mDurationTimeTextView.setText(StringFormat.getTimeFormatted(mMediaPlayer.getDuration()));
mSeekBar.setMax(mMediaPlayer.getDuration());
Expand Down Expand Up @@ -206,10 +209,10 @@ public void onStopTrackingTouch(SeekBar seekBar) {

private void setup() {
mVisualizerView = new VisualizerView(getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mVisualizerView.setLayoutParams(params);
mRelativeLayout.addView(mVisualizerView);
mFrame.addView(mVisualizerView);

mMediaPlayer = MediaPlayer.create(getActivity(), mAudioId);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
Expand Down Expand Up @@ -241,7 +244,9 @@ private void pause() {
private void reset() {
mCurrentPosition = 0;
mSeekBar.setProgress(0);
mMediaPlayer.pause();
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
stopTime();
updateTime();
getActivity().invalidateOptionsMenu();
Expand All @@ -266,7 +271,7 @@ private void destroy() {
stopTime();
updateTime();
mVisualizer.setEnabled(false);
mRelativeLayout.removeView(mVisualizerView);
mFrame.removeView(mVisualizerView);
}

private void showDialog() {
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/ambergleam/visualizer/VisualizerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class VisualizerView extends View {

private Context mContext;

private byte[] mBytes;
private float[] mPoints;
private Rect mRect = new Rect();
Expand All @@ -18,25 +19,27 @@ public class VisualizerView extends View {

public VisualizerView(Context context) {
super(context);
mContext = context;
init();
}

public VisualizerView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}

public VisualizerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}

private void init() {
mBytes = null;

mForePaint.setStrokeWidth(1f);
mForePaint.setAntiAlias(true);
mForePaint.setColor(Color.rgb(0, 128, 255));
mForePaint.setStrokeWidth(5f);
mForePaint.setColor(mContext.getResources().getColor(R.color.line_default));
}

public void updateVisualizer(byte[] bytes) {
Expand All @@ -60,11 +63,9 @@ protected void onDraw(Canvas canvas) {

for (int i = 0; i < mBytes.length - 1; i++) {
mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
mPoints[i * 4 + 1] = mRect.height() / 2
+ ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
mPoints[i * 4 + 1] = mRect.height() / 2 + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
mPoints[i * 4 + 3] = mRect.height() / 2
+ ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;
mPoints[i * 4 + 3] = mRect.height() / 2 + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;
}

canvas.drawLines(mPoints, mForePaint);
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/res/layout/fragment_visualizer.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_horizontal_margin">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_horizontal_margin">

<TextView
android:id="@+id/title"
Expand All @@ -22,6 +22,11 @@
android:layout_margin="10dp"
android:background="@android:color/black"/>

<FrameLayout
android:id="@+id/frame"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<View
android:layout_width="match_parent"
Expand Down Expand Up @@ -63,4 +68,4 @@

</LinearLayout>

</RelativeLayout>
</LinearLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="line_default">#0080FF</color>

</resources>

0 comments on commit 291db12

Please sign in to comment.