我如何使用SoundPool与线程?

时间:2013-01-06 13:38:41

标签: android multithreading soundpool

在我的项目中,当用户触摸特殊位置(如图像中的房屋)时,应用程序会发出声音。我有72个职位。

我使用了SoundPool类:

package com.app;
import android.content.Context;

import android.media.AudioManager;

import android.media.SoundPool;


public class sound_pool

{

   private Context pContext;

   private SoundPool sndPool;

   private float rate = 1.0f;

   private float leftVolume = 1.0f;

   private float rightVolume = 1.0f;





    int s_1_1, s_1_2;



   // Constructor, setup the audio manager and store the app context

   public sound_pool(Context appContext)

   {

     sndPool = new SoundPool(72, AudioManager.STREAM_MUSIC, 100);

     pContext = appContext;

     s_1_1 =   load(R.raw.s1_1);

     s_1_2 = load(R.raw.s1_2);


   }


   public void play_s_1_1()

   {

          play(s_1_1);



   }



   public void play_s_1_2()

   {

       play(s_1_2);

   }

   public int load(int sound_id)

   {

          return sndPool.load(pContext, sound_id, 1);

   }





   public void play(int sound_id)

   {


          sndPool.play(sound_id, leftVolume, rightVolume, 1, 0, rate);

   }     

这是我的main_acticity:

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













    // Create an instance of our sound manger
     snd = new sound_pool(getApplicationContext());


     // Set volume rocker mode to media volume
     this.setVolumeControlStream(AudioManager.STREAM_MUSIC);



final ImageView pic = (ImageView) findViewById(R.id.imageView1);

pic.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

    Bitmap bm = BitmapFactory.decodeResource(getResources(),  R.drawable.setar_pre_last_color);

    final float maskW = bm.getWidth();
    final float maskH = bm.getHeight();

     float diffW=1;
      float diffH=1;
     float finalHeight=1;
     float finalWidth=1;

      finalHeight = ax.getMeasuredHeight();
      finalWidth = ax.getMeasuredWidth();
     diffW = finalWidth / maskW;
     diffH = finalHeight / maskH;


    int cordinate_X = (int) ((event.getX()+ v.getScrollX()) /diffW );
    int cordinate_Y = (int) ((event.getY()+ v.getScrollY()) / diffH );
    int pixelColor =  bm.getPixel(cordinate_X , cordinate_Y);   //Get the pixel at coordinates

 System.out.println( pixelColor );

       switch (event.getAction()) {

                   case MotionEvent.ACTION_DOWN:



                       switch ( pixelColor ) {


                       case -65536:

                         snd.play_s_1_1();
                         System.out.println( pixelColor );

                                     break;




                       case -13539997:



                           snd.play_s_1_3();

                           break;




                case MotionEvent.ACTION_UP:


                      System.out.println( pixelColor );



                break;

                } 


                return true;


            }

        });     

如何通过线程使用此项目? 谢谢。

0 个答案:

没有答案