Example usage for android.media MediaPlayer MediaPlayer

List of usage examples for android.media MediaPlayer MediaPlayer

Introduction

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

Prototype

public MediaPlayer() 

Source Link

Document

Default constructor.

Usage

From source file:com.redoceanred.unity.android.RORAudioPlayer.java

private void createMediaPlayer() {
    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        if (mListener != null) {
            mMediaPlayer.setOnPreparedListener(mListener);
        }/*from ww w  . ja v a2 s.c o  m*/
    } else {
        stop();
        createMediaPlayer();
    }
}

From source file:com.melonbear.ampup.MediaSectionFragment.java

private void startPlayingMetronome() {
    mPlayerMetronome = new MediaPlayer();
    OnCompletionListener completionListener = new OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            mPlayMetronomeButton.setText("Start Playing");
            Log.i(LOG_TAG, "Finished Playing");
        }/* ww w .ja  v  a 2  s  .c  om*/
    };

    mPlayerMetronome.setOnCompletionListener(completionListener);
    try {
        String metronomeFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        metronomeFileName += "/metronome120bpm.mp3";

        mPlayerMetronome.setDataSource(metronomeFileName);
        mPlayerMetronome.prepare();
        mPlayerMetronome.start();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }
}

From source file:gstb.fd.eofficial.oa.im.widget.chatrow.EaseChatRowVoicePlayClickListener.java

public void playVoice(String filePath) {
    Log.e("sssssss", "sssssssssssssss");
    if (!(new File(filePath).exists())) {
        return;/*from  w  w w .  ja v  a2 s .  com*/
    }
    //      playMsgId = message.getMsgId();
    AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);

    mediaPlayer = new MediaPlayer();
    //      if (EaseUI.getInstance().getSettingsProvider().isSpeakerOpened()) {
    audioManager.setMode(AudioManager.MODE_NORMAL);
    audioManager.setSpeakerphoneOn(true);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
    //      } else {
    //         audioManager.setSpeakerphoneOn(false);// 
    // ?Earpiece???
    //         audioManager.setMode(AudioManager.MODE_IN_CALL);
    //         mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    //      }
    try {
        mediaPlayer.setDataSource(filePath);
        mediaPlayer.prepare();
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mediaPlayer.release();
                mediaPlayer = null;
                stopPlayVoice(); // stop animation
            }

        });
        isPlaying = true;
        currentPlayListener = this;
        mediaPlayer.start();
        showAnimation();

        // ?
        if (message.direct() == EMMessage.Direct.RECEIVE) {

            //             if (!message.isAcked() && chatType == EMMessage.ChatType.Chat) {
            //                       // ??
            ////                     EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
            //             }
            if (!message.isListened() && iv_read_status != null
                    && iv_read_status.getVisibility() == View.VISIBLE) {
                // ????
                iv_read_status.setVisibility(View.INVISIBLE);
                message.setListened(true);
                MessageDao.instance(activity).updateListen(message.getMsgId());
                //               EMClient.getInstance().chatManager().setMessageListened(message);
            }

        }

    } catch (Exception e) {
        System.out.println();
    }
}

From source file:com.xamoom.android.xamoomcontentblocks.ViewHolders.ContentBlock1ViewHolder.java

private void setupMusicPlayer(final Uri fileUrl) {
    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {// w  ww .  j a  v  a2s. c om
            mMediaPlayer.setDataSource(mFragment.getActivity(), fileUrl);
            mMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mSongProgressBar.setMax(mMediaPlayer.getDuration());
                mRemainingSongTimeTextView.setText(getTimeString(mMediaPlayer.getDuration()));

                mPlayPauseButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mMediaPlayer.isPlaying()) {
                            mMediaPlayer.pause();
                            mPlayPauseButton.setBackgroundResource(R.drawable.ic_play);
                        } else {
                            mMediaPlayer.start();
                            mPlayPauseButton.setBackgroundResource(R.drawable.ic_pause);
                            startUpdatingProgress();
                        }
                    }
                });

                mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stopUpdatingProgress();
                        if (mFragment.getActivity() != null) {
                            setupMusicPlayer(fileUrl);
                        }
                    }
                });
            }
        });
    }
}

From source file:net.xisberto.work_schedule.alarm.AlarmMessageActivity.java

private void prepareSound(Context context, Uri alert) {
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
    try {/*from   w w w.j  a  v a  2s.co  m*/
        mMediaPlayer.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
        }
    } catch (IOException e) {
        System.out.println("OOPS");
    }
}

From source file:uk.ac.hutton.ics.buntata.activity.VideoActivity.java

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

    if (player == null) {
        try {//  w  w  w  .  j  a v  a 2 s. co m
            player = new MediaPlayer();
            player.setDataSource(this, uri);

            mediaController = new MediaController(this);

            sv.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    player.setDisplay(holder);
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                    player.setDisplay(holder);
                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                }
            });

            player.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                    // // Get the dimensions of the video
                    int videoWidth = player.getVideoWidth();
                    int videoHeight = player.getVideoHeight();
                    float videoProportion = (float) videoWidth / (float) videoHeight;

                    // Get the width of the screen
                    Point point = new Point();
                    getWindowManager().getDefaultDisplay().getSize(point);
                    int screenWidth = point.x;
                    int screenHeight = point.y;
                    float screenProportion = (float) screenWidth / (float) screenHeight;

                    // Get the SurfaceView layout parameters
                    ViewGroup.LayoutParams lp = sv.getLayoutParams();
                    if (videoProportion > screenProportion) {
                        lp.width = screenWidth;
                        lp.height = (int) ((float) screenWidth / videoProportion);
                    } else {
                        lp.width = (int) (videoProportion * (float) screenHeight);
                        lp.height = screenHeight;
                    }
                    // Commit the layout parameters
                    sv.setLayoutParams(lp);

                    player.start();
                }
            });
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaController.setMediaPlayer(VideoActivity.this);
                    mediaController.setAnchorView(sv);
                }
            });
            player.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.android.settings.service.AlarmService.java

public void startAlarmSound() throws java.io.IOException, IllegalArgumentException, IllegalStateException {

    Uri alertSound = SmsCallHelper.returnUserRingtone(this);

    if (mPlaying) {
        stopAlarm();//from   ww w.  j  a  v a  2  s.  c o  m
    } else {
        mAlarmVolumeSetting = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
    }

    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mp.stop();
                mp.release();
                mMediaPlayer = null;
                return true;
            }
        });
    }

    mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);

    mMediaPlayer.setDataSource(this, alertSound);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

    if (SmsCallHelper.returnUserRingtoneLoop(this)) {
        mMediaPlayer.setLooping(true);
    } else {
        mMediaPlayer.setLooping(false);
        mMediaPlayer.setOnSeekCompleteListener(stopSelf);
    }
    mMediaPlayer.prepare();
    mMediaPlayer.start();
    mPlaying = true;
}

From source file:com.ecoplayer.beta.MusicService.java

private boolean initMediaPlayer() {
    startForeground(NOTIFICATION_ID, notiBuilder.getNotification());
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    int result = audioManager.requestAudioFocus(audioFocusChangeList, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mMediaPlayer = new MediaPlayer(); // initialize it here
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnCompletionListener(onCompletion);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        // prevent CPU from going to sleep
        mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        return true;
    } else {/*w ww  .  j  a va2s .com*/
        Log.w(LOG_TAG, "The service hasn't got the audio focus");
    }
    return false;
}

From source file:com.android.settings.beanstalk.service.AlarmService.java

public void startAlarmSound() throws java.io.IOException, IllegalArgumentException, IllegalStateException {

    Uri alertSound = QuietHoursController.getInstance(this).returnUserRingtone();

    if (mPlaying) {
        stopAlarm();/*from   w w w .j av a  2 s . c  o m*/
    } else {
        mAlarmVolumeSetting = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
    }

    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mp.stop();
                mp.release();
                mMediaPlayer = null;
                return true;
            }
        });
    }

    mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);

    mMediaPlayer.setDataSource(this, alertSound);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

    if (QuietHoursController.getInstance(this).returnUserRingtoneLoop()) {
        mMediaPlayer.setLooping(true);
    } else {
        mMediaPlayer.setLooping(false);
        mMediaPlayer.setOnSeekCompleteListener(stopSelf);
    }
    mMediaPlayer.prepare();
    mMediaPlayer.start();
    mPlaying = true;
}

From source file:com.smedic.tubtub.BackgroundAudioService.java

@Override
public void onCreate() {
    super.onCreate();
    videoItem = new YouTubeVideo();

    network = new NetworkConf(getApplicationContext());

    mWifiLock = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE))
            .createWifiLock(WifiManager.WIFI_MODE_FULL, "tub_wifi_lock");

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnErrorListener(this);

    initMediaSessions();//from  ww  w. j a  va 2  s.c o  m
    initPhoneCallListener();
}