play media stored in sdcard

All your problems with Audio, Video and Images.

play media stored in sdcard

Postby griffinshi » Wed Jun 09, 2010 4:43 am

hi guys


i meet some problem while to play music stored in sdcard, and by Uri.


but emulator will crash, plz help me. and i attach src


Code: Select all
public class LoopPlayerTest extends Activity {
   int id = 0;
   boolean isStart = true;
   
   ListView lv;
   Cursor cursor;
   
   MediaPlayer mp;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        cursor = this.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, null);
        addList();
       
        mp = new MediaPlayer();
       
        try {
         playMusic(mp);
      } catch (IllegalStateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
      
      mp.setOnCompletionListener(new OnCompletionListener(){

         @Override
         public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            
         }
         
      });
      
    }
   
    public void addList(){
        CursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2,cursor,
                new String[] {MediaStore.Audio.AudioColumns.TITLE,
                    MediaStore.Audio.AudioColumns.ARTIST},
                new int[] {android.R.id.text1,android.R.id.text2});
       
        lv = (ListView)findViewById(R.id.list);
        lv.setAdapter(adapter);
    }
   
    public void playMusic(MediaPlayer player) throws IllegalStateException, IOException{
       Uri current = Uri.withAppendedPath(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                "/"+String.valueOf(id));
       
       player = MediaPlayer.create(this, current);
       player.prepare();
       player.start();
       
    }
   
}
new to anddev:)
griffinshi
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Feb 26, 2010 8:53 am
Location: cn

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 11 guests