Pregunta

I'm trying to make it so that when i drop and object onto a text view a i thought it would work similar to how i had it set up before only just activated differently. here is the way i'm setting it up.

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 is URL suppressed 03-12 22:49:50.809: I/ViewRootImpl(10720): Reporting drop result: true

¿Fue útil?

Solución

since its a short sound just load it into memory using a sound pool. This way you have very quick access to the sound.

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;

} }

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top