David Read
David Read

Reputation: 1119

Playing sounds inside of a fragment using MediaPlayer

I am creating a soundboard app. The app will feature different "pages" (fragments) that the user can switch between. Each fragment has a number of ImageButtons that, when clicked on, will play a sound.

I put the following code first inside of the MainActivity inside the OnCreate method. After that failed I realized that I'd probably have to put that code inside of each fragment inside the fragment's OnCreateView method.

After doing so I come up for an error for the "findViewById" method. I corrected this by using the "getView()" method directly before the "findViewById" method.

Now I have an error on the "final MediaPlayer player = new MediaPlayer();". The error says that this is an unreachable statement.

I have no idea how to fix this... what can I do to correct this error?

FragmentPage1.java

public static class FragmentPage1 extends Fragment {

    int selectedSoundId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_page1, container, false);
        return rootView;

        final MediaPlayer player = new MediaPlayer();
        final Resources res = getResources();

        final int[] buttonIds = { R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9 };
        final int[] soundIds = { R.raw.num_21, R.raw.whats_9_plus_10, R.raw.a_potato_flew_around_my_room, R.raw.a_week_ago, R.raw.aint_nobody_got_time_for_dat, R.raw.awww, R.raw.baby_lip, R.raw.backflip, R.raw.baliegdeh };


        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < buttonIds.length; i++) {
                    if (v.getId() == buttonIds[i]) {
                        selectedSoundId = soundIds[i];
                        AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
                        player.reset();
                        try {
                            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            player.prepare();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        player.start();
                        break;
                    }
                }
            }
        };

        for (int i = 0; i < buttonIds.length; i++) {
            ImageButton soundButton = (ImageButton) getView().findViewById(buttonIds[i]);
            registerForContextMenu(soundButton);
            soundButton.setOnClickListener(listener);
        }
    }
}

Upvotes: 0

Views: 1054

Answers (1)

StenSoft
StenSoft

Reputation: 9609

The statement is unreachable because you unconditionally return before it can be reached. The fix is simple: move return rootView to the end of the method and instead of getView() use rootView.

Upvotes: 1

Related Questions