android play sound file from assets

Solutions on MaxInterview for android play sound file from assets by the best coders in the world

showing results for - "android play sound file from assets"
Anton
13 Jul 2020
1// MediaPlayer m; /*assume, somewhere in the global scope...*/
2
3public void playBeep() {
4    try {
5        if (m.isPlaying()) {
6            m.stop();
7            m.release();
8            m = new MediaPlayer();
9        }
10
11        AssetFileDescriptor descriptor = getAssets().openFd("beepbeep.mp3");
12        m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
13        descriptor.close();
14
15        m.prepare();
16        m.setVolume(1f, 1f);
17        m.setLooping(true);
18        m.start();
19    } catch (Exception e) {
20        e.printStackTrace();
21    }
22}