カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

音を鳴らす – ToneGenerator [Android]

この記事は最終更新日から1年以上経過しています。

ToneGeneratorを使って、簡単な音を鳴らしてみましょう。

必要なインポート

import android.media.AudioManager;
import android.media.ToneGenerator;

音を鳴らす

ToneGenerator toneGenerator;
toneGenerator = new ToneGenerator(
        AudioManager.STREAM_ALARM,
        ToneGenerator.MAX_VOLUME);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);


音を止めて、ToneGeneretorを解放します

toneGenerator.stopTone();
toneGenerator.release();

アラームとして使う

// アラームセット
public void setAlarm(final int sec) {
    new Thread() {
        public void run() {
            try {
                Thread.sleep(sec * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ToneGenerator toneGenerator;
            toneGenerator = new ToneGenerator(
                    AudioManager.STREAM_ALARM,
                    ToneGenerator.MAX_VOLUME);
            toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            toneGenerator.stopTone();
            toneGenerator.release();
        }
    }.start();
}

ただ、この方法だとアクティビティが破棄されたり、スリープ状態だとサスペンドされます。
スリープ状態などでも休まず実行させるにはサービス(IntentService)を使います。


TimerService.java

import android.app.IntentService;
import android.content.Intent;
import android.media.AudioManager;
import android.media.ToneGenerator;

public class TimerService extends IntentService {

    public TimerService() {
        super("TimerService");
    }
    
    @Override
    protected void onHandleIntent(Intent intent) {
        final int min = intent.getIntExtra("TimerService_Extra", 0);
        new Thread() {
            public void run() {
                try {
                    Thread.sleep((min*60)*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ToneGenerator toneGenerator;
                toneGenerator = new ToneGenerator(
                        AudioManager.STREAM_ALARM,
                        ToneGenerator.MAX_VOLUME);
                toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                toneGenerator.stopTone();
                toneGenerator.release();
            }
        }.start();
    }

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

}

MainActivity.java

// アラームセット
public void setAlarm(final int min) {
    new AlertDialog.Builder(context)
        .setMessage("アラームをセットします。\nキャンセルはできません。\nよろしいですか?")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_SYNC, null, context, TimerService.class);
                intent.putExtra("TimerService_Extra", min);
                context.startService(intent);
            }
        })
        .setNegativeButton("キャンセル", null)
        .show();
}
この記事がお役に立ちましたらシェアお願いします
10,172 views

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です