I have an AsyncTask in which the onPostExecute method is:
protected void onPostExecute(String args) {
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.dialog_root_element));
AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
builder.setView(layout)
.setMessage(name)
.setTitle("playing...")
.setCancelable(false)
.setNeutralButton("stop/volver",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
mediaPlayer.stop();
dialog.cancel();
}
});
alert = builder.create();
alert.show();
SeekBar volControl = (SeekBar)findViewById(R.id.dialog_seekbar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.stop();
alert.dismiss();
}
});
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
}
});
}
the error is in:
volControl.setMax(maxVolume);
but I don't understand why... (In debug mode, maxVolume is 15)
I am trying do a dialog with a volume control seekBar but I don't see the error. can I modify the volume of a sound wit AsyncTask?
thanks a lot!
You're calling findViewById
on the wrong object. The code is looking for your SeekBar
in your activity's layout file, which probably doesn't exist (I'm guessing you're getting a null pointer exception).
Try this, show your dialog, then call findViewById
on the dialog, not the activity to find your SeekBar
.
alert.show();
SeekBar volControl = (SeekBar)alert.findViewById(R.id.dialog_seekbar);
The important part here is alert.findViewById().