Why do .ogg files loop?

1.2k views Asked by At

In the following class the playback of the .ogg file is triggered.

public class HomeScreenFragment extends Fragment {
    ...
    private AudioPlayer mPlayer = new AudioPlayer();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        ...
        mStandbyButton = (Button)v.findViewById(R.id.standby_button);
        mStandbyButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPlayer.play(getActivity());
            }
        });
    }

    public void onDestroy() {
        super.onDestroy();
        mPlayer.stop();
    }
}

Despite of using the setOnCompletionListener() the files keep on looping unlike .wav

public class AudioPlayer {
    private MediaPlayer mPlayer;

    public void stop() {
        if(mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    public void play(Context c) {
        stop();

        mPlayer = MediaPlayer.create(c, R.raw.hassium);
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                stop();
            }
        });
        mPlayer.start();
    }
}

How to ensure they don't loop once playback is completed? I could possibly work with .wav files but curious as to why this happens and if possible prevent them from looping.

1

There are 1 answers

1
Mixaz On BEST ANSWER

Searching shows that there may be ANDROID_LOOP in ogg metadata: ANDROID_LOOP = true -- how to avoid MediaPlayer looping audios with this metadata tag

Related tickets in Google Code:

MediaPlayer setLooping(false) does not work for ogg files

Should document ANDROID_LOOP flag for OGG files

You can try to use MediaPlayer.setLooping(false) to see if it can help