0

I'm writing an application where I play a stream provided trough a URI. I would like to show the SurfaceView to render the video only if the stream is actually a video ... is it possibile?

The code I'm using initialize MediaPlayer instance in this way:

private void initMediaPlayer(@NonNull final Context context) {
    media_player = new MediaPlayer();
    media_player.setAudioStreamType(AudioManager.STREAM_MUSIC);

    media_player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.d(getClass().getName(), "onPreparedListener");

            //TODO: identify video/audio

            enablePlay();
        }
    });

    media_player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Log.d(getClass().getName(), "onCompletionListener");
            Toast.makeText(context, getString(R.string.streaming_completed), Toast.LENGTH_LONG).show();
        }
    });

    media_player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
            error = true;
            Log.e(getClass().getName(), String.format("MediaPlayer error: %d, %d", i, i1));
            return false;
        }
    });
}

I tought I can investigate the kind of stream inside setOnPreparedListener handler but as long as I can see there's no way to verify what the stream actually is.

The streaming is started trough this method.

private void initStreaming(@NonNull final String uri) {
    new Thread(
            new Runnable() {
                @Override
                public void run() {
                    try {
                        media_player.setDataSource(uri);
                        media_player.prepareAsync();
                    } catch (Throwable e) {
                        Log.e(getClass().getName(), "Exception: " + e.getMessage());
                    }
                }
            }
    ).start();
}

===== UPDATE =====

The uri specify a network stream not a local file therefore solution based on mime types or filename/extension will not work.

weirdgyn
  • 886
  • 16
  • 47
  • String extension = MimeTypeMap.getFileExtensionFromUrl("url"); ? – Wale Nov 10 '20 at 16:41
  • Does this answer your question? [How to determine MIME type of file in android?](https://stackoverflow.com/questions/8589645/how-to-determine-mime-type-of-file-in-android) – Wale Nov 10 '20 at 16:42
  • @Wale thnx but (as I specified above) the stream referenced by uri is a network stream not a local file (my fault)... – weirdgyn Nov 10 '20 at 17:38

0 Answers0