Permalink
Browse files

fixed "sonic01" for Android 6.0 or later as an example.

  • Loading branch information...
mkttanabe committed Nov 29, 2017
1 parent ede1d43 commit 8a0de22d4c6160555c217a4784e77439a34d81b4
Showing with 96 additions and 0 deletions.
  1. +33 −0 README
  2. +63 −0 sonic01/app/src/main/java/jp/klab/sonic01/MainActivity.java
View
33 README
@@ -1,3 +1,36 @@
Sonic Sonic
Copyright (c) 2017 KLab Inc. Copyright (c) 2017 KLab Inc.
���g�ɂ����ʐM�̎��
�u���O�G���g��
http://dsas.blog.klab.org/archives/2017-05/52274882.html
http://dsas.blog.klab.org/archives/2017-06/52275767.html
http://dsas.blog.klab.org/archives/2017-06/52276488.html
http://dsas.blog.klab.org/archives/2017-06/52276755.html
http://dsas.blog.klab.org/archives/2017-06/52276891.html
2017-11-29 �NjL�F
Android 6.0 �ȍ~�ł� RECORD_AUDIO �ɂ‚��Ď�s���p�[�~�b�V������
�擾����K�v������.
https://developer.android.com/training/permissions/requesting.html
sonic01 �� MainActivity.java ��Q�Ƃ̂���.
--------------
Experiments of sonic communication.
Blog entries (in Japanese)
http://dsas.blog.klab.org/archives/2017-05/52274882.html
http://dsas.blog.klab.org/archives/2017-06/52275767.html
http://dsas.blog.klab.org/archives/2017-06/52276488.html
http://dsas.blog.klab.org/archives/2017-06/52276755.html
http://dsas.blog.klab.org/archives/2017-06/52276891.html
2017-11-29 Note:
You need to request RECORD_AUDIO Permission at runtime
in Android 6.0 or later.
https://developer.android.com/training/permissions/requesting.html
See MainActivity.java of sonic01.
@@ -13,6 +13,8 @@
package jp.klab.sonic01; package jp.klab.sonic01;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.AudioFormat; import android.media.AudioFormat;
import android.media.AudioRecord; import android.media.AudioRecord;
import android.media.AudioManager; import android.media.AudioManager;
@@ -21,6 +23,8 @@
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
@@ -68,6 +72,19 @@ protected void onCreate(Bundle savedInstanceState) {
mProgressBar.setMax(BLOCK_NUMBER); mProgressBar.setMax(BLOCK_NUMBER);
mProgressBar.setProgress(0); mProgressBar.setProgress(0);
/***** add for Android 6.0 or later ****/
// https://developer.android.com/training/permissions/requesting.html
// https://developer.android.com/topic/libraries/support-library/index.html#backward
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) !=
PackageManager.PERMISSION_GRANTED) {
// RECORD_AUDIO の実行時パーミッションを要求
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO}, 1);
}
initAudio();
/**** add end ****/
/***** move to initAudio() ****
int bufferSizeInBytes = AudioRecord.getMinBufferSize(SAMPLE_RATE, int bufferSizeInBytes = AudioRecord.getMinBufferSize(SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT); AudioFormat.ENCODING_PCM_16BIT);
@@ -91,7 +108,53 @@ protected void onCreate(Bundle savedInstanceState) {
AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes, bufferSizeInBytes,
AudioTrack.MODE_STREAM); AudioTrack.MODE_STREAM);
****/
}
/**** add for Android 6.0 or later ****/
// https://developer.android.com/training/permissions/requesting.html
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initAudio();
} else {
finish();
}
return;
}
}
}
private void initAudio() {
int bufferSizeInBytes = AudioRecord.getMinBufferSize(SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
mBufferSizeInShort = bufferSizeInBytes / 2;
// 録音用バッファ
mRecordBuf = new short[mBufferSizeInShort];
// 再生用バッファ
mPlayBuf = new short[mBufferSizeInShort * BLOCK_NUMBER];
// 録音用
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes);
// 再生用
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
SAMPLE_RATE,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
} }
/**** add end ****/
@Override @Override
public void onStop() { public void onStop() {

0 comments on commit 8a0de22

Please sign in to comment.