Example usage for android.media MediaPlayer create

List of usage examples for android.media MediaPlayer create

Introduction

In this page you can find the example usage for android.media MediaPlayer create.

Prototype

public static MediaPlayer create(Context context, int resid) 

Source Link

Document

Convenience method to create a MediaPlayer for a given resource id.

Usage

From source file:com.example.android.gft.NumbersFragment.java

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

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(/*w  w w. j  a  v  a 2  s . c  om*/
            new Word(R.string.number_one, R.string.miwok_number_one, R.drawable.number_one, R.raw.number_one));
    words.add(
            new Word(R.string.number_two, R.string.miwok_number_two, R.drawable.number_two, R.raw.number_two));
    words.add(new Word(R.string.number_three, R.string.miwok_number_three, R.drawable.number_three,
            R.raw.number_three));
    words.add(new Word(R.string.number_four, R.string.miwok_number_four, R.drawable.number_four,
            R.raw.number_four));
    words.add(new Word(R.string.number_five, R.string.miwok_number_five, R.drawable.number_five,
            R.raw.number_five));
    words.add(
            new Word(R.string.number_six, R.string.miwok_number_six, R.drawable.number_six, R.raw.number_six));
    words.add(new Word(R.string.number_seven, R.string.miwok_number_seven, R.drawable.number_seven,
            R.raw.number_seven));
    words.add(new Word(R.string.number_eight, R.string.miwok_number_eight, R.drawable.number_eight,
            R.raw.number_eight));
    words.add(new Word(R.string.number_nine, R.string.miwok_number_nine, R.drawable.number_nine,
            R.raw.number_nine));
    words.add(
            new Word(R.string.number_ten, R.string.miwok_number_ten, R.drawable.number_ten, R.raw.number_ten));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_numbers);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}

From source file:com.joshmoles.byobcontroller.ControlActivity.java

/**
 * Plays raw game audio provided a resource location.
 * @param resource The android resource ID of audio to play.
 *//*from w ww  .j  a  v  a  2s  . c  o  m*/
private void playAudio(int resource) {
    MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), resource);
    mediaPlayer.start();
    while (mediaPlayer.isPlaying())
        android.os.SystemClock.sleep(1000);
    mediaPlayer.release();
    Log.v(Consts.LOAD_FETCH, "Audio released");
}

From source file:com.example.android.gft.FamilyFragment.java

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

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(R.string.family_father, R.string.miwok_family_father, R.drawable.family_father,
            R.raw.family_father));/*from   w  w w .java  2s . c o m*/
    words.add(new Word(R.string.family_mother, R.string.miwok_family_mother, R.drawable.family_mother,
            R.raw.family_mother));
    words.add(
            new Word(R.string.family_son, R.string.miwok_family_son, R.drawable.family_son, R.raw.family_son));
    words.add(new Word(R.string.family_daughter, R.string.miwok_family_daughter, R.drawable.family_daughter,
            R.raw.family_daughter));
    words.add(new Word(R.string.family_older_brother, R.string.miwok_family_older_brother,
            R.drawable.family_older_brother, R.raw.family_older_brother));
    words.add(new Word(R.string.family_younger_brother, R.string.miwok_family_younger_brother,
            R.drawable.family_younger_brother, R.raw.family_younger_brother));
    words.add(new Word(R.string.family_older_sister, R.string.miwok_family_older_sister,
            R.drawable.family_older_sister, R.raw.family_older_sister));
    words.add(new Word(R.string.family_younger_sister, R.string.miwok_family_younger_sister,
            R.drawable.family_younger_sister, R.raw.family_younger_sister));
    words.add(new Word(R.string.family_grandmother, R.string.miwok_family_grandmother,
            R.drawable.family_grandmother, R.raw.family_grandmother));
    words.add(new Word(R.string.family_grandfather, R.string.miwok_family_grandfather,
            R.drawable.family_grandfather, R.raw.family_grandfather));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_family);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}

From source file:nth.com.ares.utils.Utils.java

public static void playSound(Context context, int sound) {
    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
    int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    float percent = 0.7f;
    int seventyVolume = (int) (maxVolume * percent);
    audio.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
    final MediaPlayer mp = MediaPlayer.create(context, sound);
    mp.start();/*from ww  w  .j a v a2 s . co m*/
}

From source file:com.handmark.pulltorefresh.library.internal.LoadingLayout.java

public void releaseToRefresh() {
    if (!mArrowRotated) {
        mHeaderArrow.startAnimation(mRotateAnimation);
        rotateArrow();/*from   w ww .ja  v a2  s.c o  m*/
        mArrowRotated = true;
        if (!hasPlayedSound) {
            hasPlayedSound = true;
            boolean isMuted = false;

            switch (audioManager.getRingerMode()) {
            case AudioManager.RINGER_MODE_NORMAL:
                isMuted = false;
                break;
            case AudioManager.RINGER_MODE_SILENT:
                isMuted = true;
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                isMuted = true;
                break;
            }
            if (mPreferences.getBoolean(PREFERENCE_KEY_SOUND_NAVIGATION, true)) {
                if (isMuted != true) {
                    if (mPlayer != null) {
                        if (mPlayer.isPlaying()) {
                            mPlayer.stop();
                        }
                        mPlayer.release();
                    }
                    mPlayer = MediaPlayer.create(mContext, R.raw.pulldown);
                    mPlayer.start();
                }
            }
        }
    }
    mHeaderText.setText(Html.fromHtml(mReleaseLabel));
}

From source file:bucci.dev.freestyle.TimerService.java

private void playBeep() {
    if (beepPlayer == null) {
        switch (getTimerTypeValue()) {
        case BATTLE_TIME_VALUE:
            beepPlayer = MediaPlayer.create(TimerService.this, R.raw.airhorn);
            break;
        case QUALIFICATION_TIME_VALUE:
            beepPlayer = MediaPlayer.create(TimerService.this, R.raw.beep2);
            break;
        }/*www  .j a  v  a  2  s  .c o  m*/

    }

    beepPlayer.start();
}

From source file:bucci.dev.freestyle.TimerService.java

private void playFinishBeep() {
    beepPlayer = MediaPlayer.create(TimerService.this, R.raw.airhorn_finish);
    beepPlayer.start();
}

From source file:com.example.zoeoeh.ETuner.OpenGLClass.java

private void playSound(Context context, int soundID) {
    // Play sound from given ID number

    Log.d(TAG, "sound playing sound ID " + soundID + " current sound " + currentSoundIndex);

    // if not null reset and release player to be initialised again
    if (myPlayer != null) {
        myPlayer.reset();/*w  w  w.  ja v  a  2  s  .  c o  m*/
        myPlayer.release();
    }

    // prepare media player with sound source
    myPlayer = MediaPlayer.create(context, myStrings[soundID]);

    // set which chosen string it is
    TestRenderer.setChosenString(soundID);

    // set looping
    myPlayer.setLooping(loopingChecked);

    // start playing and reset flag
    myPlayer.start();
    shakeDirty = false;

}

From source file:me.tylerbwong.pokebase.gui.activities.PokemonProfileActivity.java

private void playAudio() {
    final MediaPlayer player = MediaPlayer.create(this,
            getResources().getIdentifier(AUDIO + pokemonId, RAW, getPackageName()));
    player.setOnCompletionListener(mediaPlayer -> player.release());
    player.start();// w w  w  . j av a  2 s. co m
    Toast.makeText(this, getString(R.string.sound_played), Toast.LENGTH_SHORT).show();
}

From source file:com.eyetools.pediatrics.pediatricfixation.MainFixationFragment.java

@Override
public void onResume() {
    super.onResume();

    /* start or restart the sound player */
    soundPlayer = MediaPlayer.create(mainAct, ((Anim) mViewTypes.get(mMajorViewType).get(mViewIndex)).resId);
    soundPlayer.setLooping(true);//  w ww . j a  va2 s  .co  m

    if (mSound) {
        Log.d(TAG, "onResume, plan to start sound");
        try {
            soundPlayer.start();
        } catch (Exception e) {
            Log.d(TAG, "error onResume starting sound player");
        }
    } else {
        Log.d(TAG, "onResume, no plan to start sound");
    }
}