Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,30 @@
package com.example.android.classicalmusicquiz;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

import java.util.ArrayList;

Expand All @@ -40,15 +55,18 @@ public class QuizActivity extends AppCompatActivity implements View.OnClickListe
private int mCurrentScore;
private int mHighScore;
private Button[] mButtons;
private SimpleExoPlayer mExoPlayer;
private SimpleExoPlayerView mPlayerView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);

// TODO (2): Replace the ImageView with the SimpleExoPlayerView, and remove the method calls on the composerView.
ImageView composerView = (ImageView) findViewById(R.id.composerView);

// Initialize the player view.
mPlayerView = (SimpleExoPlayerView) findViewById(R.id.playerView);

boolean isNewGame = !getIntent().hasExtra(REMAINING_SONGS_KEY);

Expand All @@ -69,9 +87,9 @@ protected void onCreate(Bundle savedInstanceState) {
mQuestionSampleIDs = QuizUtils.generateQuestion(mRemainingSampleIDs);
mAnswerSampleID = QuizUtils.getCorrectAnswerID(mQuestionSampleIDs);

// TODO (3): Replace the default artwork in the SimpleExoPlayerView with the question mark drawable.
// Load the image of the composer for the answer into the ImageView.
composerView.setImageBitmap(Sample.getComposerArtBySampleID(this, mAnswerSampleID));
// Load the question mark as the background image until the user answers the question.
mPlayerView.setDefaultArtwork(BitmapFactory.decodeResource
(getResources(), R.drawable.question_mark));

// If there is only one answer left, end the game.
if (mQuestionSampleIDs.size() < 2) {
Expand All @@ -81,17 +99,17 @@ protected void onCreate(Bundle savedInstanceState) {

// Initialize the buttons with the composers names.
mButtons = initializeButtons(mQuestionSampleIDs);

// TODO (4): Create a Sample object using the Sample.getSampleByID() method and passing in mAnswerSampleID;
// TODO (5): Create a method called initializePlayer() that takes a Uri as an argument and call it here, passing in the Sample URI.


Sample answerSample = Sample.getSampleByID(this, mAnswerSampleID);
if (answerSample == null) {
Toast.makeText(this, getString(R.string.sample_not_found_error),
Toast.LENGTH_SHORT).show();
return;
}


// In initializePayer
// TODO (6): Instantiate a SimpleExoPlayer object using DefaultTrackSelector and DefaultLoadControl.
// TODO (7): Prepare the MediaSource using DefaultDataSourceFactory and DefaultExtractorsFactory, as well as the Sample URI you passed in.
// TODO (8): Prepare the ExoPlayer with the MediaSource, start playing the sample and set the SimpleExoPlayer to the SimpleExoPlayerView.
// Initialize the player.
initializePlayer(Uri.parse(answerSample.getUri()));
}


/**
Expand All @@ -116,6 +134,37 @@ private Button[] initializeButtons(ArrayList<Integer> answerSampleIDs) {
}


/**
* Initialize ExoPlayer.
* @param mediaUri The URI of the sample to play.
*/
private void initializePlayer(Uri mediaUri) {
if (mExoPlayer == null) {
// Create an instance of the ExoPlayer.
TrackSelector trackSelector = new DefaultTrackSelector();
LoadControl loadControl = new DefaultLoadControl();
mExoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
mPlayerView.setPlayer(mExoPlayer);
// Prepare the MediaSource.
String userAgent = Util.getUserAgent(this, "ClassicalMusicQuiz");
MediaSource mediaSource = new ExtractorMediaSource(mediaUri, new DefaultDataSourceFactory(
this, userAgent), new DefaultExtractorsFactory(), null, null);
mExoPlayer.prepare(mediaSource);
mExoPlayer.setPlayWhenReady(true);
}
}


/**
* Release ExoPlayer.
*/
private void releasePlayer() {
mExoPlayer.stop();
mExoPlayer.release();
mExoPlayer = null;
}


/**
* The OnClick method for all of the answer buttons. The method uses the index of the button
* in button array to to get the ID of the sample from the array of question IDs. It also
Expand Down Expand Up @@ -161,25 +210,26 @@ public void onClick(View v) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO (9): Stop the playback when you go to the next question.
mExoPlayer.stop();
Intent nextQuestionIntent = new Intent(QuizActivity.this, QuizActivity.class);
nextQuestionIntent.putExtra(REMAINING_SONGS_KEY, mRemainingSampleIDs);
finish();
startActivity(nextQuestionIntent);
}
}, CORRECT_ANSWER_DELAY_MILLIS);

}

/**
* Disables the buttons and changes the background colors to show the correct answer.
* Disables the buttons and changes the background colors and player art to
* show the correct answer.
*/
private void showCorrectAnswer() {
mPlayerView.setDefaultArtwork(Sample.getComposerArtBySampleID(this, mAnswerSampleID));
for (int i = 0; i < mQuestionSampleIDs.size(); i++) {
int buttonSampleID = mQuestionSampleIDs.get(i);

// TODO (10): Change the default artwork in the SimpleExoPlayerView to show the picture of the composer, when the user has answered the question.
mButtons[i].setEnabled(false);

if (buttonSampleID == mAnswerSampleID) {
mButtons[i].getBackground().setColorFilter(ContextCompat.getColor
(this, android.R.color.holo_green_light),
Expand All @@ -190,10 +240,17 @@ private void showCorrectAnswer() {
(this, android.R.color.holo_red_light),
PorterDuff.Mode.MULTIPLY);
mButtons[i].setTextColor(Color.WHITE);

}
}
}

// TODO (11): Override onDestroy() to stop and release the player when the Activity is destroyed.

/**
* Release the player when the activity is destroyed.
*/
@Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,4 @@ static void endGame(Context context){
endGame.putExtra(GAME_FINISHED, true);
context.startActivity(endGame);
}

}
30 changes: 14 additions & 16 deletions app/src/main/res/layout/activity_quiz.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/playerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toTopOf="@+id/horizontalHalf"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent" />

<android.support.constraint.Guideline
android:id="@+id/horizontalHalf"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -67,20 +80,5 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonB" />

<!--TODO (1): Replace the Composer ImageView with a SimpleExoPlayerView that occupies the top half of your screen.-->
<ImageView
android:id="@+id/composerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
app:layout_constraintBottom_toTopOf="@+id/horizontalHalf"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/question_mark" />


</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
<string name="guess">Can you guess the composer?</string>
<string name="notification_text">Press play to hear the piece!</string>
<string name="sample_list_load_error">Error loading one or more samples!</string>
<string name="sample_not_found_error">Sample not found!</string>
</resources>