B
B
Boris Onofrey2015-02-19 05:13:29
Java
Boris Onofrey, 2015-02-19 05:13:29

How to play mp3 in java using NativeBass from InputStream?

I downloaded the NativeBass library, connected it, played the file, specifying the path to it. I wanted to cache audio from the network (as in the VK mobile application).
That's what came out of it:

import jouvieje.bass.Bass;
import jouvieje.bass.BassInit;
import jouvieje.bass.enumerations.STREAMPROC_SPECIAL;
import jouvieje.bass.exceptions.BassException;
import jouvieje.bass.structures.HSTREAM;
import jouvieje.bass.utils.BufferUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class tmp {

    public static void main(String[] args) throws IOException, InterruptedException {
        final String nativesPath = "NativeBass-1.1.2/lib/win64/";

        System.setProperty("org.lwjgl.librarypath", nativesPath);
        System.setProperty("java.library.path", nativesPath);

        BassInit.loadLibraries();

        if ((Bass.BASS_GetVersion() & -65536) >> 16 != BassInit.BASSVERSION())
            throw new BassException("An incorrect version of BASS.DLL was loaded");
        else if (!Bass.BASS_Init(-1, '걄', 0, null, null))
            throw new BassException("Can\'t initialize device");

        System.out.println("BASS initialized");

        HSTREAM hstream = Bass.BASS_StreamCreate('걄', 2, 0, STREAMPROC_SPECIAL.STREAMPROC_PUSH, null);
        System.out.println(hstream);

        Bass.BASS_ChannelPlay(hstream.asInt(), false);

        InputStream inputStream = new FileInputStream("media/341560479.mp3");
        int length;
        byte[] buf = new byte[4096];

        while ((length = inputStream.read(buf)) != -1) {
            ByteBuffer byteBuffer = BufferUtils.newByteBuffer(length);
            byteBuffer.put(buf);
            Bass.BASS_StreamPutData(hstream, byteBuffer, length);
        }
    }
}

It beeps on startup and crashes with this error
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffa78081a57, pid=3384, tid=7316
#
# JRE version: Java(TM) SE Runtime Environment (8.0_25-b18) (build 1.8.0_25-b18)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [msvcrt.dll+0x1a57]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\Clouds\Dropbox\Projects\IdeaProjects\Bass\hs_err_pid3384.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

So how do you reproduce data from a stream?

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