2 Replies - 3233 Views - Last Post: 31 January 2011 - 05:13 PM

#1 TheRaiderNation  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 81
  • Joined: 11-January 10

Android Media Player music playback problem

Posted 29 January 2011 - 12:41 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:
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

This post has been edited by TheRaiderNation: 29 January 2011 - 12:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Android Media Player music playback problem

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Android Media Player music playback problem

Posted 30 January 2011 - 11:36 AM

Ok well my suggestion would be to first search the music folder like so using my code snippet found here(FileSearcher):
// global vars up here
List<File> songs;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        listSongs();
        MediaPlayer mp = new MediaPlayer();
        for(File f:songs){
        try {
				mp.setDataSource(f.getAbsolutePath());
			} 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();
				}
			}

        }
}
private void listSongs()
{
 songs = FileSearcher.searchFromDirectory(".mp3",new File("/sdcard/Music/"));
}



Was This Post Helpful? 0
  • +
  • -

#3 TheRaiderNation  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 81
  • Joined: 11-January 10

Re: Android Media Player music playback problem

Posted 31 January 2011 - 05:13 PM

Thanks for the reply, but I'm no longer having problems locating the files, I'm having trouble getting my app to play both of them. What happens is that only one plays and the second does not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1