I want to read my sounds from assets, it is a game tat play a sound to guess. now my sounds are in raw folder and now I want to read them from asset. i google it and also I found some code that works correctly but I can just read the first sound.
take a look in my code:
here i read from raw:
int sound_id = mContext.getResources().getIdentifier(SoundFile, "raw",
mContext.getPackageName());
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
soundID = soundPool.load(this, sound_id, 1);
and here i use the code but it just play my first sound
private static void initiate() {
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
InputStream in_s = G.context.getAssets().open("temp.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);
routpic.parseXML(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
Log.i("EROR", "nabod");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("EROR", "nabod");
}
array_audio.clear();
for (int i = 0; i < routpic.names.size(); i++) {
array_audio.add(routpic.names.get(i));
Log.i("LOG2", "" + routpic.names.get(i));
}
}
private void play(int index) {
mediaPlayer.release();
index++;
index=soundID;
String s = "mp3/a" + index + ".mp3";
//Resources resources = getResources();
AssetFileDescriptor afd;
try {
afd = getAssets().openFd(s);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and in my assets :
<mp3>
<name>1</name>
</mp3>
I get the answer I cancel the soundpool and I use from madiapalayer. and with this way I read my musics from asset folder. I put my music in the mp3 folder like here:
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("mp3/" + "a" + lvl + ".mp3");
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepare();
//mediaPlayer.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and after it , I start my mediapalayer easily.
variables
private SoundPool soundPool;
private AssetFileDescriptor afd;
put this code in onCreate your activity or fragments
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setMaxStreams(1)
.setAudioAttributes(audioAttributes)
.build();
} else {
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
}
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(sampleId, 1.0f, 1.0f, 0, 0, 1.0f);
}
});
load and play mp3 file from assets folder
try {
afd = getResources().getAssets().openFd("mp3/ba.mp3");
}
catch (IOException e) {
e.printStackTrace();
}
soundPool.load(afd, 1);
load and play mp3 file from raw directory
//activity
soundPool.load(this, R.raw.ba, 1);
//or fragments
soundPool.load(getActivity(), R.raw.ba, 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.