丢弃对象Android时播放声音

时间:2014-03-12 23:11:30

标签: android media-player audio

我试图做到这一点,以便当我放下并反对到文本视图时,我认为它的工作方式类似于我之前设置的方式,只是以不同方式激活。这是我设置它的方式。

MediaPlayer mysound;
TextView target =(TextView) findViewById(R.id.task1);
target.setOnDragListener(dragListener);

OnDragListener dragListener = new OnDragListener()
{
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
            int dragEvent = event.getAction();
            //TextView dropText = (TextView) v;

            switch(dragEvent)
            {
                case DragEvent.ACTION_DRAG_ENTERED:

                break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:
                    mysound=MediaPlayer.create(Quiz.this, R.raw.error );
                    mysound.setOnCompletionListener(new OnCompletionListener() {
                        public void onCompletion(MediaPlayer mysound) {
                            // TODO Auto-generated method stub
                            mysound.release();
                        }

                    });   
                    mysound.start();
                    break;
            }

            return true;
    }
};

log cat

  

03-12 22:49:50.799:D / MediaPlayer(10720):start()mUri是URL被抑制     03-12 22:49:50.809:I / ViewRootImpl(10720):报告丢弃结果:true

1 个答案:

答案 0 :(得分:1)

因为它的声音很短,所以只需使用声音池将其加载到内存中即可。这样您就可以非常快速地访问声音。

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.TextView;



public class MainActivity extends Activity implements OnDragListener {


boolean loaded=false;
SoundPool soundPool;
int soundID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // here use the file name that task1 textview is contained in
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

     soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
                loaded = true;
            }
        });
        soundID = soundPool.load(this, R.raw.error, 1);


        TextView target =(TextView) findViewById(R.id.task1);
        target.setOnDragListener(this);


       }

@Override
public boolean onDrag(View v, DragEvent event) {
    int dragEvent = event.getAction();
    //TextView dropText = (TextView) v;

    switch(dragEvent)
    {
        case DragEvent.ACTION_DRAG_ENTERED:

        break;

        case DragEvent.ACTION_DRAG_EXITED:
            break;

        case DragEvent.ACTION_DROP:
         if (loaded) {
              AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
              float actualVolume = (float) audioManager
                      .getStreamVolume(AudioManager.STREAM_MUSIC);
              float maxVolume = (float) audioManager
                      .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
              float volume = actualVolume / maxVolume;

        soundPool.play(soundID, volume, volume, 1, 0, 1f);
        Log.e("Test", "Played sound");
    }
            break;
    }

    return true;

}     }