How to set custom music on Android? - android

I want to make a music player that can play all music on my memory card.
I use this code for play a music, but i can't play music from memory card, only in raw folder.
MediaPlayer mp = MediaPlayer.create(this, R.id.raw.audio.mp3)
what I should do, so i can play music from SD card ?

You'll want to do something like this
Uri song = Uri.parse(location-of-song); //location-of-song is where the music is on the sd card
mPlayer = new MediaPlayer();
mPlayer.setDataSource(getApplicationContext(), song);
mPlayer.start();

Use the version of create that takes a URI, and provide a URI to the local file you want to play.

Related

how to play music on click of an action button

i am trying to play music or mp3 on click of an action button but none is working since most of the tutorials are based on button listeners. i need help regarding how to play music on click of an option menu or action bar menu. Can anyone please help me regarding this problem.
Here is a recommended solution to playing a mp3:
Link: Simple mediaplayer play mp3 from file path?
String filePath =
Environment.getExternalStorageDirectory()+"/yourfolderNAme/yopurfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start()
and this play from raw folder.
int resID=myContext.getResources().getIdentifier(playSoundName,"raw",myContext.getPackageName());
MediaPlayer mediaPlayer = MediaPlayer.create(myContext,resID);
mediaPlayer.prepare();
mediaPlayer.start();

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();

Android playback recorded audio

I've been looking at this example https://stackoverflow.com/a/8974361/1191501 and it works perfectly. But my problem is how do I reference the recorded audio so it can be played back straight away?
the output code is:
recorder.setOutputFile("/sdcard/audio/"+filename);
and this definitely records the audio.
and then to playback the audio, I was using:
player.setDataSource();
but I don't know how to reference the filename bit so it plays back. Any ideas?
I had similar problems playing audio from the SD card at one point. This is what did it for me:
private void playMedia() {
String path = Environment.getExternalStorageDirectory() + "/audio_stuff.mp3";
mediaPlayer = MediaPlayer.create(this, Uri.parse(path));
mediaPlayer.start();
}
Make sure to release your MediaPlayer instance and set it to null when you are done. And just in case, make sure your SD card is not mounted when you try to play your audio file. :)
Looking here,
player.setDataSource("/sdcard/audio/"+filename);
player.prepare();
player.start();
would work I would think.

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