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.massivcode.androidmusicplayer.services.MusicService.java

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

    Log.d(TAG, "MusicService.onCreate()");
    Log.d(TAG, " : " + DataBackupUtil.getInstance(getApplicationContext()).loadIsShuffle() + "  : "
            + DataBackupUtil.getInstance(getApplicationContext()).loadIsRepeat());

    // EventBus ?? ?  ?  
    EventBus.getDefault().register(this);

    mMediaPlayer = new MediaPlayer();

    mMediaPlayer.setOnCompletionListener(this);

    mUnPlugReceiver = new UnPlugReceiver();
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(mUnPlugReceiver, filter);

    if (Build.VERSION.SDK_INT < 23) {
        if (mAllMusicData == null) {
            new Thread(new Runnable() {
                @Override/*  w  ww  .  j a  v  a2s. c  om*/
                public void run() {
                    Log.d(TAG, "InitEvent @ service");
                    mAllMusicData = MusicInfoLoadUtil.getAllMusicInfo(getApplicationContext());
                }
            }).start();
        }
    }

    int loadedPosition = DataBackupUtil.getInstance(getApplicationContext()).loadCurrentPlayingMusicPosition();
    if (loadedPosition != -1) {
        mCurrentPosition = loadedPosition;
        Log.d(TAG, " ?  : " + loadedPosition);
    }

    ArrayList<Long> loadedPlaylist = DataBackupUtil.getInstance(getApplicationContext()).loadLastPlayedSongs();
    if (loadedPlaylist != null && loadedPlaylist.size() != 0) {
        mCurrentPlaylist = loadedPlaylist;
        mCurrentMusicInfo = MusicInfoLoadUtil.getSelectedMusicInfo(getApplicationContext(),
                mCurrentPlaylist.get(mCurrentPosition));
        try {
            mMediaPlayer.setDataSource(getApplicationContext(),
                    switchIdToUri(mCurrentPlaylist.get(mCurrentPosition)));
            mMediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        isReady = true;
        // TODO ?  
        sendAllEvent();
        EventBus.getDefault().post(new RequestEvent());
    }

}

From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java

private MediaPlayer initAudibleMediaPlayer(String audiblePath, int type) throws IOException {
    MediaPlayer mp = mMediaPlayers.get(type);
    if (mp == null) {
        mp = new MediaPlayer();
        mMediaPlayers.put(type, mp);/*from  w  w  w  .  j a va 2 s .  c o  m*/
    } else {
        mp.reset();
    }
    mp.setDataSource(audiblePath);
    mp.prepare();
    mp.setOnCompletionListener(mPlayCompletionListener);
    return mp;
}

From source file:io.github.carlorodriguez.morningritual.MainActivity.java

private void playLegacyNotificationSound() throws IOException {
    AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.sound);

    if (afd == null)
        throw new IOException();

    if (mMediaPlayer != null) {
        mMediaPlayer.release();/*from ww  w  .jav  a  2  s .c  o m*/
    }

    mMediaPlayer = new MediaPlayer();

    mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

    afd.close();

    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

    mMediaPlayer.prepare();

    mMediaPlayer.start();
}

From source file:za.co.neilson.alarm.alert.AlarmAlertActivity.java

private void startAlarm() {

    if (alarm.getAlarmTonePath() != "") {
        mediaPlayer = new MediaPlayer();
        if (alarm.getVibrate()) {
            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            long[] pattern = { 1000, 200, 200, 200 };
            vibrator.vibrate(pattern, 0);
        }//from   w  w w.j  a v  a 2  s. c om
        try {
            mediaPlayer.setVolume(1.0f, 1.0f);
            mediaPlayer.setDataSource(this, Uri.parse(alarm.getAlarmTonePath()));
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.setLooping(true);
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (Exception e) {
            mediaPlayer.release();
            alarmActive = false;
        }
    }

}

From source file:com.yohpapa.research.simplemusicplayer.PlaybackService.java

private MediaPlayer initializePlayer() {
    MediaPlayer player = new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);
    player.setOnSeekCompleteListener(this);
    player.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);

    IntentFilter filter = new IntentFilter();
    filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(audioOutputChangedEventReceiver, filter);

    return player;
}

From source file:com.mattprecious.prioritysms.receiver.AlarmReceiver.java

private void doNotify(Context context, BaseProfile profile) {
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    MediaPlayer mediaPlayer = new MediaPlayer();

    try {// w  ww  .j  a  va2 s  .co m
        if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL || profile.isOverrideSilent()) {
            mediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
            mediaPlayer.setAudioStreamType(
                    profile.isOverrideSilent() ? AudioManager.STREAM_ALARM : AudioManager.STREAM_NOTIFICATION);
            mediaPlayer.setDataSource(context, profile.getRingtone());
            mediaPlayer.prepare();
            mediaPlayer.start();

            if (profile.isVibrate()) {
                Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(VIBRATE_PATTERN, -1);
            }
        }
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "failed to play audio", e);
    } catch (IOException e) {
        Log.e(TAG, "failed to play audio", e);
    }
}

From source file:hku.fyp14017.blencode.ui.fragment.BackPackSoundFragment.java

@Override
public void onStart() {
    super.onStart();
    mediaPlayer = new MediaPlayer();
    initClickListener();
}

From source file:com.commonslab.commonslab.Fragments.UploadToCommonsFragment.java

public void playAudio(String audioFilePath) throws IOException {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(audioFilePath);
    mediaPlayer.prepare();// ww w  .jav  a 2 s. com
    mediaPlayer.start();
}

From source file:com.geryon.ocraa.MusicService.java

/**
 * Makes sure the media player exists and has been reset. This will create the media player
 * if needed, or reset the existing media player if one already exists.
 *//* w ww.  j ava 2 s.c  o m*/
void createMediaPlayerIfNeeded() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();

        // Make sure the media player w ill acquire a wake-lock while playing. If we don't do
        // that, the CPU might go to sleep while the song is playing, causing playback to stop.
        //
        // Remember that to use this, we have to declare the android.permission.WAKE_LOCK
        // permission in AndroidManifest.xml.
        mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

        // we want the media player to notify us when it's ready preparing, and when it's done
        // playing:
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnCompletionListener(this);
        mPlayer.setOnErrorListener(this);
    } else
        mPlayer.reset();
}

From source file:com.eggwall.SoundSleep.AudioService.java

/**
 * Create a media player with the standard configuration both for white noise and music.
 * @return a generic Media player suitable for this application.
 *//*from w  w w.j ava 2  s.  c om*/
private MediaPlayer getGenericMediaPlayer() {
    final MediaPlayer player = new MediaPlayer();
    // Keep the CPU awake while playing music.
    player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
    player.setOnPreparedListener(this);
    return player;
}