Using java Syntax Highlighting
- AssetFileDescriptor mgrFileDesc = mgr.openFd("Sounds/" + grp.getChildAt(chkId).getTag().toString());
- FileDescriptor fd = mgrFileDesc.getFileDescriptor();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
If i run a toast Message here i see that the fd.toString() returns:
Using java Syntax Highlighting
- java.io.FileDescriptor@44767fc12
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
But the MediaPlayer throws an IO Exception, when i attempt to open this fd, if i toast the e.getMessage(), it returns:
Using java Syntax Highlighting
- Prepare Failed.:status =0x1.
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
I do notice however, that every time, i go from clicking one button, to another and then back to that same button, that the FileDescriptor, is different. Not just between files, but between clicks as well.
I am able to get the MediaPlayer to play files that are stored in R.raw but i cant reference those dynamically, so i'm a little confused.
i'm not sure how to debug it from here, so anyhelp would be greatly appreciated.
Also, in a nut shell, i want to dynamically generate the UI, & eventHandlers to play selected file, for an unlimited number of files in my assets directory. Unlimitied meaning, i dont want to hard code those filenames.
My code is below, i've left out just my package name....
Using java Syntax Highlighting
- import java.io.*;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ScrollView;
- import android.widget.LinearLayout;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- import android.media.MediaPlayer;
- import android.widget.RadioButton;
- import android.widget.TextView;
- import android.content.res.*;
- public class TWSSConfig extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- AssetManager mgr = getAssets();
- //Start by creating our dynamic view of the file list
- ScrollView scrView = new ScrollView(this);
- LinearLayout linLay = new LinearLayout(this);
- //Now define some properties for these layout values
- linLay.setOrientation(LinearLayout.VERTICAL);
- //Set the TextView
- TextView txtTitleArea = new TextView(this);
- txtTitleArea.setTextAppearance(this, android.R.style.TextAppearance_Medium);
- txtTitleArea.setText(R.string.greeting);
- linLay.addView(txtTitleArea);
- //Add a RadioGroup Since for now its only one option
- RadioGroup rdGroup = new RadioGroup(this);
- //Now use our file list to create all buttons for this group and set all
- try {
- String list[] = mgr.list("Sounds");
- if (list != null)
- for (int i=0; i<list.length; ++i)
- {
- //Add a group button for files
- RadioButton rdBut = new RadioButton(this);
- //Applies an id to the chkBox
- rdBut.setId(i);
- rdBut.setTag(list[i]);
- rdBut.setText(list[i]);
- rdGroup.addView(rdBut);
- }
- } catch (IOException e) {
- Toast.makeText(getBaseContext(), "List error: can't list" + "/assets/Sounds" , Toast.LENGTH_SHORT).show();
- }
- //Add Listener
- rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
- public void onCheckedChanged(RadioGroup grp, int chkId){
- //So now we know which chkBox is checked, so now we want to set that
- //play that file for the user
- try {
- AssetManager mgr = getAssets();
- AssetFileDescriptor mgrFileDesc = mgr.openFd("Sounds/" + grp.getChildAt(chkId).getTag().toString());
- FileDescriptor fd = mgrFileDesc.getFileDescriptor();
- MediaPlayer mPlay = new MediaPlayer();
- mPlay.setDataSource(fd);
- Toast.makeText(getBaseContext(),fd.toString() , Toast.LENGTH_SHORT).show(); mPlay.prepare();
- mPlay.start();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- // e.printStackTrace();
- Toast.makeText(getBaseContext(),"ERROR:"+ e.getMessage() , Toast.LENGTH_SHORT).show();
- }
- }//end of onCheckedChanged method
- }//End of OnCheckedChangedListener deceleration
- );//end of rdGroup.setOnCheckedChangedListener
- linLay.addView(rdGroup);
- //Add this Linear Layout to scroll view
- scrView.addView(linLay);
- this.setContentView(scrView);
- } //end of onCreate
Parsed in 0.048 seconds, using GeSHi 1.0.8.4

