控制Seekbar不努力控制音量

发布于 2025-01-29 16:00:01 字数 6074 浏览 4 评论 0原文

我是新手,我正在为Android创建我的第一个Java应用程序,而且我遇到了一些困难,您能帮我吗?

我创建了一个Seekbar来控制音量量,但它不起作用。

另一点是,当我单击“ play”(executarsom)时,我可以启动音频,但是在暂停(pausarsom)之后,如果我尝试通过单击播放再次播放它,则只有在再次打开应用程序时,它就不起作用。

mainActivity.java

package com.example.mediaplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
    private SeekBar seekVolume;
    private AudioManager audioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shhh);

    }

    public void inicializarseekBar(View view) {

        seekVolume = findViewById(R.id.seekBar2);

        // configurar audio manager
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        //recuperar volume máximo
        int volumeMaximo = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        //recuperar volume atual
        int volumeAtual = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        //configurar volume máximo
        seekVolume.setMax(volumeMaximo);

        //configurar volume atual
        seekVolume.setProgress(volumeAtual);

        //comfigurar o progresso atual do seekbar
        seekVolume.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) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    public void executarSom(View view){
        if (mediaPlayer != null) {
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }

    }

   public void pausarSom(View view){
       if (mediaPlayer != null) {
           mediaPlayer.stop();

        }
    }

    public void pararSom(View view){
        if (mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shhh);
            mediaPlayer.release();
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if (mediaPlayer != null && mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    protected void onStop(){
        super.onStop();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }
}

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:id="@+id/seekVolume"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="executarSom"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/buttonStop"
        style="?attr/borderlessButtonStyle"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:onClick="pararSom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/buttonPause"
        app:layout_constraintTop_toBottomOf="@+id/seekBar2"
        app:srcCompat="@drawable/stop" />

    <ImageButton
        android:id="@+id/buttonPlay"
        style="?attr/borderlessButtonStyle"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:onClick="executarSom"
        app:layout_constraintEnd_toStartOf="@+id/buttonPause"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seekBar2"
        app:srcCompat="@drawable/play" />

    <ImageButton
        android:id="@+id/buttonPause"
        style="?attr/borderlessButtonStyle"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:onClick="pausarSom"
        app:layout_constraintEnd_toStartOf="@+id/buttonStop"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/buttonPlay"
        app:layout_constraintTop_toBottomOf="@+id/seekBar2"
        app:srcCompat="@drawable/pause" />

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="280dp"
        android:layout_height="18dp"
        android:layout_marginTop="356dp"
        android:onClick="inicializarseekBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

I'm newbie and I'm creating my first java app for android, and I'm having some difficulties, can you please help me?

I created a seekbar to control my audio volume, but it's not working.

Another point is that when I click play(executarSom) I can start the audio, but after pausing(pausarSom), if I try to play it again by clicking Play, it doesn't work, only if I open the application again.

MainActivity.java

package com.example.mediaplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
    private SeekBar seekVolume;
    private AudioManager audioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shhh);

    }

    public void inicializarseekBar(View view) {

        seekVolume = findViewById(R.id.seekBar2);

        // configurar audio manager
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        //recuperar volume máximo
        int volumeMaximo = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        //recuperar volume atual
        int volumeAtual = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        //configurar volume máximo
        seekVolume.setMax(volumeMaximo);

        //configurar volume atual
        seekVolume.setProgress(volumeAtual);

        //comfigurar o progresso atual do seekbar
        seekVolume.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) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    public void executarSom(View view){
        if (mediaPlayer != null) {
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }

    }

   public void pausarSom(View view){
       if (mediaPlayer != null) {
           mediaPlayer.stop();

        }
    }

    public void pararSom(View view){
        if (mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shhh);
            mediaPlayer.release();
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if (mediaPlayer != null && mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    protected void onStop(){
        super.onStop();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }
}

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:id="@+id/seekVolume"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="executarSom"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/buttonStop"
        style="?attr/borderlessButtonStyle"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:onClick="pararSom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/buttonPause"
        app:layout_constraintTop_toBottomOf="@+id/seekBar2"
        app:srcCompat="@drawable/stop" />

    <ImageButton
        android:id="@+id/buttonPlay"
        style="?attr/borderlessButtonStyle"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:onClick="executarSom"
        app:layout_constraintEnd_toStartOf="@+id/buttonPause"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seekBar2"
        app:srcCompat="@drawable/play" />

    <ImageButton
        android:id="@+id/buttonPause"
        style="?attr/borderlessButtonStyle"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:onClick="pausarSom"
        app:layout_constraintEnd_toStartOf="@+id/buttonStop"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/buttonPlay"
        app:layout_constraintTop_toBottomOf="@+id/seekBar2"
        app:srcCompat="@drawable/pause" />

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="280dp"
        android:layout_height="18dp"
        android:layout_marginTop="356dp"
        android:onClick="inicializarseekBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2025-02-05 16:00:01

我在 @chirag的答案中找到了Seekbar的解决方案,使用seekbar来控制Android sequbar ?

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 TestExample 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.main);
        initControls();
    }

    private void initControls()
    {
        try
        {
            volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
            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();
        }
    }

I found the solution for seekBar in @Chirag's answer in Using SeekBar to Control Volume in android?

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 TestExample 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.main);
        initControls();
    }

    private void initControls()
    {
        try
        {
            volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
            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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文