Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I go little problem and maybe you know what i can do. I got database and one of the column is RECORD_PATH, in this column i want to store path for my sound and use it on Media Player. Right now I don't know which way is better (sound on SD/ASSETS/RAW). Don't know also how I can tranfer this path to MEDIA PLAYER ? Someone got similar problem like this and solved it on own project ?

share|improve this question
    
Where does your sound come from ? If it is recorded by the user, best option will be to store it on SD, If the sound file ships with your app, then you should use a raw asset. Then it is just a matter of using the API's for media playback with the path from your database. – JonasCz Mar 24 '15 at 17:52
    
Thx for answering @JonasCz. My sound type file is mp3 and ships in my app. Do you know how I can transfer this path from my database to Media Player ? – Artur Mar 24 '15 at 17:56

You will want to use the built in constants for getting access to the users SD Card.

You can get the path for this from the following call:

Environment.getExternalStorageDirectory().getAbsolutePath()

Don't forget to add the following permission though to your manifest:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

So putting all together you can do something similar to this for saving the file:

String myAppDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + 'raw';
String fileName = "myFile.wav";

String filePath = myAppDir + File.separator + fileName;

File f = new File(filePath);
f.write(...);
f.flush();
f.close();

Then to pass the file to a built in player you can make and call an Intent as follows:

File file = new File(filePath);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(i);

Let me know if you have any questions and I can expand the answer a bit more.

Updates:

The above is to save a file and then play that file using an inbuilt media player.

If the file is already on the SD card and you want to programmatically play the file you can use the following code:

mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/raw/intro.mp3"));
mpintro.setLooping(true);
        mpintro.start();
share|improve this answer
    
Very usefull answer :) Leet me explain how my application works. I read and display data from DB ofcourse i got something like nextbutton which move me to next row. For each dispay i want to play correct sound for this row. Don't know how your answer will works with my project :( – Artur Mar 24 '15 at 18:21
    
for example 1 row in my database "string 1", "string 2","string 3", "string 4","record_path". If I got in record_path for exaple sd/sound/sound.mp3 i didint see a point to use this all of your code – Artur Mar 24 '15 at 18:24
    
Hi @Artur, I updated my answer to show you the code you might need to play a Media file from the SD card. Is this closer to what you were after? – JodiMiddleton Mar 24 '15 at 21:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.