Alin
Alin

Reputation: 14581

Android sound file not playing fully, gets brutally interupted

Scenario

I have a problem that starts to drive me nuts. In one activity I have the following scenario. On create: - create a GPS Listner - create a Location Listener - Start GPS listner and location listener - Start a asyncTask that downloads a map from the web / or load it from cache - play a wav file which is a raw resource, very small (around 63 Kilo) which is short, basically it says: "Let's head to the start point" and it makes visible a textview for 10 seconds.

Problem

Rarely the sound gets play fully. Sometimes I hear only "Let's head", sometimes "Let's head to the star", sometimes no play at all. It's like something else is blocking the sound. I moved the sound in its own thread but still no success:

Code

private void ShowTextPlayVoice(String message, int soundResId)
    {
        txtProceed.setText(message);
        txtProceed.setVisibility(View.VISIBLE);
        final int soundId = soundResId;

        Handler splashHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                    switch (msg.what) {
                    case 0:
                            txtProceed.setVisibility(View.INVISIBLE);
                            break;
                    }
                    super.handleMessage(msg);
            }
        };

            Message msg = new Message();
            msg.what = 0;
            splashHandler.sendMessageDelayed(msg, 10000);

            Thread thread = new Thread() {
                public void run() {
                    MediaPlayer mp = MediaPlayer.create(ctx, soundId);
                    mp.start();
                    mp.setOnCompletionListener(new OnCompletionListener() {
                                    public void onCompletion(MediaPlayer mp) {
                                        mp.release();
                                    }
                                });
                }
            };
            thread.start();
    }

Thank you all for help.

Upvotes: 2

Views: 1691

Answers (1)

Alin
Alin

Reputation: 14581

After some tries, here is a working solution

private MediaPlayer mediaPlayer = new MediaPlayer();

private void ShowTextPlayVoice(String message, int soundResId)
    {
        txtProceed.setText(message);
        txtProceed.setVisibility(View.VISIBLE);

        Handler splashHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                    switch (msg.what) {
                    case 0:
                            txtProceed.setVisibility(View.INVISIBLE);
                            break;
                    }
                    super.handleMessage(msg);
            }
        };

            Message msg = new Message();
            msg.what = 0;
            splashHandler.sendMessageDelayed(msg, 10000);

           new SoundTask().execute(soundResId);
    }

    protected class SoundTask extends AsyncTask<Integer, Void, Void>
    {
        @Override
        protected Void doInBackground(Integer... params) 
        {
            AssetFileDescriptor afd = ctx.getResources().openRawResourceFd(params[0]);

            try
            {   
                //final MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.reset();
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                mediaPlayer.prepare();
                mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                    public void onPrepared(MediaPlayer arg0) {
                         mediaPlayer.seekTo(0);
                         mediaPlayer.start();
                                                });
                    }
                });
                afd.close();
            }
            catch (Exception e) {
            }

            return null;
        }
    }

Upvotes: 5

Related Questions