React-Native не может правильно использовать библиотеку jni - PullRequest
0 голосов
/ 16 мая 2018

Я использую nanohttpd в своем родном коде Java. Когда я использую его обычно, все выглядит хорошо, но когда я использую методы библиотеки jni, это не работает.

мое приложение использует nanohttpd для создания потока для mediaPlayer.

нативные методы:

public  native  String LH();
public  native  int P();
public  native  String EngineGS(Context context);
public  native  byte[] OGB(byte[] inputBuff);

переменные:

private MediaPlayer mp;
private HTTPServer encryptServer;

класс nanohttpd:

public class HTTPServer extends NanoHTTPD {

    public HTTPServer(int port) throws IOException {
        super(port);
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
    }

    @Override
    public Response serve(IHTTPSession session) {
        Response response = null;
        try {
            InputStream inputStream = new FileInputStream("/sdcard/Download/" + "encrypted.mp3");
            byte[] encryptedInputByteArray = IOUtils.toByteArray(inputStream);
            byte[] decryptedByteArray = OGB(encryptedInputByteArray);
            inputStream = new ByteArrayInputStream(decryptedByteArray);

            int totalLength = inputStream.available();

            String requestRange = session.getHeaders().get("range");
            if (requestRange == null) {
                response = NanoHTTPD.newFixedLengthResponse(Response.Status.OK, "audio/mpeg", inputStream, totalLength);
            } else {

                Matcher matcher = Pattern.compile("bytes=(\\d+)-(\\d*)").matcher(requestRange);
                matcher.find();
                long start = 0;
                try {
                    start = Long.parseLong(matcher.group(1));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                inputStream.skip(start);

                long restLength = totalLength - start;
                response = NanoHTTPD.newFixedLengthResponse(Response.Status.PARTIAL_CONTENT, "audio/mpeg", inputStream, restLength);

                String contentRange = String.format("bytes %d-%d/%d", start, totalLength, totalLength);
                response.addHeader("Content-Range", contentRange);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}

метод воспроизведения:

@ReactMethod
public void play() {
    mp.getCurrentPosition();
    try {
        if (encryptServer == null) {
            encryptServer = new HTTPServer(P());
        }
        Uri uri = Uri.parse(LH() + ":" + encryptServer.getListeningPort());

        mp.reset();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setDataSource(getReactApplicationContext(), uri);
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Я не знаю, где проблема.

Ошибка:

localhost

memtrack

Я думаю, что проблема возникает отсюда:

Нет провайдера контента: http://localhost:8080

...