Home > Back-end >  Why use JAVA soundpool load assets way after the load different audio playing the same song audio 2
Why use JAVA soundpool load assets way after the load different audio playing the same song audio 2

Time:10-10

When use the soundpool do more tracks program testing, found a strange phenomenon, is the load assets 2 first audio file is loaded, after playing only hear twice with a audio

Want to seek advice in the BBS have friends know the cause of the problem or have any clues?
Code downloaded from https://blog.csdn.net/aduovip/article/details/14695779, a slight modification,
Thanks for sharing,
Thank you,
 
Import the Java. IO. ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
Import the Java. IO. InputStream;
The import java.net.URL;

The import android. App. The Activity;
The import android. The content. The Context;
The import android. Content. Res. AssetFileDescriptor;
The import android. Media. AudioAttributes;
The import android. Media. AudioManager;
The import android. Media. SoundPool;
The import android. OS. Bundle;
The import android. OS. Handler;
The import android. Util. Log;
The import android. View. The view;
The import android. Widget. ToggleButton;

Public class MainActivity extends the Activity implements SoundPool. onl oadCompleteListener {
Private static final ints SRC_QUALITY=0;
Private static final ints PRIORITY=1;
Private SoundPool SoundPool=null;
Private AudioManager aMgr;

Private int sid_background;
Private int sid_roar;
Private int sid_bark;
Private int sid_chimp;
Private int sid_rooster;

Private int BG_bac;
Private int BG_Str_bac;
Private float BG_bac_Vol;
Private int BG_bac_Fre;
Private int BG_bac_Rate;
Private int BG_talk;
Private int BG_Str_talk;
Private int BG_eff;
Private Boolean isLoaded;


@ Override
Public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@ Override
Protected void onResume () {

//soundPool=new soundPool (4, AudioManager. STREAM_MUSIC SRC_QUALITY);
SoundPool. Builder.spb=new SoundPool. Builder ();
.spb. SetMaxStreams (4);
AudioAttributes. Builder attrBuilder=new AudioAttributes. Builder ();
AttrBuilder. SetLegacyStreamType (AudioManager. STREAM_MUSIC);//set the appropriate properties of the audio stream
.spb. SetAudioAttributes (attrBuilder. The build ());//convert audio format
Final SoundPool SoundPool=.spb. The build ();//create SoundPool object
//soundPool setOnLoadCompleteListener (this);

//the way to read the first 2 different MP3 files, the play will be playing the same song audio 2 times,
Try {
AssetFileDescriptor afd=this. GetAssets (). OpenFd (" res/sound/fight. Mp3 ");//barking

BG_eff=soundPool. Load (afd) getFileDescriptor (), 0, afd. GetLength (), PRIORITY);
Afd. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
Try {
AssetFileDescriptor afd2=this. GetAssets (). OpenFd (" res/sound/enter. Mp3 ");//barking

BG_talk=soundPool. Load (afd2 getFileDescriptor (), 0, afd2. GetLength (), PRIORITY);
Afd2. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
//this way after reading can normal play different audio files at the same time
//BG_eff=soundPool. Load (this, R.r aw. Enter, PRIORITY);
//BG_talk=soundPool. Load (this, R.r aw. Fight, PRIORITY);
SoundPool. SetOnLoadCompleteListener (new soundPool. onl oadCompleteListener () {
@ Override
Public void onl oadComplete (SoundPool sound, int sampleId, int status) {
IsLoaded=true;
if(status !=0)
return;
//the output play back the STREAMID
If (sampleId==BG_eff) {
BG_Str_bac=soundPool. Play (BG_eff, 1, 1, PRIORITY, 5, 1.0 f);
The e (" JAVA ", "load audio file success: str_ID:" + + ", return to the ID: "+ BG_eff BG_Str_bac +", return status: "+ status);
{} else if (sampleId==BG_talk)
BG_Str_talk=soundPool. Play (BG_talk, 1, 1, PRIORITY, 5, 1.0 f);

The e (" JAVA ", "load audio file success: str_ID:" + + ", return to the ID: "+ BG_talk BG_Str_talk +", return status: "+ status);
}
}
});
Super. OnResume ();
}
Private byte [] InputStreamToByte (InputStream is) throws IOException {
ByteArrayOutputStream bytestream=new ByteArrayOutputStream ();
Int ch;
While ((ch=is the read ())!=1) {
Bytestream. Write (ch);
}
Byte imgdata []=bytestream. ToByteArray ();
Bytestream. Close ();
Return imgdata;
}

Public void doClick (View View) {
The switch (the getId ()) {
Case R.i db utton:
If (((ToggleButton) view). IsChecked ()) {
SoundPool. AutoResume ();
} else {
SoundPool. AutoPause ();
}
break;
}
}

@ Override
Protected void onPause () {
SoundPool. Release ();
SoundPool=null;
Super. OnPause ();
}


Private void releaseSoundPool () {
If (soundPool!=null) {
SoundPool. AutoPause ();
//soundPool. Unload (mSoundId);
SoundPool. Release ();
SoundPool=null;
}
}

Private void queueSound (final int the sid, final long delay, the final float volume) {
The new Handler (.) postDelayed (new Runnable () {
@ Override
Public void the run () {
If (soundPool==null) return;
If (soundPool. Play (sid, volume, volume, PRIORITY, 0, 1.0 f)==0) {
The e (" soundPool ", "Failed to start sound (" + sid +") ");
}
QueueSound (sid, delay, volume);
}}, delay);
}
}

nullnullnullnull
  • Related