Android使用Java:使用Seekbar调整音量

huangapple go评论67阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;Button
android:id=&quot;@+id/bonkButton&quot;
android:layout_width=&quot;296dp&quot;
android:layout_height=&quot;232dp&quot;
android:text=&quot;@string/bonk&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;SeekBar
android:id=&quot;@+id/volumeSeekBar&quot;
android:layout_width=&quot;276dp&quot;
android:layout_height=&quot;26dp&quot;
android:layout_marginBottom=&quot;116dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.496&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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.

huangapple
  • 本文由 发表于 2020年8月9日 04:58:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定