Add a seek bar tied to media volume.
This commit is contained in:
@@ -4,6 +4,7 @@ import android.app.NotificationManager;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.Ringtone;
|
||||
import android.media.RingtoneManager;
|
||||
@@ -14,13 +15,16 @@ import android.os.Message;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.TableRow;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
@@ -50,6 +54,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
Uri ringtoneUri;
|
||||
MediaPlayer mediaPlayer;
|
||||
|
||||
SeekBar seekBar;
|
||||
AudioManager audioManager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
String tag = "onCreate";
|
||||
@@ -202,7 +209,29 @@ public class MainActivity extends AppCompatActivity {
|
||||
etMillis.setVisibility(View.GONE);
|
||||
//(ViewManager) etMillis.getParent().remove
|
||||
|
||||
//sharedPreferences.edit().putBoolean(keyStartStop, false).apply();
|
||||
seekBar = findViewById(R.id.seekBar);
|
||||
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
|
||||
|
||||
syncVolume();
|
||||
|
||||
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
|
||||
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, AudioManager.FLAG_SHOW_UI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
// Do Nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
// Do Nothing
|
||||
}
|
||||
});
|
||||
setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
||||
|
||||
recoverScreen();
|
||||
|
||||
if (DEBUG) Log.d(tag, "Finished");
|
||||
@@ -585,7 +614,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
if (DEBUG) Log.d(tag, "Starting");
|
||||
|
||||
/* Original code, did not work on Nexus 7 with CM 12.1-20151117 (Android 5.1.1) * /
|
||||
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri);
|
||||
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/raw/chime_sound_7143");
|
||||
//Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri);
|
||||
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), uri);
|
||||
ringtone.play();
|
||||
/**/
|
||||
|
||||
@@ -616,6 +647,17 @@ public class MainActivity extends AppCompatActivity {
|
||||
if (DEBUG) Log.d(tag, "Finished");
|
||||
}
|
||||
|
||||
private void flipInputTexts() {
|
||||
String tag = TAG + "flipInputTexts";
|
||||
if (DEBUG) Log.d(tag, "Starting");
|
||||
|
||||
etHours.setEnabled(!start);
|
||||
etMinutes.setEnabled(!start);
|
||||
etSeconds.setEnabled(!start);
|
||||
|
||||
if (DEBUG) Log.d(tag, "Finished");
|
||||
}
|
||||
|
||||
private void resetAll() {
|
||||
String tag = TAG + "resetAll";
|
||||
if (DEBUG) Log.d(tag, "Starting");
|
||||
@@ -642,6 +684,14 @@ public class MainActivity extends AppCompatActivity {
|
||||
if (DEBUG) Log.d(tag, "Finished");
|
||||
}
|
||||
|
||||
public void syncVolume() {
|
||||
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
|
||||
seekBar.setMax(maxVolume);
|
||||
|
||||
int currVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
|
||||
seekBar.setProgress(currVolume);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
String tag = TAG + "onPause";
|
||||
@@ -693,15 +743,14 @@ public class MainActivity extends AppCompatActivity {
|
||||
if (DEBUG) Log.d(tag, "Finished");
|
||||
}
|
||||
|
||||
private void flipInputTexts() {
|
||||
String tag = TAG + "flipInputTexts";
|
||||
if (DEBUG) Log.d(tag, "Starting");
|
||||
|
||||
etHours.setEnabled(!start);
|
||||
etMinutes.setEnabled(!start);
|
||||
etSeconds.setEnabled(!start);
|
||||
|
||||
if (DEBUG) Log.d(tag, "Finished");
|
||||
}
|
||||
|
||||
}
|
||||
// TODO: This is somehow always 1 action behind the actual volume when pressing volume buttons.
|
||||
// Like when doing down+up+down, the bar does nothing+down+up.
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
|
||||
boolean bool = super.onKeyDown(keyCode, event);
|
||||
syncVolume();
|
||||
return bool;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}}
|
||||
|
@@ -158,4 +158,13 @@
|
||||
|
||||
</TableLayout>
|
||||
|
||||
<SeekBar
|
||||
android:id="@+id/seekBar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/tblButtons"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_marginVertical="@dimen/activity_vertical_margin"
|
||||
android:layout_centerHorizontal="true"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
Reference in New Issue
Block a user