how to play midi file in android use fmod - android

I am trying to play midi file using fmod. But there is an error says that :a resource that the plugin requires cannot be found,(ie the DLS file for MIDI playback)
I have searched results for problems like this,and referred to the fmod.h files. It seems that I need a file named "gs_instrument.dls" but I cannot find it in my mac as well as the android simulator filesystem. I have also searched the resources in the web,no result either.
So what should I do if I want to play midi file in android using fmod.
Hope you can help me!
Thanks!

I don't know about fmod, but Android can play MIDI files right out of the box. Here's a simplified version of what works for me:
MediaPlayer mediaPlayer = new MediaPlayer();
File f = [... my MIDI file ...];
FileInputStream fis = new FileInputStream(f);
FileDescriptor fd = fis.getFD();
mediaPlayer.setDataSource( fd );
mediaPlayer.prepare();
mediaPlayer.start();

You specify the location of the DLS file with the dlsname member of the FMOD_CREATESOUNDEXINFO structure passed into System::createSound.
You must provide the actual file yourself and put it on the sdcard so you can pass in the location of it. On Windows the default DLS file is located at "C:\Windows\System32\drivers\gm.dls" or "C:\Windows\System32\drivers\etc\gm.dls". Alternatively on Mac it is located at "/System/Library/Components/CoreAudio.component/Contents/Resources/gs_instruments.dls". This being said I cannot speak to the legality of using these files in an Android project, you may need to source your own "free" dls file from somewhere else.

You can use like this. You can call following method where you want to play midi file.
public void myRingCtone()
{
Uri path = Uri.parse("android.resource://com.PackageName/raw/MIDI_RingToneName");
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),
RingtoneManager.TYPE_RINGTONE, path);
Log .i("TESTT", "Ringtone Set to Resource: "+ path.toString());
RingtoneManager.getRingtone(getApplicationContext(), path).play();
}

Related

How to use JaudioTagger to write data?

I am trying to use JaudioTagger 2.2.3 to edit tags of audio file but till now i have not achieved any success.
this is a test code to change the Artist tag of a mp3 file in Internal Storage
String path;
try
{
TagOptionSingleton.getInstance().setAndroid(true);
AudioFile f=AudioFileIO.read(new File(path));
Tag t=f.getTag();
t.setField(FieldKey.ARTIST,Str);
AudioFileIO.write(f);
}catch(CannotReadException e){}
catch(TagException e){}
catch(ReadOnlyException e){}
catch(InvalidAudioFrameException e){}
catch(CannotWriteException e){}
My Application is well elevated with android.permission.WRITE_EXTERNAL_STORAGE and android.permission.READ_EXTERNAL_STORAGE
I am testing in Android version Android 6.0
this code throws InvalidAudioFrameException with a message
No audio header found within example.mp3
Its not that the audio is corrupt as it shows similar error with other mp3s also
Also if any one has any other wat for audio tagging please do tell me and i have also used mp3agic which repeadedly shows:example.mp3:open failed :EROFS(Read only file system) on the line
Mp3File mp3file = new Mp3File(filePathStr+"example.mp3");
ID3v1 id3v1Tag;
if (mp3file.hasId3v1Tag()) {
id3v1Tag = mp3file.getId3v1Tag();
} else {
// mp3 does not have an ID3v1 tag, let's create one..
id3v1Tag = new ID3v1Tag();
mp3file.setId3v1Tag(id3v1Tag);
}
id3v1Tag.setTrack("5");
id3v1Tag.setArtist("An Artist");
id3v1Tag.setTitle("The Title");
id3v1Tag.setAlbum("The Album");
id3v1Tag.setYear("2001");
id3v1Tag.setGenre(12);
id3v1Tag.setComment("Some comment");
mp3file.save("example.mp3")//error showing in this line
My question
How to rectify Jaudiotagger?
Is there any way to use Mp3agic as an alternative to Jaudiotagger?
Or there is any other efficient way leaving these two?
I have also used JaudioTagger-android but same problem persists.
thanks in advance!
For the error when doing
mp3file.save("example.mp3")
i would recommend to not overwrite directly the old file. Try to create a new file, and then delete and rename to restore the original file setup.
song.save(location + "intermed");
File from = new File(location + "intermed");
File file = new File(location);
file.delete();
File to = new File(location);
from.renameTo(to);
Of course you need to add exception handling and what not, and also this will fail if you have no write permissions. This may be the case if you use a more recent Android version, i think above 4.4, and you try to access the SD card. Then, the write permissions need to be granted at runtime for specific folders, the manifest write permission is not sufficient. See Android's Storage Access Framework.

Can we change the extension of video and play it?

Currently I am downloading a ".mp4" video and saving it in a folder. I want to hide it from gallery. I know how to hide a folder. Currently I am saving it in a different extension for ex. like ".sss" and it is saving properly. Now how can I play that video in my application.
I tried to change the extension and in file and try to play it, but it is not working, here is my code
File extStore = Environment.getExternalStorageDirectory()+"/"+"downloadFolder"+"/"+"video1.sss".replace(".sss",".mp4");
JCVideoPlayer mJcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
mJcVideoPlayerStandard.setUp(extStore.getPath(),JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "Normal");
mJcVideoPlayerStandard.showContextMenu();
I am using this path to play the video,and I am usign JcVideo player lib for playing the video, here is the gradle for it
compile 'fm.jiecao:jiecaovideoplayer:5.4'
When I am saving the file in ".mp4" format I am able to play the video. But when I use different format,I am not able to play it. Any help will be preferable
I think what you are trying to do is to rename the file to mp4 before you play it?
If so the code above is replacing the the '.sss' with '.mp4' in the search string rather than renaming it in the file system.
In other words the line:
File extStore = Environment.getExternalStorageDirectory()+"/"+"downloadFolder"+"/"+"video1.sss".replace(".sss",".mp4");
simply translates to:
File extStore = Environment.getExternalStorageDirectory()+"/"+"downloadFolder"+"/"+"video1.mp4";
As your actual file is still video1.sss you get an error when you try to play a file named video1.mp4 as it can't be found.
If you do want to rename a file you can do it like this:
File file1 = new File("Path of file");
File file2 = new File("Path with new name for the file");
boolean ok = file1.renameTo(file2);
I think you simply can't do it! by changing the extension of a video file you can't play it. because no matter what its extension is, the Audio and Video format coding format won't change by changing extension name. If a player can't play such extension, it is not in its supported video codec.
The Audio and Video format define how a video file is build byte to byte. check this Wikipedia page.

Android Assets-Folder: it takes always the first file by alphabetical order

I'm new in Java/Android programming, so please have patience with me.
I try to play a mp3 which is locate und the assets folder. I know there is another way with the /res/raw/ folder, but use the assets-folder because later I'll try to access the file by String.
This code works to play a mp3-file:
try
{
MediaPlayer mp = new MediaPlayer();
FileDescriptor sfd = getAssets().openFd("song.mp3").getFileDescriptor();
mp.setDataSource(sfd);
mp.prepare();
mp.start();
}
catch(Exception e) {}
Now the problem: In the same assets-folder is another mp3 file stored. Though I specify the name of the mp3 to use it take the one which comes first in alphabet. E.g. the other file is named "music.mp3" it plays this one. Renaming it to "worldmusic.mp3" it will play "song.mp3". Rerename "worldmusic.mp3" back to "music.mp3" it will take this mp3 again. Another test: Renaming "song.mp3" to something other so the application can find whats specify by the code above will result that no song is played. So this means the songname have to exist, although it take arbitrary the song first in alphabet.
I'm testing with the AVD emulator of eclipse. But I think the behaviour would be the same on a real device.
Have someone an idea about this problem?
I don't believe using FileDescriptor is the right way to do this. Try using .create() and a Uri instead:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), songUri);
mp.start();
Not sure why, but the URI syntax doesn't seem to work for the assets. Try getting the AssetFileDescriptor instead, as answered in the related question:
Play audio file from the assets directory

MediaPlayer cant play audio files from program data folder?

When i record my audio from MIC and store file in /data/data/..... why
MediaPlayer can't play this file ? If i change destination to /
sdcard/..... - all works great. I do something wrong ? I not found
limitation for MediaPlayer. Device - Samsung T959 (Galaxy S)
Thanks, i hope anybody know solution....
i had the same issues... whenever i used setDataSource with filepaths, it would not work; kept getting IOException.
changing my code to use setDataSource(FileDescriptor) does work... and with this i don't have to copy the files to SDCard or anything like that.
So, with a simple File object, create a new FileInputStream and pass the actual file descriptor as data source as in:
setDataSource((new FileInputStream(myFileInstance)).getFD());
creating an input stream pulls the file data into memory and thereby addresses /data/data access violations.
Better practice is to close the stream, like this:
FileInputStream stream = new FileInputStream(path);
mediaPlayer.setDataSource(stream.getFD());
stream.close();
Even better from Kotlin:
mediaPlayer.setDataSource(
FileInputStream(path).use { it.fd }
)
It is noted in the documentation of the method:
android.media.MediaPlayer.setDataSource(FileDescriptor fd)
Sets the data source (FileDescriptor) to use. It is the caller's
responsibility to close the file descriptor. It is safe to do so as
soon as this call returns.
use MediaPlayer.setDataSource(FileDescriptor fd)
Try setting ContentValues and to store some standard meta-data properties. Then using a ContentResolver to set the meta-data and Uri to the file.
see: http://developer.android.com/guide/topics/media/index.html
"Example: Audio Capture Setup and Start", then try changing Uri base to your /data/data/-filename.
Due to the Android security model, MediaPlayer haven't enough rights. It can access SD card, but can't access another places with out permissions.
As so, setDataSource(...) can thrown SecurityException and I think it's happening.
You can play this file next ways:
copy it to temp dir and play;
copy it to temp dir and play;
copy it to sdcard;
read it fully to memory and try play via stream.

Using java with android how can I get the path of my resources files?

I added some audio files to the res file.
first I created a raw dir and then I added the folder with the audio files.
How can I get each file path? (I want to use the path to play this audio files)
I found that I need to use
classLoader.getResource
but I don't understand how. what is the argument for this function?
A reference should be added to your R class that you can get as something like R.raw.sound_file_1. Check out the Audio and Video section of the Android Developer's Guide for more details.
Once you have the reference to your audio clip you can play it by:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
So you don't really need the path to the resource at all, just use the reference.

Categories

Resources