MediaPlayer not Plyaing FileDescriptor problems

All your problems with Audio, Video and Images.

MediaPlayer not Plyaing FileDescriptor problems

Postby Rizza » Fri Nov 20, 2009 5:11 pm

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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. AssetFileDescriptor mgrFileDesc = mgr.openFd("Sounds/" + grp.getChildAt(chkId).getTag().toString());
  3.  
  4.  
  5.  
  6. FileDescriptor fd = mgrFileDesc.getFileDescriptor();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


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

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. java.io.FileDescriptor@44767fc12    
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
with the number/letters after the @chaning each time.

But the MediaPlayer throws an IO Exception, when i attempt to open this fd, if i toast the e.getMessage(), it returns:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  Prepare Failed.:status =0x1.
Parsed in 0.034 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....
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.*;
  3.  
  4. import android.app.Activity;
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.widget.ScrollView;
  9.  
  10. import android.widget.LinearLayout;
  11.  
  12. import android.widget.RadioGroup;
  13.  
  14. import android.widget.Toast;
  15.  
  16. import android.widget.RadioGroup.OnCheckedChangeListener;
  17.  
  18. import android.media.MediaPlayer;
  19.  
  20. import android.widget.RadioButton;
  21.  
  22. import android.widget.TextView;
  23.  
  24.  
  25.  
  26. import android.content.res.*;
  27.  
  28.  
  29.  
  30. public class TWSSConfig extends Activity {
  31.  
  32.  
  33.  
  34.     @Override
  35.  
  36.     public void onCreate(Bundle savedInstanceState) {
  37.  
  38.         super.onCreate(savedInstanceState);
  39.  
  40.  
  41.  
  42.                 AssetManager mgr = getAssets();  
  43.  
  44.  
  45.  
  46.         //Start by creating our dynamic view of the file list
  47.  
  48.         ScrollView scrView = new ScrollView(this);
  49.  
  50.         LinearLayout linLay = new LinearLayout(this);
  51.  
  52.        
  53.  
  54.         //Now define some properties for these layout values
  55.  
  56.        linLay.setOrientation(LinearLayout.VERTICAL);
  57.  
  58.        
  59.  
  60.        //Set the TextView
  61.  
  62.        TextView txtTitleArea = new TextView(this);
  63.  
  64.        txtTitleArea.setTextAppearance(this, android.R.style.TextAppearance_Medium);
  65.  
  66.        txtTitleArea.setText(R.string.greeting);
  67.  
  68.      
  69.  
  70.        linLay.addView(txtTitleArea);    
  71.  
  72.  
  73.  
  74.        //Add a RadioGroup Since for now its only one option
  75.  
  76.        RadioGroup rdGroup = new RadioGroup(this);
  77.  
  78.  
  79.  
  80.        //Now use our file list to create all buttons for this group and set all
  81.  
  82.        try {
  83.  
  84.                 String list[] = mgr.list("Sounds");
  85.  
  86.                 if (list != null)
  87.  
  88.                         for (int i=0; i<list.length; ++i)
  89.  
  90.                         {
  91.  
  92.                         //Add a group button for files
  93.  
  94.                         RadioButton rdBut = new RadioButton(this);
  95.  
  96.                         //Applies an id to the chkBox
  97.  
  98.                         rdBut.setId(i);
  99.  
  100.                         rdBut.setTag(list[i]);
  101.  
  102.                         rdBut.setText(list[i]);
  103.  
  104.                         rdGroup.addView(rdBut);                
  105.  
  106.                         }
  107.  
  108.             } catch (IOException e) {
  109.  
  110.                 Toast.makeText(getBaseContext(), "List error: can't list" + "/assets/Sounds" , Toast.LENGTH_SHORT).show();
  111.  
  112.             }      
  113.  
  114.        
  115.  
  116.       //Add Listener
  117.  
  118.        rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  119.  
  120.            public void onCheckedChanged(RadioGroup grp, int chkId){
  121.  
  122.                    //So now we know which chkBox is checked, so now we want to set that
  123.  
  124.                    //play that file for the user                   
  125.  
  126.                try {
  127.  
  128.                            AssetManager mgr = getAssets();  
  129.  
  130.                    AssetFileDescriptor mgrFileDesc = mgr.openFd("Sounds/" + grp.getChildAt(chkId).getTag().toString());
  131.  
  132.                    FileDescriptor fd = mgrFileDesc.getFileDescriptor();
  133.  
  134.                        MediaPlayer mPlay = new MediaPlayer();                  
  135.  
  136.                        mPlay.setDataSource(fd);
  137.  
  138.                        Toast.makeText(getBaseContext(),fd.toString() , Toast.LENGTH_SHORT).show();                         mPlay.prepare();
  139.  
  140.                            mPlay.start();                              
  141.  
  142.                         } catch (IOException e) {
  143.  
  144.                                         // TODO Auto-generated catch block
  145.  
  146.                                 //      e.printStackTrace();
  147.  
  148.                                         Toast.makeText(getBaseContext(),"ERROR:"+ e.getMessage() , Toast.LENGTH_SHORT).show();
  149.  
  150.                                 }
  151.  
  152.            }//end of onCheckedChanged method               
  153.  
  154.         }//End of OnCheckedChangedListener deceleration
  155.  
  156.        );//end of rdGroup.setOnCheckedChangedListener
  157.  
  158.        
  159.  
  160.        linLay.addView(rdGroup);
  161.  
  162.          
  163.  
  164.        //Add this Linear Layout to scroll view
  165.  
  166.        scrView.addView(linLay);      
  167.  
  168.        this.setContentView(scrView);      
  169.  
  170.     } //end of onCreate
  171.  
  172.        
Parsed in 0.049 seconds, using GeSHi 1.0.8.4
Rizza
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 20, 2009 5:02 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests