I am trying the app for speech synthesis by using some DSP approach. Fortunately, the TrasosDSP supports most of these approaches for android.TrasosDSP for Android. But I cannot play audio with AndroidAudioPlayer in TrasosDSP although I tested according to their sample code. I cannot able to trace it is why? Here is the sample code from TrasosDSP for Android.
With the TarsosDSP Android library the following code plays an MP3 from external storage:
new AndroidFFMPEGLocator(this);
new Thread(new Runnable() {
#Override
public void run() {
File externalStorage = Environment.getExternalStorageDirectory();
File mp3 = new File(externalStorage.getAbsolutePath() , "/audio.mp3");
AudioDispatcher adp;
adp = AudioDispatcherFactory.fromPipe(mp3.getAbsolutePath(),44100,5000,2500);
adp.addAudioProcessor(new AndroidAudioPlayer(adp.getFormat(),5000, AudioManager.STREAM_MUSIC));
adp.run();
}
}).start();
Provided your music is in the Music folder, try replacing:
File externalStorage = Environment.getExternalStorageDirectory();
File mp3 = new File(externalStorage.getAbsolutePath() , "/audio.mp3");
with
File mp3 = new File(Environment.getExternalStorageDirectory(), "Music/audio.mp3");
For some reason this way it works, but it took me 2 weeks of puzzling.
Related
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()
How to check a video file is valid or not without checking its extension(.mp3 or .3gp etc). Means how to check the video file on SD card is supported by device or not?
Is there any api to validate video file in android 4.0 and above?
My Scenario: I am playing video on VideoView after downloading it and play it from local SD card after download success. Next time when a request for same video, then checks in SD card, if found then start playing it(No download in this case). But sometimes network error or app kill interrupt the downloading(in this case the video file is not completely downloaded) so the downloaded file is corrupted and VideoView is unable to play this file. So how to detect this corrupted file.
Here is the code that worked for me:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(context, Uri.fromFile(fileToTest));
String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
boolean isVideo = "yes".equals(hasVideo);
#Alex had given right answer but still some problems are there as like #Kirill mention in comment that setDataSource often throws java.lang.RuntimeException: setDataSource failed exception. So Here is the function check for valid video file
private boolean videoFileIsCorrupted(String path){
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(myContext, Uri.parse(path));
}catch (Exception e){
e.printStackTrace();
return false;
}
String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
return "yes".equals(hasVideo);
}
I think this will be useful, The method is an older one that i used for other purpose just modified a bit for your use, Try it ,May work
private void checkAndLoadFile(
File currentFile) {
String path = currentFile.getAbsolutePath();
String extension = Utility.getExtension(path);
MimeTypeMap mimeMap = MimeTypeMap.getSingleton();
if(mimeMap.hasExtension(extension))
{
String mimeType = mimeMap.getMimeTypeFromExtension(extension);
Intent viewFile = new Intent(Intent.ACTION_VIEW);
viewFile.setDataAndType(Uri.fromFile(currentFile), mimeType);
PackageManager pm = getPackageManager();
List<ResolveInfo> apps =
pm.queryIntentActivities(viewFile, PackageManager.MATCH_DEFAULT_ONLY);
if (apps.size() > 0)
startActivity(viewFile); //This video is supported and there are apps installed in this device to open the video
else
showAsNotSupported();
} else
showAsNotSupported();
}
I am having a weird issue reading the length/duration of a video file recorded with a device's camera by using MediaRecorder. The file is recorded into the application's private storage directory, which is set like so:
mMediaRecorder.setOutputFile(context.getFilesDir() + "/recordings/webcam.3gpp");
After recording is complete, I attempt to read the length of the video with these methods:
Method 1:
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(context.getFilesDir() + "/recordings/webcam.3gpp");
String time = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
return Long.parseLong(time);
Method 2:
MediaPlayer mp = MediaPlayer.create(context, Uri.parse(context.getFilesDir() + "/recordings/webcam.3gpp"));
long duration = mp.getDuration();
mp.release();
return duration;
None of the methods work. mediaMetadataRetriever.extractMetadata returns null and MediaPlayer.create fails with an IOException. I have verified that the file exists.
Important note: This issue DOES NOT happen if I save the recording to "/sdcard/recordings/webcam.3gpp". For some reason, I just cannot read the duration when the file is in the private files directory that belongs to the application. Also, this issue ONLY happens on my Samsung Droid Charge, which runs Android 2.3. It DOES NOT happen on a Samsung Galaxy S4, which runs Android 4.2, and Asus Nexus 7, which runs Android 4.3.
Edit:
If I take the same file and copy it to the sdcard, then read the length of it there, everything works. What gives?
copy(new File(context.getFilesDir() + "/recordings/webcam.3gpp"), new File("/sdcard/wtfisthiscrap.3gpp"));
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource("/sdcard/wtfisthiscrap.3gpp");
String time = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
return Long.parseLong(time); // works!
What can I do to solve this issue?
I was able to solve my issue by setting a FileInputStream as the data source of MediaPlayer.
MediaPlayer mp = new MediaPlayer();
FileInputStream stream = new FileInputStream(context.getFilesDir() + "/recordings/webcam.3gpp");
mp.setDataSource(stream.getFD());
stream.close();
mp.prepare();
long duration = mp.getDuration();
mp.release();
return duration;
The source of my answer comes from https://stackoverflow.com/a/6383655/379245
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
I want to download mp3 file, on mobile device iOs or android, because i dont want to download every time from server.
I write simple code, but not work correctly. File size is too big, because is not encode and after save I cant play it again.
Please help and sorry for bad English.
private var sound:Sound;
public function loadSound(tmpUrl:String):void
{
sound = new Sound();
sound.addEventListener(Event.COMPLETE,onLoadSuccessSound, false, 0, true);
sound.load(new URLRequest(tmpUrl));
}
protected function onLoadSuccessSound(event:Event):void
{
var fileData:ByteArray = new ByteArray();
sound.extract(fileData, sound.bytesTotal);
var file:File = new File(File.applicationStorageDirectory + "\\ppp.mp3");
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeBytes(fileData,0,fileData.length);
fs.close();
}
Basically answer is to use URLStream and FileStream.
Question is pretty widespread and covered, for example, here Download a file with Adobe AIR and AS3: URLStream saving files to desktop?