Apps How to add code to a popup window

Hi Guys I have finished my app and added an about button.

On button press this happens:

popup window displays
kill popup
Audio file plays

I want
popup window displays
Audio file plays
kill popup

Is there a way to tell the pop up to play the audio rather than in the main code , what is happening makes sense but I want to change it. Just now the main code launches pop up , popup killed , main code continues and plays audio.


Code:
        switch (item.getItemId()) {
            case R.id.action_settings:

                //pop up
                RelativeLayout = (android.widget.RelativeLayout) findViewById(R.id.relative);
                layoutinflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                ViewGroup container = (ViewGroup) layoutinflater.inflate(R.layout.popup, null);
                Aboutpopup = new PopupWindow(container, 900, 900, true); //1st number hor 2nd vert
                Aboutpopup.showAtLocation(RelativeLayout, Gravity.CENTER, 0, 0); ////1st number hor 2nd vert (SMALLER VERT IS TOP)




//close popup if main window is clicked
                container.setOnTouchListener(new View.OnTouchListener() {
                    [USER=1021285]@override[/USER]
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        final MediaPlayer mp = new MediaPlayer();//gonna play tunes

                        if (mp.isPlaying()) {
                            mp.stop();
                        }
                        try {
                            mp.reset();
                            AssetFileDescriptor afd;
                            afd = getAssets().openFd("355.mp3");
                            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                            mp.prepare();
                            mp.start();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Aboutpopup.dismiss();
                        return true;
                    }


                });
        }
        return super.onOptionsItemSelected(item);
 
Last edited:
D

Deleted User

Guest
Please use [code] tags, and format nicely with indentation. Your code is unreadable.
Thanks.
 
D

Deleted User

Guest
As it stands, if the audio is playing, your code will stop it, then restart it. You need an else clause, something like the code below.
Plus there's something else not quite right with your code. Every time onTouch() is called, you create a new instance of MediaPlayer. This probably isn't what you want to do. Consider making mp a class variable.

As it stands, even if you put the else clause in, mp.isPlaying() will always evaluate to false, as it's a new instance of MediaPlayer, which isn't playing. Result is you always start the MediaPlayer every time onTouch() is called.


Code:
if (mp.isPlaying()) {
                            mp.stop();
                        }
                        else
                            try {
                                mp.reset();
                                AssetFileDescriptor afd;
                                afd = getAssets().openFd("355.mp3");
                                mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                                mp.prepare();
                                mp.start();
                            } catch (IllegalStateException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            Aboutpopup.dismiss();
                        }
                        return true;
 
Top