Android一次播放1个声音

发布于 2024-11-27 07:00:00 字数 8113 浏览 1 评论 0原文

很抱歉再次问这个问题。我以为我找到了我想要的信息,但 public void stop() 似乎不起作用。我正在使用 Soundpool 和 SoundManger。我只想一次只播放 1 个声音。一旦按下另一个按钮,我希望所有以前的声音停止并只播放最近按下的按钮。

这是我的 SoundManger:

package com.androidbook.ufgsoundboard;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;



public class SoundManager {

    private  SoundPool mSoundPool; 
    private  HashMap<Integer, Integer> mSoundPoolMap; 
    private  AudioManager  mAudioManager;
    private  Context mContext;
    public static final int maxSounds = 1;

    public SoundManager()
    {

    }

    public void initSounds(Context theContext) { 
         mContext = theContext;
         mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
         mSoundPoolMap = new HashMap<Integer, Integer>(); 
         mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
    } 

    public void addSound(int Index,int SoundID)
    {
        mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

    }

    public void playSound(int index) { 

         int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
         mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
    }


}

这是我的音板:

package com.androidbook.ufgsoundboard;

import com.androidbook.ufgsoundboard.SoundManager;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Soundboard extends Activity {
    private SoundManager mSoundManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.petera);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.sound1);
        mSoundManager.addSound(2, R.raw.sound2);
        mSoundManager.addSound(3, R.raw.sound3);
        mSoundManager.addSound(4, R.raw.sound4);
        mSoundManager.addSound(5, R.raw.sound5);
        mSoundManager.addSound(6, R.raw.sound6);
        mSoundManager.addSound(7, R.raw.sound7);
        mSoundManager.addSound(8, R.raw.sound8);
        mSoundManager.addSound(9, R.raw.sound9);
        mSoundManager.addSound(10, R.raw.sound10);
        mSoundManager.addSound(11, R.raw.sound11);
        mSoundManager.addSound(12, R.raw.sound12);
        mSoundManager.addSound(13, R.raw.sound13);
        mSoundManager.addSound(14, R.raw.sound14);
        mSoundManager.addSound(15, R.raw.sound15);
        mSoundManager.addSound(16, R.raw.sound16);
        mSoundManager.addSound(17, R.raw.sound17);
        mSoundManager.addSound(18, R.raw.sound18);




        Button SoundButton1 = (Button)findViewById(R.id.button3);
        SoundButton1.setOnClickListener(new OnClickListener() {


    public void onClick(View v) {
            mSoundManager.playSound(1);

    }
});       
        Button SoundButton2 = (Button)findViewById(R.id.button4);
        SoundButton2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(2);

    }
});    
        Button SoundButton3 = (Button)findViewById(R.id.button5);
        SoundButton3.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(3);

    }
});    
        Button SoundButton4 = (Button)findViewById(R.id.button6);
        SoundButton4.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(4);

    }
});    
        Button SoundButton5 = (Button)findViewById(R.id.button7);
        SoundButton5.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(5);

    }
});   
        Button SoundButton6 = (Button)findViewById(R.id.button8);
        SoundButton6.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(6);

    }
});    
        Button SoundButton7 = (Button)findViewById(R.id.button9);
        SoundButton7.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(7);

    }
});
  Button SoundButton8 = (Button)findViewById(R.id.button10);
       SoundButton8.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(8);

    }
 });    
        Button SoundButton9 = (Button)findViewById(R.id.button11);
        SoundButton9.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(9);

    }
});    
       Button SoundButton10 = (Button)findViewById(R.id.button12);
       SoundButton10.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(10);

    }
 });    
        Button SoundButton11 = (Button)findViewById(R.id.button13);
        SoundButton11.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(11);

    }
});    
        Button SoundButton12 = (Button)findViewById(R.id.button14);
        SoundButton12.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(12);

    }
});    
       Button SoundButton13 = (Button)findViewById(R.id.button15);
       SoundButton13.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(13);

    }
 });    
        Button SoundButton14 = (Button)findViewById(R.id.button16);
        SoundButton14.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(14);

    }
});    

        Button SoundButton15 = (Button)findViewById(R.id.button17);
                SoundButton15.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(15);

            }
        });
                Button SoundButton16 = (Button)findViewById(R.id.button18);
                SoundButton16.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(16);

            }
        });    
                Button SoundButton17 = (Button)findViewById(R.id.button19);
                SoundButton17.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(17);

            }
        });    
                Button SoundButton18 = (Button)findViewById(R.id.button20);
                SoundButton18.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(18);

            }

                });
                Button back = (Button) findViewById(R.id.button1); 
                back.setOnClickListener(new View.OnClickListener() { 
                    public void onClick(View view) {
                         // fire intent
                        finish(); // finish current activity
                        Intent myIntent = new Intent(view.getContext(), 
                                UFGcharactersActivity.class); 
                        startActivityForResult(myIntent, 0);

                    } 


                }); 





        Button next = (Button) findViewById(R.id.button2); 
        next.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) {
                 // fire intent
                finish(); // finish current activity
                Intent myIntent = new Intent(view.getContext(), 
                        Soundboardb.class); 
                startActivityForResult(myIntent, 0);

            } 


        });}

        };    

我希望这是一个我似乎无法找到或解决的简单更改。

谢谢大家的期待。

保罗

Apologies for asking this question again. I thought I found the infomation I was after but public void stop() doesnt seem to work. I'm using Soundpool and SoundManger. I want just 1 sound to play at any one time. Once another button is pressed I want all previous sounds to stop and just play the most recently pressed button.

Here is my SoundManger:

package com.androidbook.ufgsoundboard;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;



public class SoundManager {

    private  SoundPool mSoundPool; 
    private  HashMap<Integer, Integer> mSoundPoolMap; 
    private  AudioManager  mAudioManager;
    private  Context mContext;
    public static final int maxSounds = 1;

    public SoundManager()
    {

    }

    public void initSounds(Context theContext) { 
         mContext = theContext;
         mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
         mSoundPoolMap = new HashMap<Integer, Integer>(); 
         mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
    } 

    public void addSound(int Index,int SoundID)
    {
        mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

    }

    public void playSound(int index) { 

         int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
         mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
    }


}

And here is my Soundboard:

package com.androidbook.ufgsoundboard;

import com.androidbook.ufgsoundboard.SoundManager;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Soundboard extends Activity {
    private SoundManager mSoundManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.petera);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.sound1);
        mSoundManager.addSound(2, R.raw.sound2);
        mSoundManager.addSound(3, R.raw.sound3);
        mSoundManager.addSound(4, R.raw.sound4);
        mSoundManager.addSound(5, R.raw.sound5);
        mSoundManager.addSound(6, R.raw.sound6);
        mSoundManager.addSound(7, R.raw.sound7);
        mSoundManager.addSound(8, R.raw.sound8);
        mSoundManager.addSound(9, R.raw.sound9);
        mSoundManager.addSound(10, R.raw.sound10);
        mSoundManager.addSound(11, R.raw.sound11);
        mSoundManager.addSound(12, R.raw.sound12);
        mSoundManager.addSound(13, R.raw.sound13);
        mSoundManager.addSound(14, R.raw.sound14);
        mSoundManager.addSound(15, R.raw.sound15);
        mSoundManager.addSound(16, R.raw.sound16);
        mSoundManager.addSound(17, R.raw.sound17);
        mSoundManager.addSound(18, R.raw.sound18);




        Button SoundButton1 = (Button)findViewById(R.id.button3);
        SoundButton1.setOnClickListener(new OnClickListener() {


    public void onClick(View v) {
            mSoundManager.playSound(1);

    }
});       
        Button SoundButton2 = (Button)findViewById(R.id.button4);
        SoundButton2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(2);

    }
});    
        Button SoundButton3 = (Button)findViewById(R.id.button5);
        SoundButton3.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(3);

    }
});    
        Button SoundButton4 = (Button)findViewById(R.id.button6);
        SoundButton4.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(4);

    }
});    
        Button SoundButton5 = (Button)findViewById(R.id.button7);
        SoundButton5.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(5);

    }
});   
        Button SoundButton6 = (Button)findViewById(R.id.button8);
        SoundButton6.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(6);

    }
});    
        Button SoundButton7 = (Button)findViewById(R.id.button9);
        SoundButton7.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(7);

    }
});
  Button SoundButton8 = (Button)findViewById(R.id.button10);
       SoundButton8.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(8);

    }
 });    
        Button SoundButton9 = (Button)findViewById(R.id.button11);
        SoundButton9.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(9);

    }
});    
       Button SoundButton10 = (Button)findViewById(R.id.button12);
       SoundButton10.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(10);

    }
 });    
        Button SoundButton11 = (Button)findViewById(R.id.button13);
        SoundButton11.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(11);

    }
});    
        Button SoundButton12 = (Button)findViewById(R.id.button14);
        SoundButton12.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(12);

    }
});    
       Button SoundButton13 = (Button)findViewById(R.id.button15);
       SoundButton13.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(13);

    }
 });    
        Button SoundButton14 = (Button)findViewById(R.id.button16);
        SoundButton14.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(14);

    }
});    

        Button SoundButton15 = (Button)findViewById(R.id.button17);
                SoundButton15.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(15);

            }
        });
                Button SoundButton16 = (Button)findViewById(R.id.button18);
                SoundButton16.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(16);

            }
        });    
                Button SoundButton17 = (Button)findViewById(R.id.button19);
                SoundButton17.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(17);

            }
        });    
                Button SoundButton18 = (Button)findViewById(R.id.button20);
                SoundButton18.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(18);

            }

                });
                Button back = (Button) findViewById(R.id.button1); 
                back.setOnClickListener(new View.OnClickListener() { 
                    public void onClick(View view) {
                         // fire intent
                        finish(); // finish current activity
                        Intent myIntent = new Intent(view.getContext(), 
                                UFGcharactersActivity.class); 
                        startActivityForResult(myIntent, 0);

                    } 


                }); 





        Button next = (Button) findViewById(R.id.button2); 
        next.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) {
                 // fire intent
                finish(); // finish current activity
                Intent myIntent = new Intent(view.getContext(), 
                        Soundboardb.class); 
                startActivityForResult(myIntent, 0);

            } 


        });}

        };    

I'm hoping its a simple change that I just cant seem to find or work out.

Thank you all in anticipation.

Paul

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

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

发布评论

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

评论(2

安稳善良 2024-12-04 07:00:00

SoundManagerinitSounds() 方法中,将 4 更改为 1:

mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); 

我曾经遇到过同样的问题:)

祝你好运

In your initSounds() method in the SoundManager, change 4 to 1 :

mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); 

I came across the same problem once :)

Good Luck

少女净妖师 2024-12-04 07:00:00

对于简单的应用程序,我使用这个静态声音类。

public class Sound
{
private static MediaPlayer mp = null;

/** Stop old sound and start new one */
public static void play(Context context, int resource)
{
    stop(context);

    mp = MediaPlayer.create(context, resource);
    mp.setLooping(false);
    mp.start();
}

/** Stop the music */
public static void stop(Context context)
{
    if (mp != null)
    {
        mp.stop();
        mp.release();
        mp = null;
    }
}
}

For simple apps I use this static sound class.

public class Sound
{
private static MediaPlayer mp = null;

/** Stop old sound and start new one */
public static void play(Context context, int resource)
{
    stop(context);

    mp = MediaPlayer.create(context, resource);
    mp.setLooping(false);
    mp.start();
}

/** Stop the music */
public static void stop(Context context)
{
    if (mp != null)
    {
        mp.stop();
        mp.release();
        mp = null;
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文