1

I'm using nanohttpd in my native java code. When I use it normally everything looks good, but when I use jni library methods it does not work.

my app uses nanohttpd to make stream for mediaPlayer.

native methods:

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

variables :

private MediaPlayer mp;
private HTTPServer encryptServer;

nanohttpd class:

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

play method:

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

I do not know where the problem is.

Errors:

localhost

memtrack

I think the problem comes from here:

No Content Provider: http://localhost:8080

farbod
  • 91
  • 7
  • React-Native depends on a jni library itself. So, when you add your custom jni library, maybe the libreactnativejni.so cannot load – Alex Cohn May 16 '18 at 22:45
  • @AlexCohn React-Native document says can use jni libraries inside app. – farbod May 17 '18 at 07:48
  • yes you can, but there are some caveats. E.g. you should use the same version of NDK and same STL as the ones used for **libreactnativejni.so** – Alex Cohn May 17 '18 at 10:35
  • interesting.... let me talk with the one who writes this c++ library. thank you. – farbod May 17 '18 at 21:04

0 Answers0