Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am not able to play a sound in android studio. I am submitting the code below.

What I have tried:

package com.example.lenovo.audio;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

MediaPlayer mediaPlayer;

public void play (View view){

mediaPlayer.start();


}

public void pause(View view){

mediaPlayer.pause();
}



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mediaPlayer = MediaPlayer.create(this, R.raw.jackbox);

}
}
Posted
Updated 11-Oct-18 2:41am
v2
Comments
David Crow 11-Oct-18 10:17am    
Okay, so what's the problem? When do play() and pause() get called? Your onCreate() method creates a MediaPlayer object. Is that object ever used?
Member 14016088 14-Oct-18 6:18am    
Thanks David, actually I am a noob, so please pardon me if this something sounds stupid to you I apologize in advance. In my XML file I have created two buttons namely PAUSE and PLAY which are onclick of which are pause and play respectively Its a simple app, I am just not able to figure out why the audio is not playing on my phone when I run the app.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900