MediaPlayer getDuration - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for MediaPlayer getDuration method.

This code example shows how to use the following methods:getDuration, prepare, release, setDataSource
 
	protected long getDuration(String file) {
		long duration = -2;
		if (!isPlaying & !isStreaming(file)) {
			try { 
				mPlayer = new MediaPlayer();
				mPlayer.setDataSource("/sdcard/" + file);
				mPlayer.prepare();
				duration = mPlayer.getDuration();
				mPlayer.release();
			} catch (Exception e) { e.printStackTrace(); return(-3); }
		} else 
		if (isPlaying & !isStreaming(file)) {
			duration = mPlayer.getDuration();
		} else  
		if (isPlaying & isStreaming(file)) {
			try { 
				duration = mPlayer.getDuration();
			} catch (Exception e) { e.printStackTrace(); return(-4); }
		}else { return -1; } 
7
CodeRank
This code example shows how to use the following methods:getDuration, seekTo
			loadSound(currFileName, false);
		} 
	} 
	 
	void setPosition(float pct){
		if(stream && player!=null) player.seekTo((int) (player.getDuration()*pct)); // 0 = start, 1 = end;
	} 
	 
	void setPositionMS(int ms){
		if(stream && player!=null) player.seekTo(ms); // 0 = start, 1 = end;
	} 
	 
	float getPosition(){ 
		if(stream && player!=null)
			return ((float)player.getCurrentPosition())/(float)player.getDuration();
		else 
			return 0; 
	} 
	 
	int getPositionMS(){ 
8
CodeRank
This code example shows how to use the following methods:getDuration, prepare, setDataSource
    } 
 
 
    public static int getDuration(String filePath) {
        Log.v(TAG, "getDuration - " + filePath);
        MediaPlayer mp = new MediaPlayer();
        try{ 
            mp.setDataSource(filePath);
            mp.prepare(); 
        }catch (Exception e){
            Log.v(TAG, e.toString());
        } 
        int duration = mp.getDuration();
        Log.v(TAG, "Duration " + duration);
        mp.release();
        Log.v(TAG, "release");      
        return duration;
    } 
 
    public static boolean getCurrentPosition(String filePath){
7
CodeRank
This code example shows how to use the following methods:getDuration
		this.rightVol = rightVol;
	} 
 
	public int getDuration() { 
		if (mp != null)
			return mp.getDuration();
		return -1; 
	} 
 
	public int getCurrentPosition() { 
		if (mp != null)
			return mp.getCurrentPosition();
		return -1; 
	} 
	 
	public boolean isPlaying() { 
		if (mp != null)
			return mp.isPlaying();
		return false; 
	} 
7
CodeRank
This code example shows how to use the following methods:getDuration, prepare, release, setDataSource
	 
	protected long getDuration(String file) {
		long duration = -2;
		if (!isPlaying & !isStreaming(file)) {
			try { 
				mPlayer = new MediaPlayer();
				mPlayer.setDataSource("/sdcard/" + file);
				mPlayer.prepare();
				duration = mPlayer.getDuration();
				mPlayer.release();
			} catch (Exception e) { e.printStackTrace(); return(-3); }
		} else 
		if (isPlaying & !isStreaming(file)) {
			duration = mPlayer.getDuration();
		} else  
		if (isPlaying & isStreaming(file)) {
			try { 
				duration = mPlayer.getDuration();
			} catch (Exception e) { e.printStackTrace(); return(-4); }
		}else { return -1; } 
7
CodeRank
This code example shows how to use the following methods:getDuration, prepare, release, setDataSource
	 
	protected long getDuration(String file) {
		long duration = -2;
		if (!isPlaying & !isStreaming(file)) {
			try { 
				mPlayer = new MediaPlayer();
				mPlayer.setDataSource("/sdcard/" + file);
				mPlayer.prepare();
				duration = mPlayer.getDuration();
				mPlayer.release();
			} catch (Exception e) { e.printStackTrace(); return(-3); }
		} else 
		if (isPlaying & !isStreaming(file)) {
			duration = mPlayer.getDuration();
		} else  
		if (isPlaying & isStreaming(file)) {
			try { 
				duration = mPlayer.getDuration();
			} catch (Exception e) { e.printStackTrace(); return(-4); }
		}else { return -1; } 
7
CodeRank
This code example shows how to use the following methods:getCurrentPosition, getDuration, isPlaying, pause, seekTo
			return true; 
		} 
 
		if ((keyCode == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD || keyCode == KeyEvent.KEYCODE_MEDIA_NEXT)
				&& isMediaPlayerStateValid()) { 
			int skipOffset = 10000 + mediaPlayer.getCurrentPosition();
			int duration = mediaPlayer.getDuration();
			if (skipOffset > duration) {
				skipOffset = duration - 1;
			} 
			if (!mediaController.isShowing()) {
				mediaController.show(CONTROLLER_DELAY);
			} 
			mediaPlayer.seekTo(skipOffset);
			return true; 
		} 
 
		if ((keyCode == KeyEvent.KEYCODE_MEDIA_REWIND || keyCode == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
				&& isMediaPlayerStateValid()) { 
			int skipOffset = mediaPlayer.getCurrentPosition() - 10000;

6
CodeRank
This code example shows how to use the following methods:getDuration, prepare, release, setDataSource
		bundle.putInt("progress", (int) ((p) * 100));
		message.setData(bundle);
		mHandler.sendMessage(message);
	} 
	private static long getVideoDuration(final Context context,final Uri videoUri) {
		MediaPlayer a= new MediaPlayer();
		try { 
			a.setDataSource(context, videoUri);
			a.prepare();
			int result = a.getDuration();
			a.release();
			return result;
		} catch (Exception exception) {
			a.release();
			throw new RuntimeException(exception);
		} 
	} 
 
	private static Bitmap getVideoFrame(Context context, Uri uri, long time) {
		MediaMetadataRetriever retriever = new MediaMetadataRetriever();
6
CodeRank
This code example shows how to use the following methods:getDuration
	public long getCurrentPositionInMedia() { 
		return mediaPlayer.getCurrentPosition();
	} 
 
	public long getMediaDuration() { 
		return mediaPlayer.getDuration();
	} 
 
} 
6
CodeRank
This code example shows how to use the following methods:getDuration, getVideoHeight, getVideoWidth, prepare, setDataSource
     * @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;
    } 
 
8
CodeRank

Related MediaPlayer getDuration Questions & Answers:

Other MediaPlayer getDuration Examples

314:			   public void run() {
315:				   long totalDuration = mp.getDuration();
316:				   long currentDuration = mp.getCurrentPosition();
317:	
318:				   // Displaying Total Duration time

Read More

getDuration Method Overview
Gets the duration of the file.
See Code Examples for other Android MediaPlayer Methods: