简体   繁体   中英

Sound not replaying on onTouchListener

So I have a button that, when held, will play a sound. However, it does not seem to "reset" and never plays that sound again when I hold down the button again. I get a

E/MediaPlayer﹕ start called in state 0 E/MediaPlayer﹕ error (-38, 0)

error in log when I try to hold the button down again. Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
  final MediaPlayer bark = MediaPlayer.create(this, R.raw.bark);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    playSound = (Button) findViewById(R.id.btn);

    playSound.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if(!bark.isPlaying()) bark.start();
                    break;
                case MotionEvent.ACTION_UP:
                    if(bark.isPlaying()) bark.stop();
                    break;
            }
            return true;
        }
    });
}

Instead of calling start/stop you can call start/pause with seekTo() :

           case MotionEvent.ACTION_DOWN:
                if(!bark.isPlaying()) bark.start();
                break;
            case MotionEvent.ACTION_UP:
                if(bark.isPlaying()){
                bark.pause();
                bark.seekTo(0);
                }
                break;

If you want to use start/stop, make sure you call prepare() before calling start() again.

The reason why your code isn't working is because according to the state diagram stop() stops playing and puts the MP in a stopped state. Before starting to play for the next time, you should call prepare() and then start() again.

EDIT : Place your music file in the assets folder and use the file descriptor. This way you can reset the MP and replay:

Using START/STOP:

@Override
protected void onCreate(Bundle savedInstanceState) {
AssetManager assetManager=Context.getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd("bark.mp3"); //replace with right extension
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
playSound = (Button) findViewById(R.id.btn);
playSound.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    if(!bark.isPlaying()){
                     bark.reset();
                    bark.setDataSource(fileDescriptor.getFileDescriptor());
                    bark.prepare();
                    bark.start();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(bark.isPlaying())
                    bark.stop();                        
                    break;
         }
            return true;
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM