カウントダウンタイマーをつくる③

カウントダウンが終わるとアラーム音が鳴るようにします。

MediaPlayerというライブラリを使うと簡単です。少し音楽がなるまでにタイムラグはありますが、、、

とりあえず前回のに追記して作成しましたが、いろいろ問題があるようです。

prepareAsync called in state 8, mPlayer(0xc14ba0e0) というエラーメッセージが出ますが、例外処理をしているので、実行可能です。

MainActivity.java

package com.example.intervaltimerdemo3;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
private final long START_TIME = 10000;
TextView text_countdown;
Button button_start;
Button button_pause;
Button button_reset;

private boolean mTimerRunning;

CountDownTimer mCountDownTimer;
private long mTimeLeft = START_TIME;

private MediaPlayer myMediaPlayer;

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

text_countdown = findViewById(R.id.text_view_countdown);
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss",Locale.US);
text_countdown.setText(sdf.format(mTimeLeft));
button_start = findViewById(R.id.button_start);
button_pause = findViewById(R.id.button_pause);
button_reset = findViewById(R.id.button_reset);

button_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mTimerRunning == true){
Toast.makeText(MainActivity.this,"カウントダウン中は押せません",Toast.LENGTH_SHORT).show();
}else{
startTimer();
}

}
});

button_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mTimerRunning ==true){
pauseTimer();
}else{
Toast.makeText(MainActivity.this,"カウントダウン停止中なので押せません",Toast.LENGTH_SHORT).show();
}

}
});

button_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mTimerRunning == true){
Toast.makeText(MainActivity.this,"カウントダウン中はリセットできません",Toast.LENGTH_SHORT).show();
}else{
resetTimer();
}

}
});
}

private void startTimer(){
mCountDownTimer = new CountDownTimer(mTimeLeft,1000) {
@Override
public void onTick(long TimeUntilFinished) {
mTimeLeft = TimeUntilFinished;
updateCountDownText();
}

@Override
public void onFinish() {
mTimerRunning = false;
Toast.makeText(MainActivity.this,"10秒経過しました",Toast.LENGTH_LONG).show();

myMediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.alarmdemo);
try {
myMediaPlayer.prepare();
} catch (Exception e) {
Log.v(null,e.toString());
}
myMediaPlayer.start();
}
}.start();

mTimerRunning = true;
}

private void pauseTimer(){
mCountDownTimer.cancel();
mTimerRunning = false;
}

private void resetTimer(){
mTimeLeft = START_TIME;
mTimerRunning = false;
updateCountDownText();
}

private void updateCountDownText(){
int minutes = (int)(mTimeLeft/1000)/60;
int seconds = (int)(mTimeLeft/1000)%60;
String timerLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
text_countdown.setText(timerLeftFormatted);
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/text_view_countdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textSize="50sp"
android:layout_marginTop="160sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="スタート"
android:layout_marginTop="30sp"
android:layout_marginRight="20sp"/>

<Button
android:id="@+id/button_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ストップ"
android:layout_marginTop="30sp"
android:layout_marginRight="20sp" />

<Button
android:id="@+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="リセット"
android:layout_marginTop="30sp"/>
</LinearLayout>

</LinearLayout>

 

 

あと、カウントダウンが00:00になったときに、スタートを押すとまたアラーム音がなってしまうというバグが発生してます。⇒改善予定

 

音楽ファイルはmp3ファイルを使用します。

mp3ファイルをどこにいれるかというと、今回は、resディレクトリ下にrawディレクトリを作成してそこにいれる方法をとってます。しかし、ファイル数が多くなったりする場合は、Assetsに入れるのが通常のようです。(左のツリー構造で、Android表示(Project表示でなく)で、appのところで右クリックし、New>New Folder>AssetFolderを選択する。このAndroidコード内でのAssetsフォルダーへのアクセス方法は、パスを取得し、そこにアクセスするという方法をとる必要があります。)

 

mp3ファイルをいれても?が出る状態でした。

ダブルクリックすると、アラートダイアログが出てきますが、

一番したのOpen matching files ~を選択し、OKを押しておきます。

?は消えませんが何とかなるようです。?が消える方法があるかはもう少し調べてみます。