Example usage for android.media MediaPlayer setDataSource

List of usage examples for android.media MediaPlayer setDataSource

Introduction

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

Prototype

public void setDataSource(MediaDataSource dataSource) throws IllegalArgumentException, IllegalStateException 

Source Link

Document

Sets the data source (MediaDataSource) to use.

Usage

From source file:Main.java

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

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());
    mp.prepare();//  w  w w .  jav  a 2  s .  c om
    fis.close();
    mp.start();
}

From source file:Main.java

public static boolean is3gpFileAudio(String url) {
    int height = 0;
    File mediaFile = new File(url);
    try {//w w w .  j av a  2 s.c o m
        MediaPlayer mp = new MediaPlayer();
        FileInputStream fs;
        FileDescriptor fd;
        fs = new FileInputStream(mediaFile);
        fd = fs.getFD();
        mp.setDataSource(fd);
        mp.prepare();
        height = mp.getVideoHeight();
        mp.release();
    } catch (Exception e) {
        Log.e("KKIM", "Exception trying to determine if 3gp file is video.", e);
    }
    Log.i("KKIM", "The height of the file is " + height);
    return height == 0;
}

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

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

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

From source file:util.mediamanager.PlaylistUtils.java

public static void PlayMusic(String DataStream) {
    MediaPlayer mpObject = new MediaPlayer();
    if (DataStream == null)
        return;/*from w w w .ja v a2s . co  m*/
    try {
        mpObject.setDataSource(DataStream);
        mpObject.prepare();
        mpObject.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.visva.voicerecorder.utils.Utils.java

public static long getDurationTimeFromFile(String filePath) {
    if (StringUtility.isEmpty(filePath)) {
        AIOLog.e(MyCallRecorderConstant.TAG, "filePath is null");
        return 0;
    }//w  ww .  j a v  a 2  s . com
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        Log.d("KieuThang", "file it not exitsted!");
    }
    MediaPlayer mediaPlayer = new MediaPlayer();
    long duration = 0L;
    try {
        mediaPlayer.setDataSource(filePath);
        mediaPlayer.prepare();
        duration = mediaPlayer.getDuration();
        mediaPlayer.reset();
        mediaPlayer.release();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //the valid time we offer to save at least more than 1s
    if (duration > 1000) {
        AIOLog.d(MyCallRecorderConstant.TAG, "InValid time:" + duration);
        return duration;
    }
    AIOLog.d(MyCallRecorderConstant.TAG, "Valid time:" + duration);
    return 0;
}

From source file:de.azapps.mirakel.helper.TaskDialogHelpers.java

public static void playbackFile(final Activity context, final FileMirakel file, final boolean loud) {
    final MediaPlayer mPlayer = new MediaPlayer();
    final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (!loud) {//  ww  w. j a v  a2  s.c o m
        am.setSpeakerphoneOn(false);
        am.setMode(AudioManager.MODE_IN_CALL);
        context.setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    }
    try {
        mPlayer.reset();
        if (!loud) {
            mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        }
        mPlayer.setDataSource(file.getFileStream(context).getFD());
        mPlayer.prepare();
        mPlayer.start();
        mPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(final MediaPlayer mp) {
                audio_playback_dialog.dismiss();
            }
        });
        am.setMode(AudioManager.MODE_NORMAL);
        audio_playback_playing = true;
    } catch (final IOException e) {
        Log.e(TAG, "prepare() failed");
    }
    audio_playback_dialog = new AlertDialog.Builder(context).setTitle(R.string.audio_playback_title)
            .setPositiveButton(R.string.audio_playback_pause, null)
            .setNegativeButton(R.string.audio_playback_stop, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    mPlayer.release();
                }
            }).setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(final DialogInterface dialog) {
                    mPlayer.release();
                    dialog.cancel();
                }
            }).create();
    audio_playback_dialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(final DialogInterface dialog) {
            final Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    if (audio_playback_playing) {
                        button.setText(R.string.audio_playback_play);
                        mPlayer.pause();
                        audio_playback_playing = false;
                    } else {
                        button.setText(R.string.audio_playback_pause);
                        mPlayer.start();
                        audio_playback_playing = true;
                    }
                }
            });
        }
    });
    audio_playback_dialog.show();
}

From source file:org.cocos2dx.lib.Cocos2dxMusic.java

/**
 * create mediaplayer for music//  w  w w . j av a2 s  .c  o m
 * @param path the path relative to assets
 * @return 
 */
private MediaPlayer createMediaplayerFromAssets(String path) {
    MediaPlayer mediaPlayer = new MediaPlayer();

    try {
        if (path.startsWith("/")) {
            mediaPlayer.setDataSource(path);
        } else {
            AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path);
            mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(),
                    assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
        }

        mediaPlayer.prepare();

        mediaPlayer.setVolume(mLeftVolume, mRightVolume);
    } catch (Exception e) {
        mediaPlayer = null;
        Log.e(TAG, "error: " + e.getMessage(), e);
    }

    return mediaPlayer;
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VoiceObj.java

@Override
public void activate(final Context context, final SignedObj obj) {
    Runnable r = new Runnable() {
        @Override/*from   ww  w  .j av  a  2s.  co m*/
        public void run() {
            byte[] bytes = obj.getRaw();
            if (bytes == null) {
                Pair<JSONObject, byte[]> p = splitRaw(obj.getJson());
                //                content = p.first;
                bytes = p.second;
            }

            /*AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bytes.length, AudioTrack.MODE_STATIC);
            track.write(bytes, 0, bytes.length);
            try { // TODO: hack.
            Thread.sleep(500);
             } catch (InterruptedException e) {
             }
            track.play();
            */
            /****/

            File file = new File(getTempFilename());
            try {
                OutputStream os = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(os);
                bos.write(bytes, 0, bytes.length);
                bos.flush();
                bos.close();

                copyWaveFile(getTempFilename(), getFilename());
                deleteTempFile();

                MediaPlayer mp = new MediaPlayer();

                mp.setDataSource(getFilename());
                mp.prepare();
                mp.start();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    if (context instanceof Activity) {
        ((Activity) context).runOnUiThread(r);
    } else {
        r.run();
    }
}

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  www  .  j av a2  s.  c o m*/
    } else {
        mp.reset();
    }
    mp.setDataSource(audiblePath);
    mp.prepare();
    mp.setOnCompletionListener(mPlayCompletionListener);
    return mp;
}