How to play an InputStream using Exoplayer?
I have used exoplayer to play audio files and stream over http. But I can't find any interface that implements SampleSource whose constructor has an inputstream as a parameter. Please help me with this.
DataSource is the interface through which ExoPlayer reads media data, so that's the interface that you'd need to find a suitable implementation of. We do not provide an implementation that wraps an InputStream directly because the InputStream interface doesn't provide suitable random access behavior for media playback, which is required both for seeking and because some media formats place data at the end of the file that must be read at the start of playback.
However, where you're able to create InputStream instances at a given offset into the data, or where you know that InputStream.skip will behave in an efficient way for the type of InputStream that you're handling, it is possible to implement a DataSource around InputStream instances. AssetDataSource and ContentDataSource are two examples for reading assets and data from content providers respectively.
Where are your InputStream instances coming from exactly, and where is the media located that you're trying to play? Knowing that would help suggest a suitable approach.
Media is located on a server , I am creating an inputstream on the client side and I was using audiotrack to play the stream, but I want to shift to exoplayer to play this stream. Is there any way?
To put it another way, why are you trying to use an InputStream as opposed to just giving ExoPlayer the URL directly? InputStream is not a suitable interface for full featured media playback.
@ojw28 For some reasons I will have to stick to streaming from an InputStream. I created my own DataSource similar to ContentDataSource where I changed the statement : inputStream = new FileInputStream(assetFd.getFileDescriptor());
to my empty inputStream. But I am getting an exception each time I try to play the track while streaming :
Internal track renderer error. com.google.android.exoplayer.ExoPlaybackException: com.google.android.exoplayer.extractor.ExtractorSampleSource$UnrecognizedInputFormatException: None of the available extractors (WebmExtractor, FragmentedMp4Extractor, Mp4Extractor, Mp3Extractor, AdtsExtractor, TsExtractor, FlvExtractor) could read the stream. at com.google.android.exoplayer.SampleSourceTrackRenderer.maybeThrowError(SampleSourceTrackRenderer.java:154)
Any idea how to fix this?
If you're feeding content from the InputStream directly into the AudioTrack then the data being provided by the InputStream is probably unpackaged and decompressed audio. ExoPlayer handles packaged and compressed media.
It's very unclear what the goals are here. What's wrong with your existing AudioTrack based solution? Given the apparent constraints (unpackaged and decompressed media through an InputStream), there's not really a huge amount that a library like ExoPlayer could do beyond feeding the data into an AudioTrack, which is what you're doing yourself already.
The constraints themselves sounds flawed/bad, although without knowing the exact use case it's hard to say. I'd suggest revisiting those, and if they really are sensible, sticking with an AudioTrack based solution.
@ojw28 since there was not datasource available that takes a raw inputstream as a source, I created this project. Here I have created my own custom datasource (similar to AssetDataSource). I have feeded myInputStream (my custom build InputStream) instead of
"assetManager.open(path, AssetManager.ACCESS_RANDOM);" as in assetDataSource.
My project plays a song named "song.mp3" present in the root directory of SD Card. It creates an inputStream from the file and simulating a read speed of 20 KiloBytes Per Second, thus creating a buffer speed of 20 KBPS which can be changed by manipulating "BUFFER_BYTES". Please review my code :+1: https://github.com/amanmj/InputStream_Player