MediaPlayer crashes on song.start() - java

When I run my app on the emulator, the app runs fine but when I run the app on my actual phone it crashes when I click on the button to use the play method. I used another phone to see if the app will work but it crashes again.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
MediaPlayer song;
boolean playing=false;
int length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
song = MediaPlayer.create(MainActivity.this, R.raw.snakin);
}
public void play(View view){
if(playing==false) {
Button play = (Button) findViewById(R.id.play);
song.seekTo(length);
song.start();
playing=true;
}
}
public void pause(View view){
Button pause=(Button)findViewById(R.id.pause);
song.pause();
length = song.getCurrentPosition();
playing=false;
}
}

I've changed the mp3 file to another one. I really don't know why the first mp3 file only worked on the emulator, but my guess is that the file was corrupted.

You didn't mention listeners to play and pause.
Need to mention listeners for song play and pause operations.
Here, I modified your code, you can try below given code.
public class MainActivity extends AppCompatActivity {
MediaPlayer song;
boolean playing=false;
int length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
song = MediaPlayer.create(MainActivity.this, R.raw.song);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(playing==false) {
song.seekTo(length);
song.start();
playing = true;
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(playing==true) {
song.pause();
length = song.getCurrentPosition();
playing = false;
}
}
});
}
}

Related

I am making an app which if you click on a button an audio will play but the audio is not playing in last two buttons

I am making an app which if you click on a button an audio will play but the audio is not playing in last two buttons and can you also please tell me how can I stop the audio if I click on the audio button again ?? Here's The Full Code. Thanks in Advance. It's in gdsph_btn & aspamm_btn
package com.agrimplayz.radhasoamipaath;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button rrjk_btn, gdsph_btn, aspamm, stop_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rrjk_btn = findViewById(R.id.rrjk_btn);
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.radhasoami_rakshak_jeev_ke);
rrjk_btn.setOnClickListener(new View.OnClickListener() {
int counter = 0;
#Override
public void onClick(View v) {
if(counter == 2)
mediaPlayer.pause();
mediaPlayer.start();
gdsph_btn = findViewById(R.id.gdsph_btn);
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,
R.raw.guru_dhara_sheesh_par_haath);
final MediaPlayer finalMediaPlayer1 = mediaPlayer;
gdsph_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finalMediaPlayer1.start();
}
});
aspamm = findViewById(R.id.gdsph_btn);
mediaPlayer = MediaPlayer.create(MainActivity.this,
R.raw.ae_satguru_pita_aur_malik_mere);
final MediaPlayer finalMediaPlayer = mediaPlayer;
aspamm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finalMediaPlayer.start();
}
});
}
});
}
}
you can try this code:
public class MainActivity extends AppCompatActivity {
private Button rrjk_btn, gdsph_btn, aspamm, stop_btn;
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.radhasoami_rakshak_jeev_ke);
rrjk_btn = findViewById(R.id.rrjk_btn);
rrjk_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 2)
{
mediaPlayer1.pause();
}
mediaPlayer1.start();
counter++;
}
});
//
MediaPlayer mediaPlayer2 = MediaPlayer.create(MainActivity.this, R.raw.guru_dhara_sheesh_par_haath);
gdsph_btn = findViewById(R.id.gdsph_btn);
gdsph_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.start();
}
});
//
aspamm = findViewById(R.id.gdsph_btn);
MediaPlayer mediaPlayer3 = MediaPlayer.create(MainActivity.this, R.raw.ae_satguru_pita_aur_malik_mere);
aspamm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer3.start();
}
});
}
}

Problem in playing audio file using media player

Here is the code for the button which should start and pause the audio.
I have checked the button-text == "start" or "pause" and change the text and use the appropriate methods accordingly
i.e mediaplayer.start() and mediaplayer.pause().
But still, the audio won't play.
package com.example.demo;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
MediaPlayer mediaPlayer;//Mediaplayer
Button button;
public void start(View view)
{
String text = button.getText().toString();
if (text == "start")
{
mediaPlayer.start(); //starting the audio
button.setText("pause");
}
else
{
mediaPlayer.pause(); //pausing the audio
button.setText("start");
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.start);
mediaPlayer = MediaPlayer.create(this,R.raw.music);
}
}
There are a few steps you should try:
First, try this because I think It gives always a false condition:
if(text.trim().equals("start"))
If Still not work Try this one:
public class MainActivity extends AppCompatActivity
{
MediaPlayer mediaPlayer;//Mediaplayer
Button button;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.start);
mediaPlayer = MediaPlayer.create(this,R.raw.music);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = button.getText().toString().trim();
if (text.equals("start"))
{
mediaPlayer.start();
button.setText("pause");
}
else
{
mediaPlayer.pause();
button.setText("start");
}
}
}
} }

Music doesn't play again after I press home or recents button

I have an app with a button to play or pause music. Pressing the back button while playing music pauses it and opening the app again resumes the music after pressing the play button. But that doesn't work with home or recents button. The music pauses but upon reopening the app and pressing the play button doesn't play the music until a force close. Here is the code:
package com.example.firozkaoo2222.myapplication;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import static com.example.firozkaoo2222.myapplication.R.raw.police;
public class MainActivity extends AppCompatActivity {
private MediaPlayer policeSound = MediaPlayer.create(this, police);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button policeSounds = this.findViewById(R.id.police);
policeSounds.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (policeSound == null) {
policeSound = MediaPlayer.create(getApplicationContext(), R.raw.police);
}
if (policeSound.isPlaying()) {
policeSound.pause();
} else {
policeSound.start();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
if (policeSound != null) {
policeSound = MediaPlayer.create(this, R.raw.police);
policeSound.start();
}
}
#Override
public void onPause() {
super.onPause();
if (policeSound.isPlaying())
policeSound.pause();
}
//Back button pressed.
#Override
public void onBackPressed() {
super.onBackPressed();
if (policeSound.isPlaying())
policeSound.pause();
}
#Override
protected void onDestroy() {
super.onDestroy();
policeSound.stop();
policeSound = null;
}
}
If you press home button the app go to the background and when you came from the background you should override the method onResume();
Code:
public class MainActivity extends AppCompatActivity {
//THIS IS YOUR LAST MISTAKE. IF YOU TRY TO CREATE THE OBJECT WITH THE CONTEXT AND THE RESOUERCES
//WHEN THE ACTIVITY IS NOT CREATED YET, YOUR APP CRASH
//private MediaPlayer policeSound = MediaPlayer.create(this, R.raw.police);
private MediaPlayer policeSound;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button policeSounds = this.findViewById(R.id.police);
policeSounds.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (policeSound == null) {
policeSound = MediaPlayer.create(getApplicationContext(), R.raw.police);
}
if (policeSound.isPlaying()) {
policeSound.pause();
} else {
policeSound.start();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
if (policeSound != null) {
policeSound = MediaPlayer.create(this, R.raw.police);
policeSound.start();
}
}
#Override
public void onPause() {
super.onPause();
if (policeSound.isPlaying())
policeSound.pause();
}
//Back button pressed.
#Override
public void onBackPressed() {
super.onBackPressed();
if (policeSound.isPlaying())
policeSound.pause();
}
#Override
protected void onDestroy() {
super.onDestroy();
policeSound.stop();
policeSound = null;
}
}
I hope this help you.

How do I transform a string from EditText to a character array, to allow audio implementation?

This is my code:
package com.example.pembroke.finalalgorhythmic;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button asdf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
asdf = (Button) findViewById(R.id.keybutton);
asdf.setOnClickListener(MainActivity.this);
}
#Override
public void onClick(View v) {
EditText keyInput = (EditText) findViewById(R.id.key);
String notes = keyInput.getText().toString();
}
What I want to do, is create a character array, so that I can use media player class to play certain sounds based on the user input after my button is clicked. Any ideas?
Have you tried
notes.toCharArray() yet?
EDIT: Long version
EDIT 2: Sample implementation
MediaPlayer mp;
char[] currentNotes;
int noteIndex;
// Create the mp in onCreate and register your onCompletion callback
#Override
public void onClick(View v) {
EditText keyInput = (EditText) findViewById(R.id.key);
String notes = keyInput.getText().toString();
currentNotes = notes.toCharArray();
noteIndex = 0;
mp.setDataSource(getNoteResource(currentNotes[0]));
mp.start();
}
// Convert tone values into
public int getNoteResource(char tone) {…}
#Override
public void onCompletion(MediaPlayer mp) {
if(++noteIndex < currentNotes.length) {
mp.setDataSource(getNoteResource(currentNotes[noteIndex]));
mp.start();
}
}

What makes the app crash when launched on my phone?

I'm trying to figure out how progress bars work so i made a simple app where you have a plus and minus button which change the progress of the progress Bar. No errors in Android studio, but it crashes when launched on my phone (Honor 5X)
package net.gamepickle.rcap_new;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
int progressStatus = 50;
ProgressBar progressBar = (ProgressBar) findViewById(R.id.happiness_progress);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void run(){
Button plus = (Button) findViewById(R.id.happiness_plus);
Button minus = (Button) findViewById(R.id.happiness_minus);
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(progressStatus!=100){
progressStatus += 1;
progressBar.setProgress(progressStatus);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(progressStatus!=0){
progressStatus -= 1;
progressBar.setProgress(progressStatus);
}
}
});
}
}
Please add your stack trace.
It seems that you are calling
ProgressBar progressBar = (ProgressBar)findViewById(R.id.happiness_progress);
on the class level.
You need to split it.
Declare: ProgressBar progressBar;
and on the onCreate function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.happiness_progress);
}

Categories