英文:
Android w/ Java: Using Seekbar to adjust volume
问题
以下是您要翻译的内容:
"I'm new to Android App development (on android studio) and to start off I'm creating a simple 'bonk' soundboard app, and testing it using the Pixel 2 android emulator. The goal of the app is to play the bonk noise whenever someone presses the BONK button on my main screen. I also wanted to implement a seekbar that adjusts the volume when you slide it. To do this I followed a simple implementation I found here on StackOverflow (shown below).
Even though the rest of my app works fine, for some reason my volume won't adjust whenever I slide the seekbar. Here is my code:
MainActivity.java
package com.example.bonk;
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
Button bonkButton;
SeekBar volumeSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bonkButton = (Button)findViewById(R.id.bonkButton);
volumeSeekBar = (SeekBar)findViewById(R.id.volumeSeekBar);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.bonksound);
bonkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp.start();
}
});
}
}
Volumizer.java
package com.example.bonk;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class Volumizer extends Activity {
//Called when the activity is first created.
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
try {
volumeSeekbar = findViewById(R.id.volumeSeekBar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/bonkButton"
android:layout_width="296dp"
android:layout_height="232dp"
android:text="@string/bonk"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/volumeSeekBar"
android:layout_width="276dp"
android:layout_height="26dp"
android:layout_marginBottom="116dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Please let me know if you have any ideas as of what I may be doing wrong. Thank you!
英文:
<br>
I'm new to Android App development (on android studio) and to start off I'm creating a simple 'bonk' soundboard app, and testing it using the Pixel 2 android emulator. The goal of the app is to play the bonk noise whenever someone presses the BONK button on my main screen. I also wanted to implement a seekbar that adjusts the volume when you slide it. To do this I followed a simple implementation I found here on StackOverflow (shown below). <br><br>
Even though the rest of my app works fine, for some reason my volume won't adjust whenever I slide the seekbar. Here is my code:<br><br>
MainActivity.java
package com.example.bonk;
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
Button bonkButton;
SeekBar volumeSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bonkButton = (Button)findViewById(R.id.bonkButton);
volumeSeekBar = (SeekBar)findViewById(R.id.volumeSeekBar);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.bonksound);
bonkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp.start();
}
});
}
}
Volumizer.java
package com.example.bonk;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class Volumizer extends Activity {
//Called when the activity is first created.
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
try {
volumeSeekbar = findViewById(R.id.volumeSeekBar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/bonkButton"
android:layout_width="296dp"
android:layout_height="232dp"
android:text="@string/bonk"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/volumeSeekBar"
android:layout_width="276dp"
android:layout_height="26dp"
android:layout_marginBottom="116dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Please let me know if you have any ideas as of what I may be doing wrong. Thank you!
答案1
得分: 1
我能够通过将initControls()函数定义移动到我的MainActivity.java类中并在onCreate中调用它来解决这个问题。
英文:
I was able to solve the problem by moving my initControls() function definition into my MainActivity.java class and calling it onCreate.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论