Fabian Montossi
Fabian Montossi

Reputation: 189

How to put a method in this app?

i have the next code, and i want to put a method (i am newbie and don´t get how can i do that). Because when i am playing the sound and finish the app, this continue and can´t stop it, and if i put mp.pause(); before this finish(by Exit button) works, BUT if i NEVER pressed Play, my app don´t play the sound after this. With methods i mean: if (SoundPlaying) mp.pause();

Code:

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity {


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

    final AdView adView = (AdView) findViewById(R.id.adView);
      adView.loadAd(new AdRequest());

    final MediaPlayer mp = MediaPlayer.create(this, R.raw.rain);

    final ImageView Play = (ImageView) findViewById(R.id.imageView1);
        Play.setOnClickListener(new OnClickListener(){



        @Override
        public void onClick(View v) {
            Animation Rotate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
                Play.startAnimation(Rotate);

            mp.start();

        }});


        final ImageView Pause = (ImageView) findViewById(R.id.imageView2);
        Pause.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

             mp.pause();
             Animation Rotate = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
                Pause.startAnimation(Rotate);

            }});


        final ImageView Exit = (ImageView) findViewById(R.id.imageView3);
        Exit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {



                 AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                         MainActivity.this);

                 // Setting Dialog Title
                 alertDialog.setTitle("Do you want to exit?");

                 // Setting Dialog Message
                 alertDialog.setMessage("If you liked the app, please Rate us!");

                 // Setting Icon to Dialog
                 alertDialog.setIcon(R.drawable.exit2);

                 // Setting Positive Yes Button
                 alertDialog.setPositiveButton("Yes",
                         new DialogInterface.OnClickListener() {

                             public void onClick(DialogInterface dialog,
                                     int which) {
                                 // User pressed Yes button. Write Logic Here
                                finish();
                             }
                         });
                 // Setting Neutral Button
                 alertDialog.setNeutralButton("Rate us!",
                         new DialogInterface.OnClickListener() {

                             public void onClick(DialogInterface dialog,
                                     int which) {
                                    Uri uri = Uri.parse("market://details?id=" + getPackageName());
                                    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                                    try {
                                        startActivity(goToMarket);
                                    } catch (ActivityNotFoundException e) {
                                     Toast.makeText(getApplicationContext(), "Couldn´t launch Google Play",
                                       Toast.LENGTH_LONG).show();
                                    }

                             }
                         });
                 // Setting Positive "Cancel" Button
                 alertDialog.setNegativeButton("No",
                         new DialogInterface.OnClickListener() {

                             public void onClick(DialogInterface dialog,
                                     int which) {

                             }
                         });
                 // Showing Alert Message
                 alertDialog.show();

             }
         });

      //Orientación de la APP Vertical
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

} 

Upvotes: 0

Views: 89

Answers (2)

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4377

When you are pausing sound after that write int length = mp.getCurrentPosition();

Like-

int length;
mp.pause();
length = mp.getCurrentPosition();

When user click no button write following code to continue playing from recent postion.

mp.seekTo(length);
mp.start();

And for yes button @Nood already answered.

Upvotes: 1

Noob
Noob

Reputation: 534

I think you mean "How do i implement an if statement" rather than method. You need to check if music is playing, and stop it if it is. Then once it has been stopped you can finish the activity.

alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        if (mp.isPlaying()){
            mp.stop();
        }
        finish();
    }
});

Upvotes: 2

Related Questions