I have created a simple activity for playing a list of music stored in the raw folder but I wanted to create another activities for example, MainActivity
– songs from 2008, Other Activity 1 – songs from 2009, Other Activity 2 – songs from 2010, etc. So How do I only read certain files from the raw folder Or is it possible to create sub-directories or read from other new folders?
Here are the codes I have done so far:
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView musicListView;
ArrayList<String> arrayList;
ArrayAdapter musicAdapter;
MediaPlayer musicplayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
musicListView = findViewById(R.id.musicListView);
arrayList = new ArrayList<String>();
Field[] fields = R.raw.class.getFields();
for (int i= i=0; i<fields.length; i++) {
arrayList.add(fields[i].getName());
}
musicAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
musicListView.setAdapter(musicAdapter);
musicListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (musicplayer != null) {
musicplayer.release();
}
int resId = getResources().getIdentifier(arrayList.get(i), "raw", getPackageName());
musicplayer = MediaPlayer.create(MainActivity.this, resId);
musicplayer.start();
}
});
}
}
2
Answers
Question A: Fix:
When you create All Audio Files in /resources folder or raw folder(similar), create audio files with name 2008 and append by some string and current timestamp. So, all files for 2008 year will have the string "2008" in /resources folder. So, to retrieve parse the String and retrieve audio files which contain the string "2008", Similarly for 2009 and 2010 years.
Reading files from Raw folder: Fix: using resources.getBundle() in java or create file with properties class in default directory which is src/resources folder and using library InputStream or FileInputStream.
Is it possible to create folders and sub folders in /resources folder? Yes its possible with Java’s mkdir. But keep in mind to create folder names with string like "2008" dynamically appended by current timestamp. and while retrieval, retrieve all files in respective folders which has names like "2008|, "2009" etc. If Filenames become too recursive and tedious, may be some algorithm that has random string can be used and with fixed length. ofcourse, we can put all permutations and combinations of strings to best use to keep number of strings(filenames)(reusable permutation of filenames) to minimum.
Hope i answered your question.
There are few steps to complete the task:
Model
ClassNow to read the specific files from the raw folder call the function as
here I suppose that your raw folder has songs with the names like songFrom2008_1,songFrom2008_2 …, songFrom2010_1 … and so on the list.
To call the list of songs from 2010 use,
Inside your
musicListView.setOnItemClickListener
, you can use:Hope it helps!