How to get the full path Mp3 songs from internal storage? - android

I want to implement a simple Media Player.How can i retrieve the full path of Mp3 songs from internal storage in android.

mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
mpintro.start();

If you have any problem search first. do your efforts you'll find it...
here's a link try it Get Mp3 from SD card

Try this check for file if it exists or not
public static String rootPath = Environment.getExternalStorageDirectory() + "/YourDirectory/";
File file = new File(Environment.getExternalStorageDirectory()
+ "/YourDirectoryFolder/Audio/" + NmaeofFile + "/");
if (!file.exists())
return false;
else{
//Do your work here
}
And then simply start media player
player.setDataSource(rootPath);//rootpath is the path of your file
player.prepare();
player.start();

Related

How to retrieve a particular file in an Android Device?

I have created an Android Application to download an .mp3 file from server and store it in a folder in internal memory. Now I want to fetch that downloaded file and play it for the user.
How can I retrieve that single file and play that file using Media Player .?
Can anyone find me a solution for this ?
For getting the file and changing its Extension this code maybe usefull..
public void jjj(View view) {
File file = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.srt"); // handler to your ZIP file
File file2 = new File(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.mp3");
// destination dir of your file
boolean success = file.renameTo(file2);
//-----------------------------------------------------------------------------------------------------------
final MediaPlayer mp=new MediaPlayer();
try{
//you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3
mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Jithin's/downloadedfile.mp3");
mp.prepare();
}catch(Exception e){e.printStackTrace();}
mp.start();
//------------------------------------------------------------------------------------------------------------
if (success) {
// File has been renamed
Toast.makeText(MainActivity.this, "Changed Extension .... ", Toast.LENGTH_SHORT).show();
}
EDIT: the question was first, how to play a MP3 file from a file from internal storage. My answer is related to that question.
You can use Context.getFilesDir() method to get the path to the internal storage.
Usage:
String path = getFilesDir() + "/" + name;
Note that the name can also contain directory names, if you put the mp3 file in a subdirectory.
Playing it:
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start()

Android recorded aac(.mp3) file not playing in iOS

Hi I'm using the following code to record audio on Android
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
myRecorder.setAudioSamplingRate(44100);
myRecorder.setAudioEncodingBitRate(256);
myRecorder.setAudioChannels(1);
voiceFileName = getFilename();
myRecorder.setOutputFile(voiceFileName);
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, "test");
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
but that aac(.mp3) file does not play on iOS.
EDIT
let audioFilename = getDocumentsDirectory().stringByAppendingPathComponent("whistle.mp3")
let audioURL = NSURL(fileURLWithPath: audioFilename)
let whistlePlayer:AVAudioplayer = try AVAudioPlayer(contentsOfURL: audioURL)
whistlePlayer.play()
How do I play this file in IOS(Swift)?
i got solution just changed extension to .m4a it work on ios
let audioFilename = getDocumentsDirectory().stringByAppendingPathComponent("voice.m4a")
let audioURL = NSURL(fileURLWithPath: audioFilename)
do
{
audioPlayer = try AVAudioPlayer(contentsOfURL: audioURL)
audioPlayer.delegate = self
audioPlayer.play()
}
catch{
print("filenotfound")
}
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); suggests you should have been writing to mp4 (m4a). Not sure aac can be written to mp3 container
check if you can play that file in safari of iPhone.
and can you tell how are you trying to play that file?

Can not play video from my internal application directory

It looks like I can not play videos stored in my internal application directory.
I have videos stored in /data/data/my.package.org/files/
And I'm trying to play a file from there using
String fpath = "/data/data/my.package.org/files/video.mpg"
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(fpath), "video/*");
But both Android default video player and some external videoplayer (MX player) say 'this video can not be played".
Whereas when I'm saving videos to SD card they are played fine.
Why is that?
Put your video in assets folder and use this code to play video with MediaPlayer
InputStream is = getResources().getAssets().open("video.mpg");
OR
Put your video in assets folder and...
String fpath = "/data/data/my.package.org/assets/video.mpg"
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(fpath), "video/*");
put your video in the res\raw folder. Then copy it out to the external directories where apps like video player can read them. Only your app can read /data/data/blah....
if its in res\raw, then
typeName = sourceSink.Types.video.toString();
for (sourceSink.VideoFiles video: sourceSink.VideoFiles.values() ){
resourceName = video.toString();
fileName = resourceName + ".mp4";
resource = getResources().getIdentifier(resourceName, "raw", "com.invodo.allshareplay");
createExternalStoragePublicFile(typeName,fileName,resource);
}
and then you can use this to copy it:
void createExternalStoragePublicFile(String fType, String fname, int res ) {
// Create a path where we will place our picture in the user's
// public pictures directory. Note that you should be careful about
// what you place here, since the user often manages these files. For
// pictures and other media owned by the application, consider
// Context.getExternalMediaDir().
File path = null;
if (((fType.equals(sourceSink.Types.photo.toString())) || (fType.equals(sourceSink.Types.file.toString())) ) ){
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
}
if (fType.equals(sourceSink.Types.music.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
}
if (fType.equals(sourceSink.Types.video.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES);
}
File file = new File(path, "/" + fname);
try {
// Make sure the Pictures directory exists.
path.mkdirs();
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(res);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
scanMedia(file);
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
then it's easy - just do the second half of the answer I provided and write them out. /data/data/ folder is never going to be viewed by anything other than your app.

Can't get media player to work when SD drive is not installed

First I make a call to
get_fullPathToAudio();
to determine the path of where an audio file was downloaded.
public File get_fullPathToAudioAsFile()
{
File storageFile = this.getExternalFilesDir(null);
if (storageFile == null)
{
// no external storage so store on private path
storageFile = this.getFilesDir();
}
return storageFile;
}
public String get_fullPathToAudio() {
return get_fullPathToAudioAsFile() + File.separator + this.get_currentArticleAudioFileName();
}
String filePath = this.myApp.get_fullPathToAudio();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start();
All works well if I have an SD drive on my emulator but when I don't have an emulator the function
get_fullPathToAudioAsFile();
uses
getFilesDir();
instead of
getExternalFilesDir(null);
which returns a path to my private drive where my app is installed. I can see the file in
data/data/com.myapp/files/myfile.m4a
for example. Is there a different way to play an audio file if it's not on the SD drive?
The error messsage I get is
error(-1, -2147483648)
Could not open file null for playback
I solved it this way
FileInputStream fileInputStream = new FileInputStream(mFile);
mPlayer.setDataSource(fileInputStream.getFD());
mPlayer.prepare();
MediaPlayer error -2147483648 when playing file on internal storage

Recorded Audio using Native Audio Recorder Issue

I am recording audio using Audio recorder (native app) of android.Using following code :
final Intent audioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
String path = Environment.getExternalStorageDirectory()
.toString();
File AudioFolder = new File(path + "/MyFolder/",
"RecordedAudio");
File audioFile = new File(AudioFolder, "myaudio.mp4");
Uri audioFileUri = Uri.fromFile(audioFile);
audioIntent.putExtra(MediaStore.EXTRA_OUTPUT, audioFileUri);
startActivityForResult(audioIntent, 0);
Audio recording is working fine with me. After successful recording It has been stored default in sdcard. I am not able to store it with my particular folder "MyFolder" that is located in sdcard so It might be store in /sdcard/Myfolder/RecordedAudio/myaudio.mp4.
What is going wrong with my above code.?
Please help me with your suggestions.
Thanks

Categories

Resources