MediaPlayer mp.stop() not stopping - android

I have a music which I want it to be as a background music for the game I made.
I want it to stop when score becomes empty in order to start another one.
But it does not stop by using mp.stop(); !
My code:
public class countgame extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countgame);
final MediaPlayer mp = MediaPlayer.create(countgame.this, R.raw.game);
mp.start();
final ImageView i1 = (ImageView)findViewById(R.id.imageView1);
final ImageView i2 = (ImageView)findViewById(R.id.imageView2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
incorrect();
}
});
}
public void incorrect(){
final ImageView score = (ImageView) findViewById(R.id.i3);
if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score3).getConstantState())){
score.setBackgroundResource(R.drawable.score2);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score2).getConstantState())){
score.setBackgroundResource(R.drawable.score1);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score1).getConstantState())){
score.setBackgroundResource(R.drawable.empty);
MediaPlayer m = MediaPlayer.create(countgame.this, R.raw.game);
m.stop();//here is the problem
MediaPlayer mp=MediaPlayer.create(countgame.this, R.raw.gameover);
mp.start();
}
}

Change your code to this:
public class countgame extends Activity implements OnClickListener{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countgame);
mp = MediaPlayer.create(countgame.this, R.raw.game);
mp.start();
final ImageView i1 = (ImageView)findViewById(R.id.imageView1);
final ImageView i2 = (ImageView)findViewById(R.id.imageView2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
incorrect();
}
});
}
public void incorrect(){
final ImageView score = (ImageView) findViewById(R.id.i3);
if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score3).getConstantState())){
score.setBackgroundResource(R.drawable.score2);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score2).getConstantState())){
score.setBackgroundResource(R.drawable.score1);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score1).getConstantState())){
score.setBackgroundResource(R.drawable.empty);
mp.stop();//here is the problem
mp=MediaPlayer.create(countgame.this, R.raw.gameover);
mp.start();
}
}
}

Related

How can I make my ImageButton behave differently depending on ToggleButton status?

I am trying to add a toggle button, so that my image button switches the direction that it translates based on the status of the toggle button. However, I am running into problems when trying to add it to the media player, and when using the if statement.
public class MainActivity extends AppCompatActivity {
TextToSpeech tts1;
ToggleButton toggleButton;
public void changeTranslation(View view){
boolean on = ((ToggleButton)view).isChecked();
if(on){
toggleButton.setText("Speech-to-Farts");
Toast.makeText(getApplicationContext(),"Translating speech to farts.",Toast.LENGTH_SHORT);
} else{
toggleButton.setText("Farts-to-Speech");
Toast.makeText(getApplicationContext(),"Translating farts to speech.",Toast.LENGTH_SHORT);
}
}
private static final String TAG = "MainActivity";
private MediaPlayer mMediaPlayer;
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mediaPlayer){
releaseMediaPlayer();
}
};
private void releaseMediaPlayer(){
if(mMediaPlayer != null){
mMediaPlayer.release();
mMediaPlayer=null;
}
}
TextView tv1;
ImageButton b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(textView);
b1 = (ImageButton) findViewById(R.id.recordButton);
final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle_button);
toggleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (toggleButton.isChecked()) {
toggleButton.setText("Farts-to-Speech");
} else {
toggleButton.setText("Speech-to-Farts");
}
}
});
tts1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts1.setLanguage(Locale.US);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onClick(View v) {
int[] fartLibrary={};
String[] speechLibrary={};
if(toggleButton==true) {
releaseMediaPlayer();
String randomTranslation = (speechLibrary[new Random().nextInt(speechLibrary.length)]);
tts1.speak(randomTranslation, TextToSpeech.QUEUE_FLUSH, null);
} else {
releaseMediaPlayer();
mMediaPlayer=MediaPlayer.create(this,fartLibrary);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
}
}
This is my Translations.java. I'm not sure if this is necessary anymore though
public class Translations{
private String mSpeechTranslation;
private int mFartTranslation;
public Translations(String speechTranslation, int fartTranslation){
mFartTranslation = fartTranslation;
mSpeechTranslation = speechTranslation;
}
public String getmSpeechTranslation(){ return mSpeechTranslation;}
public int getmFartTranslation(){ return mFartTranslation;}
}

How to make ImageButton repeat a sound each time it's clicked

I am trying to get the Image Button to play a sound each time it's clicked. For example, if the sound is playing for few seconds and I press the button again it should start from the beginning. I managed to get the sound working but it's not repeating it. How can I get it to start over?
ImageButton bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
}
Try this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, R.raw.bird);
}
mp.start();
} catch(Exception e) {
e.printStackTrace();
}
}
Use MediaPlayer.seekto() to seek to the start of the audio file.
ImageButton bt;
private MediaPlayer mp = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.seekTo(0);
} else {
mp.start();
}
}
});
}

Why is VideoView not playing Video?

I am trying to play a video by starting it when the Activity starts.
However,I am just getting a black screen when the Activity starts.
I do not understand why this is occurring.
Is this not the proper way to configure and play a video?
HelloMoonFragment:
public class HelloMoonFragment extends Fragment {
private AudioPlayer mPlayer = new AudioPlayer();
private Button mPlayButton;
private Button mStopButton;
private Button mPauseButton;
private Button mResumeButton;
private Button mStartVideo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#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() {
#Override
public void onClick(View v) {
mPlayer.play(getActivity());
}
});
mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
mStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.stop();
}
});
mResumeButton = (Button)v.findViewById(R.id.helloMoon_startButton);
mResumeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.resume();
}
});
mPauseButton = (Button)v.findViewById(R.id.hellomoon_pauseButton);
mPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.pause();
}
});
mStartVideo = (Button)v.findViewById(R.id.hellomoon_startVideo);
mStartVideo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),VideoActivity.class);
startActivity(i);
}
});
return v;
}
#Override
public void onDestroy(){
super.onDestroy();
mPlayer.stop();
}
}
VideoActivity:
public class VideoActivity extends Activity {
private String mVideoPath = "android.resource://" + "com.tubbs.android.hellomoon" + "/" + "R.raw/apollo_17_stroll";
private Uri mVideo;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
mVideo = Uri.parse(mVideoPath);
VideoView videoView = (VideoView)findViewById(R.id.hellomoon_startVideo);
MediaController mc = new MediaController(VideoActivity.this);
mc.setAnchorView(videoView);
videoView.setMediaController(mc);
videoView.start();
}
}
activity_video.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="#+id/hellomoon_startVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

loop for array of images and audio

I am making an app in which there are many images and audio. All I want to do is apply a loop for these images and audio so that when an image gets displayed the audio will get played, i.e. image1=audio1, image2=audio2. There are next and previous buttons in my app. Next will display the next image and audio, previous will display previous one. Should I use a for loop? or switch case? I have tried this code. But when the images and audio array comes to an end I want to start all over again. Please help.
public class MainActivity extends Activity {
int i=1;
MediaPlayer mediaplayer;
int image = 0;
int sound ;
int [] sounds;
int [] audio;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sounds = new int [] {
R.raw.audio_one, R.raw.audio_two, R.raw.audio_three, R.raw.audio_four
};
mediaplayer = MediaPlayer.create(this, sounds[0]);
mediaplayer.start();
final int [] images = {
R.drawable.num1, R.drawable.num2, R.drawable.num3, R.drawable.num4
};
ImageButton next = (ImageButton)findViewById(R.id.imageButton1);
ImageButton prev = (ImageButton)findViewById(R.id.imageButton2);
final ImageView img1 = (ImageView)findViewById(R.id.imageView2);
final ImageView img2 = (ImageView)findViewById(R.id.imageView3);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(image==3){
}
else {
image++;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (image<=0){
img1.setImageResource(R.drawable.num1);
}
else
{
image--;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
}
public void audio(int a)
{
mediaplayer = MediaPlayer.create(this, sounds[a]);
return;
}
}
use the following code...
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(image== images.lenght()-1){
image=0;
}
else {
image++;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (image==0){
img1.setImageResource(R.drawable.num1);
}
else
{
image--;
}
i=image;
audio(i);
mediaplayer.start();
img1.setImageResource(images[image]);
}
});
}

My code won't run?

I am working with Fragments however this code wont run. can someone help me with this. The problem in the code seems to be with this part "(start.this,".
public class frag extends FragmentActivity {
public MediaPlayer mp = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(start.this, R.raw.praise1);
mp.start();
}else {
mp.stop();
mp = null;
} }
});
}
}
Change your code to
public class frag extends FragmentActivity {
public MediaPlayer mp = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(getApplicationContext(), R.raw.praise1);//==> Here is the change
mp.start();
}else {
mp.stop();
mp = null;
} }
});
}
}
Change start.this to an actual context. You could use getParent() or you could store the context before creating the onClickListener and pass it in:
setContentView(R.layout.activity_main);
final Context context = this;
//inside the onClickListener
mp = MediaPlayer.create(context, R.raw.praise1);

Categories

Resources