Best code examples for MediaPlayer getDuration method (android.media.MediaPlayer.getDuration)

These code examples were ranked by Codota’s semantic indexing as the best open source examples for MediaPlayer getDuration method.
You can now enable Codota on your own code to easily search and navigate your Java codebase.

MediaPlayer getDuration examples from Open Source projects
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; } 
Full Snippet Info
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(){ 
Full Snippet Info
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){
Full Snippet Info
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; 
	} 
Full Snippet Info
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; } 
Full Snippet Info
7
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: