Example usage for android.media MediaPlayer start

List of usage examples for android.media MediaPlayer start

Introduction

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

Prototype

public void start() throws IllegalStateException 

Source Link

Document

Starts or resumes playback.

Usage

From source file:Main.java

public static void playSound(Context context, Uri uri) {
    final MediaPlayer player = new MediaPlayer();
    try {//from   www .j  a v a  2  s  .com
        player.setDataSource(context.getApplicationContext(), uri);
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
            }
        });
        player.prepareAsync();
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void runAlarm(Context context, int resourceId) {
    MediaPlayer mediaPlayer = MediaPlayer.create(context, resourceId);
    mediaPlayer.setVolume(1.0f, 1.0f);/*from ww  w . j a  v  a 2s . co m*/
    mediaPlayer.start();
}

From source file:Main.java

public static boolean play(Context context, String name) {
    Integer id = resources.get(name);
    if (id == null)
        return false;

    MediaPlayer mp = MediaPlayer.create(context, id);
    if (mp == null)
        return false;

    mp.start();
    return true;// w w  w  . ja  v a2s . c om
}

From source file:Main.java

public static void playRecordAction(File audiofile) throws Exception {
    MediaPlayer mp = new MediaPlayer();
    FileInputStream fis = new FileInputStream(audiofile.getAbsolutePath());
    mp.setDataSource(fis.getFD());//from  w w  w. j  a  v a2s . c om
    mp.prepare();
    fis.close();
    mp.start();
}

From source file:Main.java

public static void startPlaying(MediaPlayer mediaPlayer, String filePath) {
    try {/*ww  w. j  a v a 2s.  c om*/
        mediaPlayer.setDataSource(filePath);
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();
}

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 source file:Main.java

public static void playSoundfile(Context context, int R_raw_fileid) {
    MediaPlayer mp = MediaPlayer.create(context, R_raw_fileid);
    mp.setOnCompletionListener(new OnCompletionListener() {
        @Override/*w  ww .j a  v  a 2s.  c om*/
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
    mp.start();
}

From source file:es.deustotech.piramide.utils.tts.TextToSpeechWeb.java

private static synchronized void speech(final Context context, final String text, final String language) {
    executor.submit(new Runnable() {
        @Override/*  w  w  w.j  av  a  2  s . c  om*/
        public void run() {
            try {
                final String encodedUrl = Constants.URL + language + "&q="
                        + URLEncoder.encode(text, Encoding.UTF_8.name());
                final DefaultHttpClient client = new DefaultHttpClient();
                HttpParams params = new BasicHttpParams();
                params.setParameter("http.protocol.content-charset", "UTF-8");
                client.setParams(params);
                final FileOutputStream fos = context.openFileOutput(Constants.MP3_FILE,
                        Context.MODE_WORLD_READABLE);
                try {
                    try {
                        final HttpResponse response = client.execute(new HttpGet(encodedUrl));
                        downloadFile(response, fos);
                    } finally {
                        fos.close();
                    }
                    final String filePath = context.getFilesDir().getAbsolutePath() + "/" + Constants.MP3_FILE;
                    final MediaPlayer player = MediaPlayer.create(context.getApplicationContext(),
                            Uri.fromFile(new File(filePath)));
                    player.start();
                    Thread.sleep(player.getDuration());
                    while (player.isPlaying()) {
                        Thread.sleep(100);
                    }
                    player.stop();

                } finally {
                    context.deleteFile(Constants.MP3_FILE);
                }
            } catch (InterruptedException ie) {
                // ok
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:com.todoroo.astrid.files.FilesControlSet.java

private static void play(String file, PlaybackExceptionHandler handler) {
    MediaPlayer mediaPlayer = new MediaPlayer();

    try {/*from w w  w. j  a va 2 s . c o m*/
        mediaPlayer.setDataSource(file);
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        handler.playbackFailed();
    }
}

From source file:org.gearvrf.keyboard.util.Util.java

public static void loadAudioClipAndPlay(Context context, int idResource) {
    MediaPlayer mediaPlayer = MediaPlayer.create(context, idResource);
    mediaPlayer.start();
}