Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Added background music and click sound #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.2.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'

testImplementation 'junit:junit:4.12'
}
21 changes: 20 additions & 1 deletion app/src/main/java/com/snatik/matches/common/Music.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.snatik.matches.common;

import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

Expand All @@ -8,6 +9,7 @@
public class Music {

public static boolean OFF = false;
private static MediaPlayer mediaPlayer;

public static void playCorrent() {
if (!OFF) {
Expand All @@ -26,7 +28,12 @@ public void onCompletion(MediaPlayer mp) {
}

public static void playBackgroundMusic() {
// TODO
mediaPlayer = MediaPlayer.create(Shared.context, R.raw.background_music);
if(isMusicOn()) {
mediaPlayer.start();
mediaPlayer.setLooping(true);
}
OFF = false;
}

public static void showStar() {
Expand All @@ -44,4 +51,16 @@ public void onCompletion(MediaPlayer mp) {
mp.start();
}
}

public static void stopBackgroundMusic(){

if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
OFF = true;
}
}

public static boolean isMusicOn(){
return !OFF;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -15,12 +16,13 @@

import com.snatik.matches.R;
import com.snatik.matches.common.Memory;
import com.snatik.matches.common.Music;
import com.snatik.matches.common.Shared;
import com.snatik.matches.events.ui.DifficultySelectedEvent;
import com.snatik.matches.themes.Theme;
import com.snatik.matches.ui.DifficultyView;

public class DifficultySelectFragment extends Fragment {
public class DifficultySelectFragment extends Fragment implements View.OnKeyListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Expand Down Expand Up @@ -90,6 +92,30 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

}

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

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

this.getView().setFocusableInTouchMode(true);
this.getView().requestFocus();
this.getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK )
{
Music.stopBackgroundMusic();
return true;
}
return false;
}
});
return false;
}

private String getBestTimeForStage(int theme, int difficulty) {
int bestTime = Memory.getBestTime(theme, difficulty);
if (bestTime != -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.snatik.matches.fragments;

import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -10,6 +13,7 @@
import android.widget.TextView;

import com.snatik.matches.R;
import com.snatik.matches.common.Music;
import com.snatik.matches.common.Shared;
import com.snatik.matches.events.engine.FlipDownCardsEvent;
import com.snatik.matches.events.engine.GameWonEvent;
Expand All @@ -22,7 +26,7 @@
import com.snatik.matches.utils.FontLoader;
import com.snatik.matches.utils.FontLoader.Font;

public class GameFragment extends BaseFragment {
public class GameFragment extends BaseFragment{

private BoardView mBoardView;
private TextView mTime;
Expand All @@ -47,7 +51,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
Shared.eventBus.listen(FlipDownCardsEvent.TYPE, this);
Shared.eventBus.listen(HidePairCardsEvent.TYPE, this);
Shared.eventBus.listen(GameWonEvent.TYPE, this);

return view;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -72,11 +73,10 @@ public void onAnimationEnd(Animator animation) {
startLightsAnimation();
startTootipAnimation();

// play background music
Music.playBackgroundMusic();
return view;
}


protected void animateAllAssetsOff(AnimatorListenerAdapter adapter) {
// title
// 120dp + 50dp + buffer(30dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.snatik.matches.R;
import com.snatik.matches.common.Memory;
import com.snatik.matches.common.Music;
import com.snatik.matches.common.Shared;
import com.snatik.matches.events.ui.ThemeSelectedEvent;
import com.snatik.matches.themes.Theme;
Expand Down Expand Up @@ -66,6 +67,11 @@ public void onClick(View v) {
return view;
}

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

private void animateShow(View view) {
ObjectAnimator animatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f, 1f);
ObjectAnimator animatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f, 1f);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/snatik/matches/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class Game {

/**
/**
* The board configuration
*/
public BoardConfiguration boardConfiguration;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/snatik/matches/ui/BoardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.Gravity;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class BoardView extends LinearLayout {
private List<Integer> flippedUp = new ArrayList<Integer>();
private boolean mLocked = false;
private int mSize;
private MediaPlayer mediaPlayer;

public BoardView(Context context) {
this(context, null);
Expand Down Expand Up @@ -142,6 +144,8 @@ protected void onPostExecute(Bitmap result) {
public void onClick(View v) {
if (!mLocked && tileView.isFlippedDown()) {
tileView.flipUp();
mediaPlayer = MediaPlayer.create(Shared.context, R.raw.click_on);
mediaPlayer.start();
flippedUp.add(id);
if (flippedUp.size() == 2) {
mLocked = true;
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/snatik/matches/ui/DifficultyView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ public void setDifficulty(int difficulty, int stars) {
int drawableResourceId = Shared.context.getResources().getIdentifier(titleResource, "drawable", Shared.context.getPackageName());
mTitle.setImageResource(drawableResourceId);
}



}
14 changes: 11 additions & 3 deletions app/src/main/java/com/snatik/matches/ui/PopupSettingsView.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.snatik.matches.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
Expand All @@ -16,7 +19,7 @@
import com.snatik.matches.utils.FontLoader;
import com.snatik.matches.utils.FontLoader.Font;

public class PopupSettingsView extends LinearLayout {
public class PopupSettingsView extends LinearLayout{

private ImageView mSoundImage;
private TextView mSoundText;
Expand All @@ -27,19 +30,22 @@ public PopupSettingsView(Context context) {

public PopupSettingsView(Context context, AttributeSet attrs) {
super(context, attrs);
if(Music.OFF) Music.stopBackgroundMusic();
setOrientation(LinearLayout.VERTICAL);
setBackgroundResource(R.drawable.settings_popup);
LayoutInflater.from(getContext()).inflate(R.layout.popup_settings_view, this, true);
mSoundText = (TextView) findViewById(R.id.sound_off_text);
mSoundText.setText("Sound OFF");
TextView rateView = (TextView) findViewById(R.id.rate_text);
FontLoader.setTypeface(context, new TextView[] { mSoundText, rateView }, Font.GROBOLD);
mSoundImage = (ImageView) findViewById(R.id.sound_image);
mSoundImage.setImageResource(R.drawable.button_music_off);
View soundOff = findViewById(R.id.sound_off);
soundOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Music.OFF = !Music.OFF;
setMusicButton();
Music.OFF = !Music.OFF;
}
});
View rate = findViewById(R.id.rate);
Expand All @@ -54,16 +60,18 @@ public void onClick(View v) {
}
}
});
setMusicButton();

}

private void setMusicButton() {
if (Music.OFF) {
mSoundText.setText("Sound OFF");
mSoundImage.setImageResource(R.drawable.button_music_off);
Music.stopBackgroundMusic();
} else {
mSoundText.setText("Sound ON");
mSoundImage.setImageResource(R.drawable.button_music_on);
Music.playBackgroundMusic();
}
}
}
Binary file added app/src/main/res/raw/click_on.mp3
Binary file not shown.
15 changes: 0 additions & 15 deletions app/src/test/java/com/snatik/matches/ExampleUnitTest.java

This file was deleted.

32 changes: 32 additions & 0 deletions app/src/test/java/com/snatik/matches/common/MusicTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.snatik.matches.common;

import android.media.MediaPlayer;

import com.snatik.matches.R;

import org.junit.Test;

import static org.junit.Assert.*;

public class MusicTest {

@Test
public void testPlayBackgroundMusic(){
assertTrue(Music.isMusicOn());
}

@Test
public void testStopBackgroundMusic() {
Music.OFF = true;
assertFalse(Music.isMusicOn());
}

@Test
public void testMusicOn() {
Music.OFF = true;
assertFalse(Music.isMusicOn());

Music.OFF = false;
assertTrue(Music.isMusicOn());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.snatik.matches.model;

import org.junit.Test;

import static org.junit.Assert.*;

public class BoardConfigurationTest {

BoardConfiguration boardConfiguration = new BoardConfiguration(5);

@Test
public void testNumTiles() {
int numTiles = boardConfiguration.getNumTiles();
assertEquals(numTiles, 32);
}

@Test
public void testNumTilesInRow() {
int numTilesInRow = boardConfiguration.getNumTilesInRow();
assertEquals(numTilesInRow, 8);
}

@Test
public void testNumRows() {
int numRows = boardConfiguration.getNumRows();
assertEquals(numRows, 4);
}

@Test
public void testTime() {
int time = boardConfiguration.getTime();
assertEquals(time, 180);
}
}