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();
Related
In my application I want to play server m3u file directly with out downloading the file. For this i am using bellow code
String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();`enter code here`
But did not work.
this is the server url file
http://stream.abacast.net/playlist/mwfmadison-wrisfmmp3-ibc3.m3u.
So can any one please how to do this
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.
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...
Is there any folder like res/drawable for mp3 or generally audio files? If yes, what is it and how can I get access to it from the app?
The best place to put such .mp3 or any other files would be in the assets folder.
These files once stored will become a part of your android app itself and can be read easily. This tutorial describes it well.
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
Alternatively you can also store it in the raw folder and read it directly by specifying the path as the raw folder.
this can be played as:
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
Here are some steps you can easily follow.
Open the android studio with the project in which you want to add-on audio clip/media file.
Create a raw folder in the resources folder.
Add media file to the raw folder by simply copy and paste that to the raw folder.
Here we added a media file “ring.mp3”. Now open the Java File of the desired activity, here we are adding the audio in the MainActivity.
Further add this code.
MediaPlayer ring = MediaPlayer.create(MainActivity.this, R.raw.ring);
ring.start();
Now run the App and your music will play when App starts
You should save the .mp3 into res/raw. AndroidStudio recognizes the raw folder. (By contrast, it does not automatically recognize a res/assets folder).
To play music.mp3:
mediaPlayer = MediaPlayer.create(ctx, R.raw.cat_meow);
mediaPlayer.start();
Note the convenient use of R. syntax.
Place it into your assets folder. Preferably under assets/raw/myfile.mp3
You can access it using:
String mp3File = "raw/music.mp3";
AssetManager assetMan = getAssets();
MediaPlayer media = new MediaPlayer();
FileInputStream mp3Stream = assetMan.openFd(mp3File).createInputStream();
media.setDataSource(mp3Stream.getFD());
media.prepare();
media.start();
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