Play a sound from sdcard0 in android - android

MediaPlayer mp;
mp = MediaPlayer.create(this, R.drawable.test);
mp.start();
That's my code. It just plays a sound from res/drawable/ folder. I need to play a sound from android's sdcard0/Test Folder/testsound.amr How to do this ?
Please Help. Thanks in advance.

Just use the setDataSource Method:
MediaPlayer mp = new MediaPlayer();
//set the source of the Sound...
mp.setDataSource("/mnt/sdcard/Test Folder/testsound.amr");
//load the file
mp.prepare();
//Do the thing (:
mp.start();
You can also do it dynamically by doing:
mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Test Folder/testsound.amr");
NOTE: I'm not clearly sure if you need to do .getPath() or not...

Related

How to get mp3 file uri and play it in android programatically

I want to make a application in which i play the .mp3 file
i play the sound by selecting it from raw or asset folder which is hard coded in code.
But i want that user pick any mp3 file anywhere from their android phone and make play it
Can any one help me
Thanks in advance
MediaPlayer mediaPlayer= MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
int duration = mediaPlayer.getDuration();
int current_position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
Great tutorial on Media Playback through MediaPlayer:
http://www.tutorialspoint.com/android/android_mediaplayer.htm

Media Player set path of another folder

I am making a simple app that plays an mp3 file. I use the code
mp = MediaPlayer.create(MainActivity.this, R.raw.mysong);
But this works only if my song is saved in the res\raw folder of my PC. Sooner or later I will run the app of my iphone (as soon as I buy one!). What happens if I must set the path to the one that the mobile is saved? Let's say my Downloads folder from my mobile phone.
You can do it by giving it it's absolute path from the ExternalStorage as follwoing
mp = MediaPlayer.create(MainActivity.this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/mysong.mp3"));
OR Use setDataSource() method:
String PATH_TO_FILE = "/sdcard/music.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();
mediaPlayer.start()
String filePath = "somepath/somefile.mp3";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();

Audio attachment with image in android

I have image is my Draw able folder.When I call this image I want to play an audio also which is in my raw folder.can I do this how??please help me.
You can achieve by creating image button and put image on it. and onClick of that button you can play the music using MediaPlayer api.
Refer this : http://developer.android.com/guide/topics/media/index.html
Example code for Play audio from Raw folder
: http://www.barebonescoder.com/2010/06/android-development-audio-playback-safely/
you can write this code at onResume:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),
R.raw.name);
mp.start();
while (mp.isPlaying()) { // donothing
}
;
mp.release();

How to Play MID file in Android?

I am writing a small piece of code
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("http://..../file.mid");
mediaPlayer.prepare();
mediaPlayer.start();
which is not working, where as
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("http://..../file.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
is working fine.
Please see this thread on the Pragmatic Forums:
Java SE5 javax.sound (MIDI) classes removed from Android libraries?
Your best option appears to be through the JetPlayer class

android - application displaying videos

how do i display videos retrieved from resources in my android application ?
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(context, R.raw.vid);
mp.start();
i have placed a vid.3gp file in my res/raw folder..
i am getting NullPointerException.. what changes do i need to make
why so??
do we need something called as Surface Holder or something similar ?
using the MediaPlayer you should be able to play video

Categories

Resources