I have  an Activity that dynamically creates a group of radio buttons,
using the Asset Manager, and then assigns the file to the Tag portion
of the radio button.  The Listener for these radio buttons, then looks
at the Tag, to get the filename,  which is used by the asset file
descriptor to get the fileDesc

AssetFileDescriptor mgrFileDesc = mgr.openFd("Sounds/" + grp.getChildAt
(chkId).getTag().toString());

FileDescriptor fd = mgrFileDesc.getFileDescriptor();

If i run a toast Message here i see that the fd.toString() returns:

java.io.filedescrip...@12345678

put the MediaPlayer throws an IO Exception, when i attempt to open
this fd, if i toast the e.getMessage(), it returns:  Prepare
Failed.:status =0x1.

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....

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

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to