Problem playing multiple audio files

All your problems with Audio, Video and Images.

Problem playing multiple audio files

Postby TheRaiderNation » Sun Jan 30, 2011 6:25 am

Essentially what I'm trying to do is simply create basic code that will play all .mp3 files in the folder located at "/sdcard/Music/". When I compile my code and install the .apk on my phone (not on the android virtual device) only one song is played. Also main.xml is not shown when the application starts, I get a lot of errors saying that the app isn't responding, going from portrait to landscape gives problems as well as attempting to turn the volume up and down.

I mainly need help with figuring out why it is only playing one of the two audio files. Any possible comment on the other problems would be helpful too.

The path that I'm using looks like this:
sdcard/
----Music/
------Song1.mp3
------Song2.mp3

Here's my source code:
Code: Select all
package com.auto.playlist;

import java.io.*;
import java.util.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.media.*;

public class AutoPlaylist extends Activity {
    /** Called when the activity is first created. */
    @Override
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        String songName = "/sdcard/Music/";
        File fileHandler = new File("/sdcard/Music/");
        String[] dirInFile = fileHandler.list();
       
        MediaPlayer mp = new MediaPlayer();
   
       
        //------Start Media Playlist------
       
        for(int i=0;i<dirInFile.length;i++)
        {
           String temp = songName;
           songName+=dirInFile[i];
           try {
            mp.setDataSource(songName);
         } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         try {
            mp.prepare();
         } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         mp.start();
         int duration = mp.getDuration();
         while(duration>mp.getCurrentPosition())
         {
            if(duration<=mp.getCurrentPosition())
            {
               mp.pause();
               mp.stop();
            }
         }
         songName = temp;

        }
   
   
    }
}


One last thing. Does anyone know how the File class lists files in the list() method? Because it my code should be playing the first song in the array of files (Song1.mp3). But when the application starts it plays Song2.mp3

Any help is much appreciated
TheRaiderNation
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jan 30, 2011 6:22 am

Top

Re: Problem playing multiple audio files

Postby strider2023 » Thu Mar 03, 2011 10:21 pm

Hi,

You can try this tutorial it is very well written and I think it will answer all your doubts. http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 17 guests