7

I'm using several media player objects to loop some tracks and I want to know that is the difference between using the MediaPlayer.create(resId) versus manually programming the different states, using setDataSource(FileDescriptor) ect.. I'm still new to android so I have no idea.

user3094038
  • 101
  • 5

1 Answers1

4

.create() is a static method of MediaPlayer class, whenever you want to call .create() you have to call it by ClassName.methodName() like MediaPlayer.create()

while setDataResource() is a method in MediaPlayer class it will be call through the instance of MediaPlayer like

MediaPlayer mp;
mp.setDataResource("your sdCard File Path...");



Now if you use MediaPlayer.create() you should have audio(mp3) file in your raw folder under res. If you don't have raw folder create one (normally we have to create raw folder manually in our project) and pass the resId of that mp3 file in .create() method like

MediaPlayer mp = MediaPlayer.create(R.raw.mp3FileName);

Second one is setDataResource() method is used where you want to play audio files through your SDCard but You need to make sure the path you give to setDataSource() is exactly correct. The best way to do this, instead of hardcoding the reference to '/sdcard/', is to use

android.os.Environment.getExternalStorageDirectory()
MediaPlayer mediaPlayer = new MediaPlayer();
File path = android.os.Environment.getExternalStorageDirectory();
mediaPlayer.setDataSource(path + "/fileName.mp3");


In this way you can get correct path and play your mp3 through SDCard. Hope this explaination will help you to understand. For more info see MediaPlayer From Android Developer Site

Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47