user548956
user548956

Reputation: 136

Android MediaPlayer problems

I am triggering a MediaPlayer to play a sound on a button click. Sometimes, the player will play the whole sound, sometimes it will not. It always cuts off on the end. I read a few threads on here where people were having the same problem, but none of the suggestions worked. For example, someone said that adding a mediaPlayer.onCompletionListener() would fix the issue, but it has not.

Here is the code with the MediaPlayer:

public View getView(int position, View convertView, ViewGroup parent) {



    LayoutInflater inflater = getLayoutInflater();
    View row = inflater.inflate(R.layout.vocab_row, parent, false);

    ImageView playIcon = (ImageView) row
                .findViewById(R.id.blueplay_icon);
TextView vocabWord = (TextView) row
        .findViewById(R.id.vocab_text_word);
TextView vocabMeaning = (TextView) row
        .findViewById(R.id.vocab_text_meaning);

vocabWord.setText(data.get(position).getKey());
vocabMeaning.setText(data.get(position).getDefinition());

final String fileName = "audio/" + data.get(position).getAudio();

// set the click listener for the play button
playIcon.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        final MediaPlayer player = new MediaPlayer();
        AssetManager manager = SingleLesson.this.getAssets();
        final AssetFileDescriptor descriptor;

        try {

            descriptor = manager.openFd(fileName);
            long start = descriptor.getStartOffset();
            long end = descriptor.getLength();

            //reset player 
            if (player != null) {
                player.reset();
            }

            player.setDataSource(descriptor.getFileDescriptor(),
                    start, end);

        } catch (IOException e) {
            Log.e("IO EXCEPTION: ", "while getting mp3 assets.");
            e.printStackTrace();
        }

        // set volume
        player.setVolume(100, 100);

        try {
            player.prepare();
        } catch (IllegalStateException e) {
            Log.e("ERROR: ", "media player, illegal state");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("ERROR: ", "media player, IO exception");
            e.printStackTrace();
        }

        player.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer inPlayer) {
                player.start();
            }
        });

        // called when the file is finished playing
        player.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer player) {
                player.stop();
                player.release();
            }

        });
    }

This problem is happening on the emulator but not my htc incredible. It also doesn't happen on my girlfriend's moto droid2, but the sounds do have "clicks" in them on the moto droid2.

Upvotes: 1

Views: 1149

Answers (2)

Peshal
Peshal

Reputation: 1518

I have couple suggestions for you. It might or might not work. MediaPlayer.prepare() is a synchronous method, that means it will complete before any other code down the line runs, so you don't need onPreparedListener to start the playback. you can just type MediaPlayer.start() below the prepare method. Other suggestion is since you wanna play the sound again and again you prolly dont wanna release the mediaplayer. Try putting the MediaPlayer.reset() method instead in the onCompletionListener. Hope it solves the problem. You can also try adding MediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); before the setDataSource method.

Thanks.

Upvotes: 0

bytebender
bytebender

Reputation: 7491

How old is the emulator? I have seen issues with emulators files getting corrupt after some time. If it is not a new one, try creating a one.

Upvotes: 1

Related Questions