我想创建一个原生 android 模块来检测视频中是否存在音轨。现在我正在使用 MediaPlayer,但没有调用 onPrepared。我错过了什么吗? native 模块也正确 bundle 。我的日志显示 MediaPlayer:无法在客户端打开文件;尝试服务器端:java.io.FileNotFoundException:没有内容提供商:https://storage.googleapis.com/SPECIAL_FILE.mp4
public class RNSoundDetectionModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
public RNSoundDetectionModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@ReactMethod
public void doesLocalVideoHaveAudioTrack(String path, Callback cb) {
boolean audioTrack =false;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(path);
String hasAudioStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO);
if(hasAudioStr!=null && hasAudioStr.equals("yes")){
audioTrack=true; }
else{
audioTrack=false; }
try {
cb.invoke(audioTrack, null);
} catch (Exception e) {
cb.invoke(e.toString(), null);
}
}
@ReactMethod
public void getRemoteAudioTracks(String path, final Callback cb) {
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(path);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
cb.invoke(mp.getTrackInfo(), null);
}
});
mediaPlayer.prepareAsync();
} catch (IOException e) {
cb.invoke(e.toString(), null);
}
}
@Override
public String getName() {
return "RNSoundDetection";
}
}
最佳答案
已修复 - 请在此处查看我的图书馆:https://github.com/evanjmg/react-native-sound-detection
关于android - React Native(Android)检测远程视频网址上存在声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53503522/