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:net.ddns.mlsoftlaberge.trycorder.contacts.ContactAdminFragment.java

private void buttonsound() {
    MediaPlayer mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), R.raw.keyok2);
    mediaPlayer.start(); // no need to call prepare(); create() does that for you
}

From source file:camera2basic.Camera2BasicFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
    sonidoFoto = MediaPlayer.create(getActivity(), R.raw.sonido);
}

From source file:ca.taglab.PictureFrame.ScreenSlidePageFragment.java

private void messageSent(View v) {
    switch (v.getId()) {
    case R.id.video:
        mConfirmation.setImageResource(R.drawable.confirm_red);
        break;/*from  ww w  .java2s.  co  m*/

    case R.id.photo:
        mConfirmation.setImageResource(R.drawable.confirm_blue);
        break;

    case R.id.audio:
        mConfirmation.setImageResource(R.drawable.confirm_green);
        break;

    case R.id.wave:
        mConfirmation.setImageResource(R.drawable.confirm_orange);
        break;
    }

    MediaPlayer mPlayer = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.success_low);
    mPlayer.start();

    mConfirmation.setAlpha(0f);
    mConfirmation.setVisibility(View.VISIBLE);
    mConfirmation.animate().alpha(1f).setDuration(mLongAnimationDuration * 2)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    mConfirmation.animate().alpha(0f).setDuration(mLongAnimationDuration * 2)
                            .setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    mConfirmation.setVisibility(View.GONE);
                                }
                            });
                }
            });

}

From source file:com.google.android.apps.santatracker.games.matching.MemoryMatchFragment.java

private void loadBackgroundMusic() {
    mBackgroundMusic = MediaPlayer.create(getActivity(), R.raw.santatracker_musicloop);
    mBackgroundMusic.setLooping(true);/*  ww w  .j  a  v  a 2s. c  o m*/
    mBackgroundMusic.setVolume(.1f, .1f);
    mBackgroundMusic.start();
}

From source file:com.google.android.apps.santatracker.games.gumball.TiltGameFragment.java

private void loadBackgroundMusic() {
    mBackgroundMusic = MediaPlayer.create(getActivity(), R.raw.santatracker_musicloop);
    mBackgroundMusic.setLooping(true);/* ww  w.  j a v a  2 s . com*/
    mBackgroundMusic.setVolume(.2f, .2f);
    mBackgroundMusic.start();
}

From source file:com.jungle.base.utils.MiscUtils.java

public static void playSound(Context context, int soundResId) {
    final MediaPlayer player = MediaPlayer.create(context, soundResId);
    if (player == null) {
        return;/*from w ww .ja va  2 s  .  c o m*/
    }

    AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int currVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);

    float volume = 1.0f;
    if (maxVolume > 0) {
        volume = (float) currVolume / (float) maxVolume;
    }

    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            player.release();
        }
    });

    player.setVolume(volume, volume);
    player.start();
}

From source file:com.android.server.telecom.testapps.TestConnectionService.java

private MediaPlayer createMediaPlayer() {
    // Prepare the media player to play a tone when there is a call.
    MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.beep_boop);
    mediaPlayer.setLooping(true);/*from   w w w .  j a v  a  2s  . c  om*/
    return mediaPlayer;
}

From source file:com.saulcintero.moveon.services.MoveOnService.java

private void playSound(int id) {
    switch (id) {
    case 1:/*from w  ww.  j a v  a 2s .  c om*/
        mMediaPlayer = MediaPlayer.create(this, R.raw.beep);
        break;
    case 2:
        mMediaPlayer = MediaPlayer.create(this, R.raw.coach_whistle);
        break;
    }

    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });

    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.stop();
            mp.release();
        }
    });
}

From source file:com.aimfire.demo.CameraActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG)
        Log.d(TAG, "create CameraActivity");

    checkPreferences();//  w ww .j  av  a  2s .  c o  m

    /*
     *  keep the screen on until we turn off the flag 
     */
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    /*
     * Obtain the FirebaseAnalytics instance.
     */
    mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

    /*
     * disable nfc push
     */
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter != null)
        nfcAdapter.setNdefPushMessage(null, this);

    /*
     * get the natural orientation of this device. need to be called before
     * we fix the display orientation
     */
    mNaturalOrientation = getDeviceDefaultOrientation();

    /*
     * force CameraActivity in landscape because it is the natural 
     * orientation of the camera sensor
     */
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    mLandscapeOrientation = getDeviceLandscapeOrientation();

    Bundle extras = getIntent().getExtras();
    if (extras == null) {
        if (BuildConfig.DEBUG)
            Log.e(TAG, "onCreate: error create CameraActivity, wrong parameter");
        finish();
        return;
    }

    /*
     *  make sure we have camera
     */
    if (!checkCameraHardware(this)) {
        if (BuildConfig.DEBUG)
            Log.e(TAG, "onCreate: error create CameraActivity, cannot find camera!!!");
        finish();
        return;
    }

    mIsLeft = extras.getBoolean(MainConsts.EXTRA_ISLEFT);

    mView3DButton = (ImageButton) findViewById(R.id.view3D_button);
    mExitButton = (ImageButton) findViewById(R.id.exit_button);
    mCaptureButton = (ImageButton) findViewById(R.id.capture_button);
    mPvButton = (ImageButton) findViewById(R.id.switch_photo_video_button);
    mFbButton = (ImageButton) findViewById(R.id.switch_front_back_button);
    mLevelButton = (Button) findViewById(R.id.level_button);
    mModeButton = (ImageButton) findViewById(R.id.mode_button);

    if (mIsLeft) {
        mCaptureButton.setImageResource(R.drawable.ic_photo_camera_black_24dp);
    } else {
        mCaptureButton.setVisibility(View.INVISIBLE);

        mPvButton.setVisibility(View.INVISIBLE);
        mFbButton.setVisibility(View.INVISIBLE);
    }

    mView3DButton.setOnClickListener(oclView3D);
    mExitButton.setOnClickListener(oclExit);
    mPvButton.setOnClickListener(oclPV);
    mFbButton.setOnClickListener(oclFB);
    mCaptureButton.setOnClickListener(oclCapture);

    /*
     * we could get here in two ways: 1) directly after MainActivity ->
     * AimfireService sync with remote device. 2) we could get here 
     * because of a switch from video to photo mode. 
     * 
     * mSyncTimeUs is determined by AudioContext. each device 
     * calculates it, and they correspond to the same absolute moment 
     * in time
     */
    mSyncTimeUs = extras.getLong(MainConsts.EXTRA_SYNCTIME, -1);

    /*
     * start camera client object in a dedicated thread
     */
    mCameraClient = new CameraClient(Camera.CameraInfo.CAMERA_FACING_BACK, PHOTO_DIMENSION[0],
            PHOTO_DIMENSION[1]);

    /*
     * create our SurfaceView for preview 
     */
    mPreview = new CameraPreview(this);
    mPreviewLayout = (AspectFrameLayout) findViewById(R.id.cameraPreview_frame);
    mPreviewLayout.addView(mPreview);
    mPreviewLayout.setOnTouchListener(otl);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "add camera preview view");

    mShutterSoundPlayer = MediaPlayer.create(this, Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));

    /*
     * place UI controls at their initial, default orientation
     */
    adjustUIControls(0);

    /*
     * load the latest thumbnail to the view3D button
     */
    loadCurrThumbnail();

    /*
     * initializes AimfireService, and bind to it
     */
    mAimfireServiceConn = new AimfireServiceConn(this);

    /*
     * binding doesn't happen until later. wait for it to happen in another 
     * thread and connect to p2p peer if necessary
     */
    (new Thread(mAimfireServiceInitTask)).start();

    if (ADD_LOGO) {
        /*
         * init our logo that will be embedded in processed photos
         */
        AssetManager assetManager = getAssets();
        p.getInstance().a(assetManager, MainConsts.MEDIA_3D_RAW_PATH, "logo.png");
    }

    /*
     * register for AimfireService message broadcast
     */
    LocalBroadcastManager.getInstance(this).registerReceiver(mAimfireServiceMsgReceiver,
            new IntentFilter(MainConsts.AIMFIRE_SERVICE_MESSAGE));

    /*
     * register for intents sent by the media processor service
     */
    LocalBroadcastManager.getInstance(this).registerReceiver(mPhotoProcessorMsgReceiver,
            new IntentFilter(MainConsts.PHOTO_PROCESSOR_MESSAGE));
}

From source file:com.example.android.snake.SnakeView.java

/**
 * Figure out which way the snake is going, see if he's run into anything (the
 * walls, himself, or an apple). If he's not going to die, we then add to the
 * front and subtract from the rear in order to simulate motion. If we want to
 * grow him, we don't subtract from the rear.
 *
 *//*from  w  ww  .ja  v  a2  s . c  o  m*/
//private int frameCount = 0;
private void updateSnake() {
    /*
    frameCount++;
    if (frameCount%5 == 0)
    displayNewPortion();
    */
    boolean growSnake = false;
    // grab the snake by the head
    Coordinate head = mSnakeTrail.get(0);
    Coordinate newHead = new Coordinate(1, 1);

    mDirection = mNextDirection;

    switch (mDirection) {
    case EAST: {
        newHead = new Coordinate(head.x + 1, head.y);
        break;
    }
    case WEST: {
        newHead = new Coordinate(head.x - 1, head.y);
        break;
    }
    case NORTH: {
        newHead = new Coordinate(head.x, head.y - 1);
        break;
    }
    case SOUTH: {
        newHead = new Coordinate(head.x, head.y + 1);
        break;
    }
    }

    // Collision detection
    // For now we have a 1-square wall around the entire arena
    if ((newHead.x < 1) || (newHead.y < 1) || (newHead.x > mXTileCount - 2) || (newHead.y > mYTileCount - 2)) {
        setMode(LOSE);
        return;

    }

    // Look for collisions with itself
    int snakelength = mSnakeTrail.size();
    for (int snakeindex = 0; snakeindex < snakelength; snakeindex++) {
        Coordinate c = mSnakeTrail.get(snakeindex);
        if (c.equals(newHead)) {
            setMode(LOSE);
            return;
        }
    }

    // Look for apples
    int applecount = mAppleList.size();
    for (int appleindex = 0; appleindex < applecount; appleindex++) {
        Coordinate c = mAppleList.get(appleindex);
        if (c.equals(newHead)) {
            mAppleList.remove(c);
            addRandomApple();
            mScore++;
            mMoveDelay *= 0.9;
            //displayNewPortion();
            if (mScore == 1) {
                mSnakeEatPlayer = MediaPlayer.create(getContext(), R.raw.snakeeat);
                mSnakeEatPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        if (mp == mSnakeEatPlayer) {
                            mSnakeEatPlayer.start();
                        }
                    }
                });
            } else {
                mSnakeEatPlayer.start();
            }
            growSnake = true;
        }
    }

    // push a new head onto the ArrayList and pull off the tail
    mSnakeTrail.add(0, newHead);
    // except if we want the snake to grow
    if (!growSnake) {
        mSnakeTrail.remove(mSnakeTrail.size() - 1);
    }

    int index = 0;
    for (Coordinate c : mSnakeTrail) {
        if (index == 0) {
            setTile(YELLOW_STAR, c.x, c.y);
        } else {
            setTile(RED_STAR, c.x, c.y);
        }
        index++;
    }
}