List of usage examples for android.media MediaPlayer setDataSource
public void setDataSource(MediaDataSource dataSource) throws IllegalArgumentException, IllegalStateException
From source file:com.phonegap.Capture.java
/** * Get the Image specific attributes/*from w ww .j a v a 2 s.c o m*/ * * @param filePath path to the file * @param obj represents the Media File Data * @param video if true get video attributes as well * @return a JSONObject that represents the Media File Data * @throws JSONException */ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean video) throws JSONException { MediaPlayer player = new MediaPlayer(); try { player.setDataSource(filePath); player.prepare(); obj.put("duration", player.getDuration()); if (video) { obj.put("height", player.getVideoHeight()); obj.put("width", player.getVideoWidth()); } } catch (IOException e) { Log.d(LOG_TAG, "Error: loading video file"); } return obj; }
From source file:com.msohm.blackberry.samples.bdvideoplayback.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Initialize BlackBerry Dynamics. GDAndroid.getInstance().activityInit(this); setContentView(R.layout.activity_main); copyButton = (Button) findViewById(R.id.copyButton); //The copy button is used to copy an existing video file you have into the secure //BlackBerry Dynamics file system. This only needs to be run once. copyButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //Ensure we have permission to read the file being copied. int permissionCheck = ContextCompat.checkSelfPermission(v.getContext(), Manifest.permission.READ_EXTERNAL_STORAGE); if (permissionCheck != PackageManager.PERMISSION_GRANTED) { //Permission isn't set. Request it. ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); } else { copyFile();//from ww w .j a v a 2 s.c om } } }); playButton = (Button) findViewById(R.id.playButton); //Initializes the MediaPlayer. playButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { if (!isPaused) { BDMediaDataSource source = new BDMediaDataSource(FILENAME); mp.setDataSource(source); mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); mp.prepareAsync(); } else { mp.start(); } isPaused = false; } catch (IOException ioex) { } } }); SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); SurfaceHolder holder = surfaceView.getHolder(); holder.addCallback(this); mp = new MediaPlayer(); isPaused = false; }
From source file:org.apache.cordova.Capture.java
/** * Get the Image specific attributes/* w ww . jav a 2 s.c om*/ * * @param filePath path to the file * @param obj represents the Media File Data * @param video if true get video attributes as well * @return a JSONObject that represents the Media File Data * @throws JSONException */ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean video) throws JSONException { MediaPlayer player = new MediaPlayer(); try { player.setDataSource(filePath); player.prepare(); obj.put("duration", player.getDuration() / 1000); if (video) { obj.put("height", player.getVideoHeight()); obj.put("width", player.getVideoWidth()); } } catch (IOException e) { Log.d(LOG_TAG, "Error: loading video file"); } return obj; }
From source file:com.polyvi.xface.extension.capture.MediaType.java
/** * audio video ./*from www.j a v a 2 s .c om*/ * * @param fileAbsPath ? * @param obj ? * @param isVideo ? video * @return ? JSONObject * @throws JSONException */ private JSONObject getAudioVideoData(String fileAbsPath, JSONObject obj, boolean isVideo) throws JSONException { MediaPlayer player = new MediaPlayer(); try { player.setDataSource(fileAbsPath); player.prepare(); obj.put(PROP_DURATION, player.getDuration() / XConstant.MILLISECONDS_PER_SECOND); if (isVideo) { obj.put(PROP_HEIGHT, player.getVideoHeight()); obj.put(PROP_WIDTH, player.getVideoWidth()); } } catch (IOException e) { XLog.e(CLASS_NAME, "Error: loading video file"); } player.release(); return obj; }
From source file:org.kontalk.ui.AudioDialog.java
void playAudio() { mProgressBar.setClickable(true);//from ww w. ja v a2 s .c om try { MediaPlayer player = mData.getPlayer(); player.setAudioStreamType(AudioManager.STREAM_MUSIC); player.setDataSource(mFile.getAbsolutePath()); player.prepare(); } catch (IOException e) { Log.e(TAG, "error reading from external storage", e); new MaterialDialog.Builder(getContext()).content(R.string.err_playing_sdcard) .positiveText(android.R.string.ok).show(); } catch (Exception e) { Log.e(TAG, "error playing audio", e); } setupForPlaying(); animate(mProgressBar, null, 0, MAX_PROGRESS, mData.getPlayer().getDuration()); resumeAudio(); }
From source file:com.karura.framework.plugins.Capture.java
/** * Get the Image specific attributes//w w w . j a v a 2 s . c om * * @param filePath * path to the file * @param obj * represents the Media File Data * @param video * if true get video attributes as well * @return a JSONObject that represents the Media File Data * @throws JSONException */ private JSONObject getAudioVideoMetadata(String filePath, JSONObject obj, boolean video) throws JSONException { MediaPlayer player = new MediaPlayer(); try { player.setDataSource(filePath); player.prepare(); obj.put(DURATION_FIELD, player.getDuration()); if (video) { obj.put(HEIGHT_FIELD, player.getVideoHeight()); obj.put(WIDTH_FIELD, player.getVideoWidth()); } } catch (IOException e) { Log.d(LOG_TAG, "Error: loading video file"); } return obj; }
From source file:com.novemser.voicetest.ui.MainActivity.java
/** * mp3//from w ww. j a v a2 s. co m * * @return */ public MediaPlayer createNetMp3(String url) { MediaPlayer mp = new MediaPlayer(); try { mp.setDataSource(url); } catch (IllegalArgumentException e) { return null; } catch (IllegalStateException e) { return null; } catch (IOException e) { return null; } return mp; }
From source file:hku.fyp14017.blencode.ui.controller.SoundController.java
private void handleSoundInfo(SoundViewHolder holder, SoundInfo soundInfo, SoundBaseAdapter soundAdapter, int position, Context context) { try {/* ww w. java 2s . c o m*/ MediaPlayer tempPlayer = new MediaPlayer(); tempPlayer.setDataSource(soundInfo.getAbsolutePath()); tempPlayer.prepare(); long milliseconds = tempPlayer.getDuration(); long seconds = milliseconds / 1000; if (seconds == 0) { seconds = 1; } String timeDisplayed = DateUtils.formatElapsedTime(seconds); holder.timePlayedChronometer.setText(timeDisplayed); holder.timePlayedChronometer.setVisibility(Chronometer.VISIBLE); if (soundAdapter.getCurrentPlayingPosition() == Constants.NO_POSITION) { SoundBaseAdapter.setElapsedMilliSeconds(0); } else { SoundBaseAdapter.setElapsedMilliSeconds( SystemClock.elapsedRealtime() - SoundBaseAdapter.getCurrentPlayingBase()); } if (soundInfo.isPlaying) { holder.playAndStopButton.setImageResource(hku.fyp14017.blencode.R.drawable.ic_media_stop); holder.playAndStopButton .setContentDescription(context.getString(hku.fyp14017.blencode.R.string.sound_stop)); if (soundAdapter.getCurrentPlayingPosition() == Constants.NO_POSITION) { startPlayingSound(holder.timePlayedChronometer, position, soundAdapter); } else if ((position == soundAdapter.getCurrentPlayingPosition()) && (SoundBaseAdapter.getElapsedMilliSeconds() > (milliseconds - 1000))) { stopPlayingSound(soundInfo, holder.timePlayedChronometer, soundAdapter); } else { continuePlayingSound(holder.timePlayedChronometer, SystemClock.elapsedRealtime()); } } else { holder.playAndStopButton.setImageResource(hku.fyp14017.blencode.R.drawable.ic_media_play); holder.playAndStopButton .setContentDescription(context.getString(hku.fyp14017.blencode.R.string.sound_play)); stopPlayingSound(soundInfo, holder.timePlayedChronometer, soundAdapter); } tempPlayer.reset(); tempPlayer.release(); } catch (IOException ioException) { Log.e(TAG, "Cannot get view.", ioException); } }
From source file:org.FrancoisDescamps.CatPlayer.MusicService.java
public void playMusic(final String pathToMusic) { //register receiver IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); HeadphonesUnplugReceiver receiver = new HeadphonesUnplugReceiver(); registerReceiver(receiver, receiverFilter); if (mp != null) { mp.stop();/* www .j av a 2 s. c o m*/ mp = null; } try { mp = new MediaPlayer(); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDataSource(pathToMusic); mp.prepare(); mp.start(); } catch (Exception e) { /*NOP*/ } mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { switch (repeat) { case 0: // Lecture en boucle dsactive /* NOP */ break; case 1: // Lecture en boucle d'une seule musique playMusic(pathToMusic); break; case 2: // Lecture en boucle de toutes les musiques if (positionOfInitialMusic + cpt + 1 == queu.size()) { cpt = 0; positionOfInitialMusic = 0; } else { cpt++; } playMusic(queu.get(positionOfInitialMusic + cpt).getPath()); break; } currentTitle = properties[0][positionOfInitialMusic + cpt]; currentAlbum = properties[1][positionOfInitialMusic + cpt]; currentArtist = properties[2][positionOfInitialMusic + cpt]; currentPath = properties[3][positionOfInitialMusic + cpt]; musicAsChanged = true; MainActivity.notifyOtherMusicStarted(); buildNotification(true); //save "cpt" editor = preferences.edit(); editor.putInt("cpt", cpt); if (Build.VERSION.SDK_INT < 9) { editor.commit(); } else { editor.apply(); } } }); }
From source file:org.catrobat.catroid.ui.controller.SoundController.java
private void handleSoundInfo(SoundViewHolder holder, SoundInfo soundInfo, SoundBaseAdapter soundAdapter, int position, Context context) { try {/*ww w .j a v a 2 s . co m*/ MediaPlayer tempPlayer = new MediaPlayer(); tempPlayer.setDataSource(soundInfo.getAbsolutePath()); tempPlayer.prepare(); long milliseconds = tempPlayer.getDuration(); long seconds = milliseconds / 1000; if (seconds == 0) { seconds = 1; } String timeDisplayed = DateUtils.formatElapsedTime(seconds); holder.timePlayedChronometer.setText(timeDisplayed); holder.timePlayedChronometer.setVisibility(Chronometer.VISIBLE); if (soundAdapter.getCurrentPlayingPosition() == Constants.NO_POSITION) { SoundBaseAdapter.setElapsedMilliSeconds(0); } else { SoundBaseAdapter.setElapsedMilliSeconds( SystemClock.elapsedRealtime() - SoundBaseAdapter.getCurrentPlayingBase()); } if (soundInfo.isPlaying) { holder.playAndStopButton.setImageResource(R.drawable.ic_media_stop); holder.playAndStopButton.setContentDescription(context.getString(R.string.sound_stop)); if (soundAdapter.getCurrentPlayingPosition() == Constants.NO_POSITION) { startPlayingSound(holder.timePlayedChronometer, position, soundAdapter); } else if ((position == soundAdapter.getCurrentPlayingPosition()) && (SoundBaseAdapter.getElapsedMilliSeconds() > (milliseconds - 1000))) { stopPlayingSound(soundInfo, holder.timePlayedChronometer, soundAdapter); } else { continuePlayingSound(holder.timePlayedChronometer, SystemClock.elapsedRealtime()); } } else { holder.playAndStopButton.setImageResource(R.drawable.ic_media_play); holder.playAndStopButton.setContentDescription(context.getString(R.string.sound_play)); stopPlayingSound(soundInfo, holder.timePlayedChronometer, soundAdapter); } tempPlayer.reset(); tempPlayer.release(); } catch (IOException ioException) { Log.e(TAG, "Cannot get view.", ioException); } }