React Native(Android)检测远程视频URL上是否存在声音

时间:2018-11-27 15:59:14

标签: android react-native android-mediaplayer

我想创建一个本地android模块来检测视频中是否存在音轨。目前,我正在使用MediaPlayer,但未调用onPrepared。我想念什么吗?本机模块也正确捆绑。我的日志显示MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: 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";
  }
}

1 个答案:

答案 0 :(得分:0)

解决了它-在这里查看我的图书馆:https://github.com/evanjmg/react-native-sound-detection