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
135:	mPlayer = new MediaPlayer(); 
136:	mPlayer.setDataSource("/sdcard/" + file); 
137:	mPlayer.prepare(); 
138:	duration = mPlayer.getDuration(); 
139:	mPlayer.release(); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getDuration, seekTo
152:	if(stream && player!=null) player.seekTo((int) (player.getDuration()*pct)); // 0 = start, 1 = end; 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods: getDuration, prepare, setDataSource
90:	MediaPlayer mp = new MediaPlayer(); 
-
92:	mp.setDataSource(filePath); 
93:	mp.prepare(); 
-
97:	int duration = mp.getDuration(); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getDuration
68:	return mp.getDuration(); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setDataSource
128:	mPlayer = new MediaPlayer(); 
129:	mPlayer.setDataSource("/sdcard/" + file); 
130:	mPlayer.prepare(); 
131:	duration = mPlayer.getDuration(); 
132:	mPlayer.release(); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setDataSource
135:	mPlayer = new MediaPlayer(); 
136:	mPlayer.setDataSource("/sdcard/" + file); 
137:	mPlayer.prepare(); 
138:	duration = mPlayer.getDuration(); 
139:	mPlayer.release(); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getCurrentPosition, getDuration, isPlaying, pause, seekTo
280:	int skipOffset = 10000 + mediaPlayer.getCurrentPosition(); 
281:	int duration = mediaPlayer.getDuration(); 
-
288:	mediaPlayer.seekTo(skipOffset); 
-
294:	int skipOffset = mediaPlayer.getCurrentPosition() - 10000; 
-
301:	mediaPlayer.seekTo(skipOffset); 
-
306:	if (mediaPlayer.isPlaying()) { 
307:	mediaPlayer.pause(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setDataSource
78:	MediaPlayer a= new MediaPlayer(); 
-
80:	a.setDataSource(context, videoUri); 
81:	a.prepare(); 
82:	int result = a.getDuration(); 
83:	a.release(); 
-
86:	a.release(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration
45:	return mediaPlayer.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, getVideoHeight, getVideoWidth, prepare, setDataSource
174:	MediaPlayer player = new MediaPlayer(); 
-
176:	player.setDataSource(filePath); 
177:	player.prepare(); 
178:	obj.put("duration", player.getDuration() / 1000); 
-
180:	obj.put("height", player.getVideoHeight()); 
181:	obj.put("width", player.getVideoWidth()); 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods: getDuration
41:	return mediaPlayer.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getCurrentPosition, getDuration
167:	return ((float) mPlayer.getCurrentPosition()) / mPlayer.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setDataSource
135:	mPlayer = new MediaPlayer(); 
136:	mPlayer.setDataSource("/sdcard/" + file); 
137:	mPlayer.prepare(); 
138:	duration = mPlayer.getDuration(); 
139:	mPlayer.release(); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: getDuration
42:	return mediaPlayer.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration
68:	return mp.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, prepare, reset
132:	player.reset(); 
133:	sourceProvider.setDataSource(player, filePath); 
134:	player.prepare(); 
135:	duration += player.getDuration(); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getDuration, getVideoHeight, getVideoWidth, prepare, setDataSource
177:	MediaPlayer player = new MediaPlayer(); 
-
179:	player.setDataSource(filePath); 
180:	player.prepare(); 
181:	obj.put("duration", player.getDuration() / 1000); 
-
183:	obj.put("height", player.getVideoHeight()); 
184:	obj.put("width", player.getVideoWidth()); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getDuration
65:	return player.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, prepare, reset, setDataSource, setOnBufferingUpdateListener
49:	m_mediaPlayer = new MediaPlayer(); 
50:	m_mediaPlayer.setOnBufferingUpdateListener(this); 
51:	m_mediaPlayer.setOnCompletionListener(this); 
52:	m_mediaPlayer.reset(); 
-
54:	m_mediaPlayer.setDataSource(path); 
-
63:	m_mediaPlayer.prepare(); 
-
69:	return m_mediaPlayer.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getCurrentPosition, getDuration, setOnSeekCompleteListener, start
267:	+ mMediaPlayer.getCurrentPosition()); 
268:	mMediaPlayer.start(); 
269:	mMediaPlayer.setOnSeekCompleteListener(null); 
-
273:	if (endTimeMS < mMediaPlayer.getDuration()) { 
Full Snippet Info
5
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setDataSource
302:	MediaPlayer mp = new MediaPlayer(); 
303:	mp.setDataSource(recorded_file); 
304:	mp.prepare(); 
305:	int duration = mp.getDuration(); 
-
309:	mp.release(); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getCurrentPosition, getDuration, isLooping, isPlaying
174:	result.put("duration", player.getDuration()); 
175:	result.put("position", player.getCurrentPosition()); 
176:	result.put("isplaying", player.isPlaying()); 
-
178:	result.put("looping", player.isLooping()); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, getVideoHeight, getVideoWidth, prepare, setDataSource
177:	MediaPlayer player = new MediaPlayer(); 
-
179:	player.setDataSource(filePath); 
180:	player.prepare(); 
181:	obj.put("duration", player.getDuration() / 1000); 
-
183:	obj.put("height", player.getVideoHeight()); 
184:	obj.put("width", player.getVideoWidth()); 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setDataSource, setDisplay
141:	MediaPlayer mediaPlayer = new MediaPlayer(); 
-
143:	mediaPlayer.setDataSource(outputFilePath); 
-
145:	mediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 
-
147:	mediaPlayer.prepare(); 
-
149:	mOutputDuration = mediaPlayer.getDuration(); 
-
153:	mediaPlayer.release(); 
-
156:	mediaPlayer.release(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, getVideoHeight, getVideoWidth, prepare, setDataSource
159:	MediaPlayer player = new MediaPlayer(); 
-
161:	player.setDataSource(filePath); 
162:	player.prepare(); 
163:	obj.put("duration", player.getDuration()); 
-
165:	obj.put("height", player.getVideoHeight()); 
166:	obj.put("width", player.getVideoWidth()); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, prepare, reset, setDataSource, setOnBufferingUpdateListener
49:	m_mediaPlayer = new MediaPlayer(); 
50:	m_mediaPlayer.setOnBufferingUpdateListener(this); 
51:	m_mediaPlayer.setOnCompletionListener(this); 
52:	m_mediaPlayer.reset(); 
-
54:	m_mediaPlayer.setDataSource(path); 
-
63:	m_mediaPlayer.prepare(); 
-
69:	return m_mediaPlayer.getDuration(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setDataSource
135:	mPlayer = new MediaPlayer(); 
136:	mPlayer.setDataSource("/sdcard/" + file); 
137:	mPlayer.prepare(); 
138:	duration = mPlayer.getDuration(); 
139:	mPlayer.release(); 
Full Snippet Info
6
CodeRank
This code example shows how to use the following methods: getDuration, prepare, setAudioStreamType, setDataSource, setDisplay
154:	mMediaPlayer = new MediaPlayer(); 
155:	mMediaPlayer.setDataSource(path); 
156:	mMediaPlayer.setDisplay(holder); 
157:	mMediaPlayer.prepare(); 
158:	mMediaPlayer.setOnBufferingUpdateListener(this); 
159:	mMediaPlayer.setOnCompletionListener(this); 
160:	mMediaPlayer.setOnPreparedListener(this); 
161:	mMediaPlayer.setOnVideoSizeChangedListener(this); 
162:	mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
163:	mSeekBar.setMax(mMediaPlayer.getDuration()); 
Full Snippet Info
5
CodeRank
This code example shows how to use the following methods: getDuration, prepare, release, setAudioStreamType, setDataSource
257:	mp = new MediaPlayer(); 
258:	mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
-
260:	mp.setOnErrorListener(this); 
-
262:	mp.setDisplay(mHolder); 
-
265:	mp.setDataSource(path); 
-
271:	mp.prepare(); 
272:	Log.v(TAG, "Duration:  ===>" + mp.getDuration()); 
273:	mp.setOnBufferingUpdateListener(this); 
274:	mp.setOnCompletionListener(this); 
275:	mp.setOnVideoSizeChangedListener(this); 
276:	mp.setOnPreparedListener(this); 
-
289:	mp.stop(); 
290:	mp.release(); 
Full Snippet Info
3
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

MediaPlayer class can be used to control playback of audio/video files and streams. An example on how to use the methods in this class can be found in VideoView.

Topics covered here are: State Diagram Valid and Invalid States Permissions Register informational and error callbacks Developer Guides For more information about how to use MediaPlayer, read the Media Playback developer guide. State Diagram Playback control of audio/video files and streams is managed as a state machine. The following diagram shows the life cycle and the states of a MediaPlayer object driven by the supported playback control operations. The ovals represent the states a MediaPlayer object may reside in. The arcs represent the playback control operations that drive the object state transition. There are two types of arcs. The arcs with a single arrow head represent synchronous method calls, while those with a double arrow head represent asynchronous method calls.From this state diagram, one can...
getDuration Method Overview
Gets the duration of the file.
See Also
  • isLooping
    Checks whether the MediaPlayer is looping or non-looping.
  • isPlaying
    Checks whether the MediaPlayer is playing.
  • prepare
    Prepares the player for playback, synchronously.
  • prepareAsync
    Prepares the player for playback, asynchronously.
  • release
    Releases resources associated with this MediaPlayer object.
  • reset
    Resets the MediaPlayer to its uninitialized state.
  • seekTo
    Seeks to specified time position.
  • setDataSource
    Sets the data source (file-path or http/rtsp URL) to use.
  • setDisplay
    Sets the SurfaceHolder to use for displaying the video portion of the media.
  • setLooping
    Sets the player to be looping or non-looping.
  • setOnCompletionListener
    Register a callback to be invoked when the end of a media source has been reached during playback.
  • setOnErrorListener
    Register a callback to be invoked when an error has happened during an asynchronous operation.
  • setOnInfoListener
    Register a callback to be invoked when an info/warning is available.
  • setScreenOnWhilePlaying
    Control whether we should use the attached SurfaceHolder to keep the screen on while video playback is occurring.
  • setWakeMode
    Set the low-level power management behavior for this MediaPlayer.
  • start
    Starts or resumes playback.
  • stop
    Stops playback after playback has been stopped or paused.