Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is really insane!!! I've looked everywhere on the internet and I can not find an answer to a very simple feature of most media players. I am creating an app that will loop music files in order (a loop all button), but there doesn't seem to be any code out there on the net eventhough every media player i know has this feature (along with random select too). Can someone pose code for looping all the songs in a file. My files are in the raw folder of my app so it must come from there, not the sd card! Any help would be appreciated!!
Posted
Comments
Maciej Los 15-Jul-14 13:30pm    
What have you tried Where are you stuck?
Member 10948350 15-Jul-14 13:34pm    
I've tried this, but the problem is it works fine on the initial loop, but after that it mixes the tracks up or skip tracks...Weird, huh???

Here is the code:

public class OneExp extends ActionBarActivity {

private ListView lv;
private ArrayAdapter<string> arrayAdapter;
private MediaPlayer mp;
private MediaPlayer mp2;
int[] myAudio = {R.raw.e_1101, R.raw.e_1102, R.raw.e_1103, R.raw.e_1104, R.raw.e_1105,
R.raw.e_1106, R.raw.e_1107, R.raw.e_1108, R.raw.e_1109, R.raw.e_1110, R.raw.e_1111};
int mCompleted = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_exp);

Button btnLoop = (Button) findViewById(R.id.button1);
Button btnStop = (Button) findViewById(R.id.button2);

btnStop.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp2 != null) {

if (mp2.isPlaying()) {
mp2.stop();

}
mp2.reset();
mp2.release();
mp2 = null;
}
}
});

btnLoop.setOnClickListener(new OnClickListener() {



@Override
public void onClick(View arg0) {

mp2 = MediaPlayer.create(getBaseContext(), myAudio[0]);
mp2.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp2)
{
mCompleted++;
mp2.reset();
if (mCompleted < myAudio.length)
{
try
{
AssetFileDescriptor afd = getResources().openRawResourceFd(myAudio[mCompleted]);
if (afd != null)
{
mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp2.prepare();
mp2.start();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}

}
else if (mCompleted == myAudio.length)
{
mCompleted =0;
try
{
AssetFileDescriptor afd = getResources().openRawResourceFd(myAudio[mCompleted]);
if (afd != null)
{
mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp2.prepare();
mp2.start();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else
{
mCompleted=0;
mp2.release();
mp2 = null;
}

}
});


mp2.start();

}

});
Maciej Los 15-Jul-14 13:37pm    
Rather then postin code in comment, please update yor question. Use "Improve question" widget!
Member 10948350 15-Jul-14 14:00pm    
Sorry, your solution doesn't help me. I'm not talking about getting a set of files from a directory.

1 solution

If you want to get list of files placed on internal storage, have a look here: http://stackoverflow.com/questions/11871925/how-to-get-list-of-files-from-a-specific-folder-in-internal-storage[^].
If you want to list all files, this would help you: http://android-coding.blogspot.com/2011/10/list-filesdirectory-in-android.html[^]

For further information, please see:
Storage options[^]
Saving files[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900