P
P
potatos07772019-11-29 13:29:54
JavaScript
potatos0777, 2019-11-29 13:29:54

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();
            }
        }
    }

And its call via Javascript
playSound("play_1","blabla.mp3");
It is necessary not only to lose, but also to be able to lose several, and even in parallel, besides being able to control (start, stop, delete).
That is, it is probably necessary to create an object with a name from String name
playAudio(String name, String src)
here
final MediaPlayer mp;
and further.
Or maybe MediaPlayer has some built-in methods for this.
Help super-programmers, a.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question