You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中JNI库结合NanoHttpd无法正常工作问题排查

调用JNI方法后,NanoHttpd给MediaPlayer的音频流服务失效了

我在原生Java代码里用NanoHttpd给MediaPlayer做音频流服务,原本一切正常,但调用了JNI库的几个方法之后,这个功能就突然失效了,实在找不到问题出在哪,来求助一下!

先给大家看看相关的代码和信息:

JNI方法定义

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

出现的错误信息

  • 核心错误提示:No Content Provider: http://localhost:8080
  • 还有一条相关错误:memtrack: Couldn't load memtrack module (No such file or directory)

我感觉问题应该出在那个No Content Provider的错误上,但就是搞不懂为什么调用JNI方法后会出现这个情况,有没有懂的朋友帮忙分析一下?

内容的提问来源于stack exchange,提问作者farbod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:23:57