Added code to ask for microphone permission if not yet granted, fixes #2
This commit is contained in:
parent
da8095e393
commit
c04b86b6b2
|
@ -16,8 +16,12 @@
|
||||||
|
|
||||||
package be.bitscuit.mike;
|
package be.bitscuit.mike;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.media.AudioAttributes;
|
import android.media.AudioAttributes;
|
||||||
import android.media.AudioFormat;
|
import android.media.AudioFormat;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
|
@ -25,7 +29,9 @@ import android.media.AudioRecord;
|
||||||
import android.media.AudioTrack;
|
import android.media.AudioTrack;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.media.MediaRecorder;
|
import android.media.MediaRecorder;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Process;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
|
@ -35,6 +41,9 @@ import static android.os.Process.setThreadPriority;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
// Static vars
|
||||||
|
private static final int RC_PERMISSIONS = 1000;
|
||||||
|
|
||||||
// Views
|
// Views
|
||||||
private TextView textState;
|
private TextView textState;
|
||||||
private Switch switchOnOff, switchMute, switchStandby;
|
private Switch switchOnOff, switchMute, switchStandby;
|
||||||
|
@ -130,6 +139,14 @@ public class MainActivity extends AppCompatActivity {
|
||||||
private void recordStart(){
|
private void recordStart(){
|
||||||
if(recording) return; // Return if already recording
|
if(recording) return; // Return if already recording
|
||||||
|
|
||||||
|
// Check if permission to use microphone is granted
|
||||||
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
||||||
|
ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED){
|
||||||
|
// Show dialog to user
|
||||||
|
requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, RC_PERMISSIONS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Start recording
|
// Start recording
|
||||||
recording = true;
|
recording = true;
|
||||||
|
|
||||||
|
@ -153,7 +170,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
);
|
);
|
||||||
|
|
||||||
// Init player
|
// Init player
|
||||||
setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
|
setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
|
||||||
AudioTrack audioTrack = new AudioTrack(
|
AudioTrack audioTrack = new AudioTrack(
|
||||||
AudioManager.STREAM_MUSIC,
|
AudioManager.STREAM_MUSIC,
|
||||||
44100,
|
44100,
|
||||||
|
@ -213,8 +230,6 @@ public class MainActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void updateUI(){
|
private void updateUI(){
|
||||||
|
|
||||||
// Set right state text
|
// Set right state text
|
||||||
|
@ -229,4 +244,22 @@ public class MainActivity extends AppCompatActivity {
|
||||||
textState.setText(stateText);
|
textState.setText(stateText);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
|
// Check request code
|
||||||
|
switch(requestCode){
|
||||||
|
case RC_PERMISSIONS:
|
||||||
|
// Check if granted
|
||||||
|
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
|
||||||
|
// Start recording
|
||||||
|
recordStart();
|
||||||
|
}else{
|
||||||
|
// Set main switch to off
|
||||||
|
switchOnOff.setChecked(false);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue