Answer the question
In order to leave comments, you need to log in
Webview using multiple MediaPlayer objects via @javascript interface in parallel?
Hello :)
There is a small interface between Webview and AndroidSDK
public class AudioInterface {
Context mContext;
AudioInterface(Context c) {
mContext = c;
}
//Play an audio file from the webpage
@JavascriptInterface
public void playAudio(String name, String src) { //String aud - file name passed
//from the JavaScript function
final MediaPlayer mp;
try {
AssetFileDescriptor fileDescriptor = mContext.getAssets().openFd(src);
mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),fileDescriptor.getLength());
fileDescriptor.close();
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
} 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();
}
}
}
playAudio(String name, String src)
final MediaPlayer mp;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question