Code example for MediaPlayer

Methods: getDurationpreparesetDataSource

0
		if (resId < 0) {
			if (inPath.charAt(0) == File.separatorChar) {
				try { 
		        	FileInputStream fis = new FileInputStream(new File(inPath));
			        FileDescriptor fd = fis.getFD();
					mp = new MediaPlayer();
					mp.setDataSource(fd);
					mp.prepare();
		        } catch(FileNotFoundException e) { 
		            System.out.println(e.getMessage());
		            return null; 
		        } catch(IOException e) { 
		            System.out.println(e.getMessage());
		            return null; 
		        } 
		    } else { 
				Uri uri = Uri.parse(inPath);
				mp = MediaPlayer.create(mContext, uri);
		    } 
		} else { 
			mp = MediaPlayer.create(mContext, resId);
		} 
 
		return mp;
	} 
 
	public static int playMusic(String inPath, double inVolLeft, double inVolRight, int inLoop, double inStartTime)
    { 
    	Log.i("Sound", "playMusic");
		 
		if (mediaPlayer != null) {
			mediaPlayer.stop ();
		} 
		 
		MediaPlayer mp = createMediaPlayer(inPath);
		if (mp == null) {
			return -1; 
		} 
 
		return playMediaPlayer(mp, inPath, inVolLeft, inVolRight, inLoop, inStartTime);
	} 
		 
	private static int playMediaPlayer(MediaPlayer mp, final String inPath, double inVolLeft, double inVolRight, int inLoop, double inStartTime)
	{	 
		mediaPlayer = new ManagedMediaPlayer(mp, (float)inVolLeft, (float)inVolRight, inLoop);
		mediaPlayerPath = inPath;
		mp.seekTo((int)inStartTime);
		mediaPlayer.start();
 
		return 0; 
	} 
 
	public static void stopMusic(String inPath)
	{ 
		Log.v("Sound", "stopMusic");
		 
		if (mediaPlayer != null && inPath.equals(mediaPlayerPath)) {
			mediaPlayer.stop ();
		} 
	} 
	 
	public static int getDuration(String inPath)
	{ 
		int duration = -1;
		if (mediaPlayer != null && inPath.equals(mediaPlayerPath)) {
			duration = mediaPlayer.getDuration ();
		} else { 
			MediaPlayer mp = createMediaPlayer(inPath);
			if (mp != null) {
				duration = mp.getDuration();
				mp.release();
			} 
		} 
 
		return duration;