image shows a screenshot of the app interfaceI am very new to writing any kind of code, and am attempting to build an app that can host two podcasts. I want users to be able to play two separate podcasts on the app, and so need to use two separate onStop methods. However, I get the error:
onStop()' is already defined in "app name"
Is there a way to implement 2 separate onStop methods?
package com.example.breastiesapp;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
public class MindActivity extends AppCompatActivity {
public Button button;
MediaPlayer mediaPlayer;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mind);
mediaPlayer = null;
//Body Button
button = (Button) findViewById(R.id.bodyBTN);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MindActivity.this,BodyActivity.class);
startActivity(intent);
}
});
//Home Button
button = (Button) findViewById(R.id.homeBTN);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MindActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
//Play Pod 1
public void PlayPod1(View view) {
switch (view.getId()) {
case R.id.btnPlayPod1:
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, R.raw.pod1feelmorejoy);
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopPod1();
}
});
mediaPlayer.start();
break;
case R.id.btnPausePod1:
if (mediaPlayer !=null)
mediaPlayer.pause();
break;
case R.id.btnStopPod1:
if (mediaPlayer != null) {
mediaPlayer.stop();
stopPod1();
}
break;
}
}
private void stopPod1() {
mediaPlayer.release();
mediaPlayer = null;
}
#Override
protected void onStop() {
super.onStop();
stopPod1();
}
//Play Pod 2
public void PlayPod2(View view) {
switch (view.getId()) {
case R.id.btnPlayPod2:
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, R.raw.usingmindfulnesstoreducestressandstrengthenyourimmune);
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopPod2();
}
});
mediaPlayer.start();
break;
case R.id.btnPausePod2:
if (mediaPlayer !=null)
mediaPlayer.pause();
break;
case R.id.btnStopPod2:
if (mediaPlayer != null) {
mediaPlayer.stop();
stopPod2();
}
break;
}
}
private void stopPod2() {
mediaPlayer.release();
mediaPlayer = null;
}
#Override
protected void onStop() {
super.onStop();
stopPod1();
}
}
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 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();
}
});
}
}
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.
**Down here I am trying to build a music app in android studio,my app is working fine displaying all my SD card songs and play too ,all button works but when I click on next button it works normally but don't know why app sometimes gets stopped working and shows runtime exception ->>please open the image of run log of the application.
package com.example.ramandeepsingh.tunes;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.jar.Manifest;
public class PlayerActivity extends AppCompatActivity{
static MediaPlayer mp;//assigning memory loc once or else multiple songs will play at once
int position;
SeekBar sb;
ArrayList<File> mySongs;
Thread updateSeekBar;
Button pause,forward,reverse,next,previous;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
pause = (Button)findViewById(R.id.pause);
forward = (Button)findViewById(R.id.forward);
previous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
reverse = (Button)findViewById(R.id.reverse);
sb=(SeekBar)findViewById(R.id.seekBar);
updateSeekBar=new Thread(){
#Override
public void run(){
int totalDuration = mp.getDuration();
int currentPosition = 0;
// sb.setMax(totalDuration);
while(currentPosition < totalDuration){
try{
sleep(500);
currentPosition=mp.getCurrentPosition();
}
catch (InterruptedException e){
e.printStackTrace();
System.out.println(e);
}
sb.setProgress(currentPosition);
}
}
};
if(mp != null){
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songs");
position = b.getInt("pos",0);
Uri u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
if(mp.isPlaying()){
pause.setText(">");
mp.pause();
}
else {
pause.setText("||");
mp.start();
}
}
});
forward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()+5000);
}
});
reverse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()-5000);
}
});
next.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
mp.reset();
position=((position+1)%mySongs.size());
Uri u = Uri.parse(mySongs.get( position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
}
});
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
mp.release();
position=((position-1)<0)?(mySongs.size()-1):(position-1);
Uri u = Uri.parse(mySongs.get( position).toString());//%mysongs so that it do not go to invalid position
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
}
});
}}
I'm trying to make a sort of soundboard app in Android Studio. For now I have three buttons: 2 of them play specific audio files and the other one stops the audio. Everything works except if you play one audio file while another one is going already they both play at the same time and the stop button no longer works on either of them. You have to exit the app. I cannot figure out how to get the first file to quit before playing the second. Here is my code so far:
package com.something.example.com;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button startLie, startTruth,
stopScan;
private MediaPlayer play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startLie = (Button) findViewById(R.id.startlie);
startLie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play = MediaPlayer.create(MainActivity.this, R.raw.lie_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
stopScan = (Button) findViewById(R.id.stopScan);
stopScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play.stop();
};
});
startTruth = (Button) findViewById(R.id.starttruth);
startTruth.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play = MediaPlayer.create(MainActivity.this, R.raw.truth_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
So I seem to have been able to finally get this to work. I basically created a stopPlaying function (placed at the end of my file. and then called it every time before the audio files play. Here's the code again, and this time with a couple more buttons.
package com.something.example;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button startLie, startTruth,
startIdiot, startGenius, stopScan;
private MediaPlayer play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startLie = (Button) findViewById(R.id.startlie);
startLie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.lie_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
stopScan = (Button) findViewById(R.id.stopScan);
stopScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play.stop();
};
});
startTruth = (Button) findViewById(R.id.starttruth);
startTruth.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.truth_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
startIdiot = (Button) findViewById(R.id.startidiot);
startIdiot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.idiot_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
startGenius = (Button) findViewById(R.id.startgenius);
startGenius.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.genius_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
}
private void stopPlaying() {
if (play != null) {
play.stop();
play.release();
play = null;
}
}
The "stop" and "play" work fine, but "pause" works like "stop". When I press pause, the audiofile stops playing and when I press play, it starts from the beginning.
The "stop" and "play" work fine, but "pause" works like "stop". When I press pause, the audiofile stops playing and when I press play, it starts from the beginning.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class HelloMoonFragment extends Fragment
{
private AudioPlayer mPlayer = new AudioPlayer();
private Button mPlayButton;
private Button mStopButton;
private Button mPauseButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
mPlayButton = (Button)v.findViewById(R.id.hellomoon_playButton);
mPlayButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
mPlayer.play(getActivity());
}
});
mPauseButton = (Button) v.findViewById(R.id.hellomoon_pauseButton);
mPauseButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
mPlayer.pause();
}
});
mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
mStopButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
mPlayer.stop();
}
});
return v;
}
#Override
public void onDestroy()
{
super.onDestroy();
mPlayer.stop();
}
}
package com.example.hellomoon;
import android.content.Context;
import android.media.MediaPlayer;
public class AudioPlayer {
private MediaPlayer mPlayer;
public void stop()
{
if(mPlayer != null)
{
mPlayer.release();
mPlayer = null;
}
}
public void pause()
{
if(mPlayer.isPlaying())
{
mPlayer.pause();
}
}
public void play(Context c)
{
stop();
mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
stop();
}
});
mPlayer.start();
}
}
it starts from the beginning because here:
public void play(Context c) {
stop();
mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
you are releasing the old one a create it again
Try remembering the the player is paused:
public class AudioPlayer {
boolean isPaused = false;
public void pause() {
if(mPlayer.isPlaying()) {
mPlayer.pause();
isPaused = true;
}
}
public void play(Context c) {
if (isPaused && mPlayer != null) {
mPlayer.start();
isPaused = false
return;
}
stop();
mPlayer = MediaPlayer.create(c, R.raw.one_small_step);