Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 673
BNDV Avatar asked Jan 05 '17 10:01

BNDV


People also ask

What class in Android can play audio?

One of the most important components of the media framework is the MediaPlayer class. An object of this class can fetch, decode, and play both audio and video with minimal setup.

How do I play music in the background on my Android?

This example demonstrates how do I play background music in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

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()

like image 93
Arpan Sharma Avatar answered Oct 12 '22 07:10

Arpan Sharma