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:de.rosche.spectraTelemetry.SpectraTelemetry.java

private void setDefaultPrefs(Map<String, String> DbPrefs) {
    String valueTmp = "";
    mVoltageAlarm = getDouble(mVoltageAlarm, DbPrefs.get("mVoltageAlarm"));
    mUsedCap = getDouble(mUsedCap, DbPrefs.get("mUsedCap"));
    mCurrentAlarm = getDouble(mCurrentAlarm, DbPrefs.get("mCurrentAlarm"));
    mPowerAlarm = getDouble(mPowerAlarm, DbPrefs.get("mPowerAlarm"));

    mSpeedAlarm = getInteger(mSpeedAlarm, DbPrefs.get("mSpeedAlarm"));
    mRpmAlarm = getInteger(mRpmAlarm, DbPrefs.get("mRpmAlarm"));
    mAltitudeAlarm = getInteger(mAltitudeAlarm, DbPrefs.get("mAltitudeAlarm"));
    mFuelAlarm = getInteger(mFuelAlarm, DbPrefs.get("mFuelAlarm"));
    voltage_hysteresis = getInteger(voltage_hysteresis, DbPrefs.get("voltage_hysteresis"));
    current_hysteresis = getInteger(current_hysteresis, DbPrefs.get("current_hysteresis"));
    rpm_hysteresis = getInteger(rpm_hysteresis, DbPrefs.get("rpm_hysteresis"));
    temperature_hysteresis = getInteger(temperature_hysteresis, DbPrefs.get("temperature_hysteresis"));
    power_hysteresis = getInteger(power_hysteresis, DbPrefs.get(""));
    mTemperatureAlarm = getInteger(mTemperatureAlarm, DbPrefs.get("power_hysteresis"));
    speech_volume = getInteger(speech_volume, DbPrefs.get("speech_volume"));
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int sb2value = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    sb2value = sb2value * speech_volume / 100;
    am.setStreamVolume(AudioManager.STREAM_MUSIC, sb2value, 0);
    vibrate_on = getBoolean(vibrate_on, DbPrefs.get("vibrate_on"));
    voltage_speech_ack = getBoolean(voltage_speech_ack, DbPrefs.get("voltage_speech_ack"));
    current_speech_ack = getBoolean(current_speech_ack, DbPrefs.get("current_speech_ack"));
    height_speech_ack = getBoolean(height_speech_ack, DbPrefs.get("height_speech_ack"));
    used_speech_ack = getBoolean(used_speech_ack, DbPrefs.get("used_speech_ack"));
    speed_speech_ack = getBoolean(speed_speech_ack, DbPrefs.get("speed_speech_ack"));
    rpm_speech_ack = getBoolean(rpm_speech_ack, DbPrefs.get("rpm_speech_ack"));
    temp_speech_ack = getBoolean(temp_speech_ack, DbPrefs.get("temp_speech_ack"));

    voltage_speech_label = DbPrefs.get("voltage_speech_label");
    voltage_speech_unit = DbPrefs.get("voltage_speech_unit");

    height_speech_label = DbPrefs.get("height_speech_label");
    height_speech_unit = DbPrefs.get("height_speech_unit");

    current_speech_label = DbPrefs.get("current_speech_label");
    current_speech_unit = DbPrefs.get("current_speech_unit");

    used_speech_label = DbPrefs.get("used_speech_label");
    used_speech_unit = DbPrefs.get("used_speech_unit");

    speed_speech_label = DbPrefs.get("speed_speech_label");
    speed_speech_unit = DbPrefs.get("speed_speech_unit");

    rpm_speech_label = DbPrefs.get("rpm_speech_label");
    rpm_speech_unit = DbPrefs.get("rpm_speech_unit");

    temp_speech_label = DbPrefs.get("temp_speech_label");
    temp_speech_unit = DbPrefs.get("temp_speech_unit");

    valueTmp = DbPrefs.get("log_file");
    if (valueTmp != "") {
        log_file = valueTmp;/*from w  w w . jav a  2  s.c o  m*/
    }

    valueTmp = DbPrefs.get("bluetooth_device");
    if (valueTmp != "") {
        bluetooth_device = valueTmp;
    }
    mSound_on = getBoolean(mSound_on, DbPrefs.get("mSound_on"));
    speech_on = getBoolean(speech_on, DbPrefs.get("speech_on"));
    speech_intervall = DbPrefs.get("speech_intervall");
    mAlarm_on = getBoolean(mAlarm_on, DbPrefs.get("mAlarm_on"));
    mlog_file = getBoolean(mlog_file, DbPrefs.get("mlog_file"));
    alert_tone = DbPrefs.get("alert_tone");
    label_rpm1 = DbPrefs.get("label_rpm1");
    label_rpm2 = DbPrefs.get("label_rpm2");
    label_temp1 = DbPrefs.get("label_temp1");
    label_temp2 = DbPrefs.get("label_temp2");
    unit_rpm1 = DbPrefs.get("unit_rpm1");
    unit_rpm2 = DbPrefs.get("unit_rpm2");
    unit_temp1 = DbPrefs.get("unit_temp1");
    unit_temp2 = DbPrefs.get("unit_temp2");
    current_sensor_type = DbPrefs.get("current_sensor_type");
    alarm_voltage = getBoolean(alarm_voltage, DbPrefs.get("alarm_voltage"));
    alarm_used = getBoolean(alarm_used, DbPrefs.get("alarm_used"));
    alarm_current = getBoolean(alarm_current, DbPrefs.get("alarm_current"));
    alarm_speed = getBoolean(alarm_speed, DbPrefs.get("alarm_speed"));
    alarm_fuel = getBoolean(alarm_fuel, DbPrefs.get("alarm_fuel"));
    alarm_temperature = getBoolean(alarm_temperature, DbPrefs.get("alarm_temperature"));
    alarm_altitude = getBoolean(alarm_altitude, DbPrefs.get("alarm_altitude"));
    alarm_rpm = getBoolean(alarm_rpm, DbPrefs.get("alarm_rpm"));
    alarm_power = getBoolean(alarm_power, DbPrefs.get("alarm_power"));

    if (rpm1Label != null)
        rpm1Label.setText(label_rpm1);
    if (rpm2Label != null)
        rpm2Label.setText(label_rpm2);
    if (tempLabel != null)
        tempLabel.setText(label_temp1);
    if (temp2Label != null)
        temp2Label.setText(label_temp2);

    if (rpm1_unit != null)
        rpm1_unit.setText(unit_rpm1);
    if (rpm2_unit != null)
        rpm2_unit.setText(unit_rpm2);
    if (temp1_unit != null)
        temp1_unit.setText(unit_temp1);
    if (temp1_unit != null)
        temp1_unit.setText(unit_temp2);

    if (alert_tone.equals("")) {
        uri = null;
    } else {
        uri = Uri.parse(alert_tone);
    }

    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
        if (uri != null && !uri.toString().equals("res/raw/alarm.mp3")) {
            mp = MediaPlayer.create(getApplicationContext(), uri);
        } else {
            mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
        }
    }
}

From source file:com.sentaroh.android.SMBSync.SMBSyncMain.java

private void playBackDefaultNotification() {
    //      Thread th=new Thread(){
    //         @Override
    //         public void run() {
    //            Uri uri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    //            if (uri!=null) {
    ////               Ringtone rt=RingtoneManager.getRingtone(mContext, uri);
    ////               rt.play();
    ////               SystemClock.sleep(1000);
    ////               rt.stop();
    //               MediaPlayer player = MediaPlayer.create(mContext, uri);
    //               if (player!=null) {
    //                  int dur=player.getDuration();
    //                  player.start();
    //                  SystemClock.sleep(dur+10);
    //                  player.stop();
    //                  player.reset();
    //                  player.release();
    //               }
    //            }
    //         }// w  w w.j a va2  s.  c o  m
    //      };
    //      th.start();
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (uri != null) {
        MediaPlayer player = MediaPlayer.create(mContext, uri);
        if (player != null) {
            int dur = player.getDuration();
            player.start();
            SystemClock.sleep(dur + 10);
            player.stop();
            player.reset();
            player.release();
        }
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc//from   www  .  ja v a  2s  .c  o  m
 */
@Override
public Media createMedia(final String uri, boolean isVideo, final Runnable onCompletion) throws IOException {
    if (getActivity() == null) {
        return null;
    }
    if (!uri.startsWith(FileSystemStorage.getInstance().getAppHomePath())) {
        if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")) {
            return null;
        }
    }
    if (uri.startsWith("file://")) {
        return createMedia(removeFilePrefix(uri), isVideo, onCompletion);
    }
    File file = null;
    if (uri.indexOf(':') < 0) {
        // use a file object to play to try and workaround this issue:
        // http://code.google.com/p/android/issues/detail?id=4124
        file = new File(uri);
    }

    Media retVal;

    if (isVideo) {
        final AndroidImplementation.Video[] video = new AndroidImplementation.Video[1];
        final boolean[] flag = new boolean[1];
        final File f = file;
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                VideoView v = new VideoView(getActivity());
                v.setZOrderMediaOverlay(true);
                if (f != null) {
                    v.setVideoURI(Uri.fromFile(f));
                } else {
                    v.setVideoURI(Uri.parse(uri));
                }
                video[0] = new AndroidImplementation.Video(v, getActivity(), onCompletion);
                flag[0] = true;
                synchronized (flag) {
                    flag.notify();
                }
            }
        });
        while (!flag[0]) {
            synchronized (flag) {
                try {
                    flag.wait(100);
                } catch (InterruptedException ex) {
                }
            }
        }
        return video[0];
    } else {
        MediaPlayer player;
        if (file != null) {
            FileInputStream is = new FileInputStream(file);
            player = new MediaPlayer();
            player.setDataSource(is.getFD());
            player.prepare();
        } else {
            player = MediaPlayer.create(getActivity(), Uri.parse(uri));
        }
        retVal = new Audio(getActivity(), player, null, onCompletion);
    }
    return retVal;
}

From source file:org.mdc.chess.MDChess.java

@Override
public void computerMoveMade() {
    if (soundEnabled) {
        if (moveSound != null) {
            moveSound.release();//from   w w w. ja v a2s .com
        }
        try {
            moveSound = MediaPlayer.create(this, R.raw.movesound);
            if (moveSound != null) {
                moveSound.start();
            }
        } catch (NotFoundException ex) {
            Log.d("Exception", ex.toString());
        }
    }
    if (vibrateEnabled) {
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(500);
    }
}

From source file:edu.mit.viral.shen.DroidFish.java

@Override
public void computerMoveMade() {
    if (soundEnabled) {
        if (moveSound != null)
            moveSound.release();//from   ww  w. ja  v  a2  s .c o m
        moveSound = MediaPlayer.create(this, R.raw.movesound);
        if (moveSound != null)
            moveSound.start();
    }
    if (vibrateEnabled) {
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(500);
    }
}

From source file:com.if3games.chessonline.DroidFish.java

@Override
public void computerMoveMade() {
    if (soundEnabled) {
        if (moveSound != null)
            moveSound.release();/*from w  w  w  . j  av  a 2 s .c  o m*/
        try {
            moveSound = MediaPlayer.create(this, R.raw.movesound);
            if (moveSound != null)
                moveSound.start();
        } catch (NotFoundException ex) {
        }
    }
    if (vibrateEnabled) {
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(500);
    }
}