BNDV
BNDV

Reputation: 73

Android MediaPlayer not playing

Following problem: I just want to play a small .wav file with a mediaplayer in Android. The .wav file is in the raw directory. I want to start the sound from the MainActivity. This is how I play the sound:

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound1);
mp.start();

As soon as I call mp.start() I get following errorsin my LogCat:

01-05 11:07:17.729 19960-19960/com.example.simplesound E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
01-05 11:07:17.729 19960-19960/com.example.simplesound E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
01-05 11:07:17.862 19960-19960/com.example.simplesound D/MediaPlayer: setSubtitleAnchor in MediaPlayer
01-05 11:07:17.925 19960-21184/com.example.simplesound E/MediaPlayer: error (1, -2147483648)
01-05 11:07:17.926 19960-19960/com.example.simplesound E/MediaPlayer: Error (1,-2147483648)

Anyone knows how tho fix that?

Upvotes: 7

Views: 16614

Answers (4)

Harshitha Palihawadana
Harshitha Palihawadana

Reputation: 2211

I think your MediaPlayer is working. Check the device 'Media volume' in settings. JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 this message comes when the device 'Media volume' set to zero.

Upvotes: 3

Krupa Kakkad
Krupa Kakkad

Reputation: 857

Try this: Playing short .wav files - Android

SoundPool is the best way to play short .wav(2-3 sec.) file

For more information: https://developer.android.com/reference/android/media/SoundPool.html

Upvotes: 3

Arpan Sharma
Arpan Sharma

Reputation: 2162

MediaPlayer player = MediaPlayer.create(this,  R.raw.zindgivalaaop);
player.setVolume(50,50);
player.start();

@Override
public void onDestroy() {
     super.onDestroy();
     player.stop();
     player.release();
}

try passing class context instead getApplicationContext()

Upvotes: 3

Chingiz
Chingiz

Reputation: 332

Use the code below which answered here. It'll work like a charm.

package com.example.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;

public class AudioPlayer {

private MediaPlayer mMediaPlayer;

public void stop() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

public void play(Context c, int rid) {
    stop();

    mMediaPlayer = MediaPlayer.create(c, rid);
    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            stop();
        }
    });

    mMediaPlayer.start();
}

}

Upvotes: 3

Related Questions