I want to play Video with ExoPlayer, but unfortunately I cannot find any solution.Please help me I've been scratching my head for two days but can't find anything.
This is my code when preparing and setting exoPlayer properties.
String path = "asset:///splash_video.mp4";
Uri uri = Uri.parse(path);
player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
DataSource.Factory factory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, getPackageName()));
simpleExoPlayerView.setPlayer(player);
ExtractorMediaSource extractorMediaSource = new ExtractorMediaSource(uri, factory, new DefaultExtractorsFactory(), new Handler(), null);
player.prepare(extractorMediaSource);
player.setPlayWhenReady(true);
Also I've been following this link from youtube.
https://www.youtube.com/watch?v=vwImlEfitQU
Here is my code is working for audio files,working fine and tested ok:
You just pass file name which should be like 001 is the file in assets\001.mp3
you should call the function like this:
prepareExoPlayerFromAssetResourceFile(001);
the code is:
private void prepareExoPlayerFromAssetResourceFile(int current_file) {
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector((TrackSelection.Factory) null), new DefaultLoadControl());
exoPlayer.addListener(eventListener);
//DataSpec dataSpec = new DataSpec(Uri.parse("asset:///001.mp3"));
DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + (current_file))+".mp3");
final AssetDataSource assetDataSource = new AssetDataSource(this);
try {
assetDataSource.open(dataSpec);
} catch (AssetDataSource.AssetDataSourceException e) {
e.printStackTrace();
}
DataSource.Factory factory = new DataSource.Factory() {
#Override
public DataSource createDataSource() {
//return rawResourceDataSource;
return assetDataSource;
}
};
MediaSource audioSource = new ExtractorMediaSource(assetDataSource.getUri(),
factory, new DefaultExtractorsFactory(), null, null);
/*
MediaSource audioSource1 = new ExtractorMediaSource(
Uri.parse("asset:///" + getfileName(current_file)), // file audio ada di folder assets
new DefaultDataSourceFactory(this, userAgent),
new DefaultExtractorsFactory (),
null,
null
);
*/
exoPlayer.prepare(audioSource);
}
You may also need to set appt compress config in build.gradle(app).
android {
...
aaptOptions{
noCompress "mp4" // file extesion
}
}
You should use a DefaultDataSourceFactory instead of a DefaultHttpDataSourcFactory:
new ExtractorMediaSource(
Uri.parse("asset://splash_video.mp4"),
new DefaultDataSourceFactory(context, userAgent),
new DefaultExtractorsFactory(),
null,
null);
Create a folder "raw" inside your application's "res" folder and copy the file to the newly created folder and replace,
ExtractorMediaSource audioSource = new ExtractorMediaSource(
Uri.parse("asset:///" + current_file), // file audio ada di folder assets
new DefaultDataSourceFactory(this, userAgent),
new DefaultExtractorsFactory(),
null,
null
);
this code with,
Uri uri = Uri.parse("android.resource://Your Package Name/" + R.raw.welcome_video);
ExtractorMediaSource audioSource = new ExtractorMediaSource(
Uri.parse("uri ), // file audio ada di folder assets
new DefaultDataSourceFactory(this, userAgent),
new DefaultExtractorsFactory(),
null,
null
);
Related
[I provided image link please see image i got so many errors1 private void initializeExoplayerView(String videoUrl) {
try{
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(this).build();
TrackSelector trackSelector = new DefaultTrackSelector(this,newAdaptiveTrackSelection.Factory(bandwidthMeter));
exoPlayer = (SimpleExoPlayer) ExoPlayerFactory.newSimpleInstance(this, trackSelector);
Uri videouri = Uri.parse(videoUrl);
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videouri, dataSourceFactory, extractorsFactory, null, null);
exoPlayerView.setPlayer(exoPlayer);
exoPlayer.prepare(mediaSource);
exoPlayer.setPlayWhenReady(true);
} catch (Exception e){
Log.e("TAG", "Error : " + e.toString());
}
}
You should use the new Factory for HTTP Data source instead 'DefaultHttpDataSource.Factory'.
https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.Factory.html
You can find examples of it's use in the ExoPlayer GitHub repository in the test and demo code - for example, if you look at the extract below and follow the calls its makes:
public void start_playlistCanBlockReloadLowLatencyFullSegment_correctMsnAndPartParams()
throws IOException, TimeoutException, InterruptedException {
List<HttpUrl> httpUrls =
enqueueWebServerResponses(
new String[] {
"/master.m3u8",
"/media0/playlist.m3u8",
"/media0/playlist.m3u8?_HLS_msn=14&_HLS_part=0"
},
getMockResponse(SAMPLE_M3U8_LIVE_MASTER),
getMockResponse(SAMPLE_M3U8_LIVE_MEDIA_CAN_BLOCK_RELOAD_LOW_LATENCY_FULL_SEGMENT),
getMockResponse(SAMPLE_M3U8_LIVE_MEDIA_CAN_BLOCK_RELOAD_LOW_LATENCY_FULL_SEGMENT_NEXT));
List<HlsMediaPlaylist> mediaPlaylists =
runPlaylistTrackerAndCollectMediaPlaylists(
new DefaultHttpDataSource.Factory(),
Uri.parse(mockWebServer.url("/master.m3u8").toString()),
/* awaitedMediaPlaylistCount= */ 2);
from: https://github.com/google/ExoPlayer/blob/0ba317b1337eaa789f05dd6c5241246478a3d1e5/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/playlist/DefaultHlsPlaylistTrackerTest.java
I am using ExoPlayer to play videos from URL. For some reason, my video keeps on buffering even it is low size video also. Please find the code snippet below which I have used to build ExoPlayer.
private MediaSource buildMediaStore(String mediaUrl) {
Uri videoUri = Uri.parse(mediaUrl);
DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, getApplicationInfo().packageName), defaultBandwidthMeter);
DefaultExtractorsFactory extractorFactory = new DefaultExtractorsFactory();
return new ExtractorMediaSource(videoUri, dataSourceFactory, extractorFactory, null, null);
}
defaultBandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory(defaultBandwidthMeter);
exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(ViewVideoNewPotriateFullScreen.this),
new DefaultTrackSelector(trackSelectionFactory), new DefaultLoadControl());
MediaSource datasource = buildMediaStore(url);
exoPlayer.prepare(datasource, true, true);
simpleExoPlayer.setPlayer(exoPlayer);
simpleExoPlayer.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
exoPlayer.setRepeatMode(Player.REPEAT_MODE_OFF);
exoPlayer.setPlayWhenReady(true);
exoPlayer.seekTo(getIntent().getLongExtra("position", 0));
//updateTimers();
exoPlayer.addListener(this);
I'm trying to play DASH video with the help of Uri obtained from exoplayer demo app https://github.com/google/ExoPlayer/blob/release-v2/demos/main/src/main/assets/media.exolist.json
When I change my network from WIFI to mobile data , video quality doesn't change instead it starts buffering.
My code
private void initializePlayer() {
if (player == null) {
player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(this),
new DefaultTrackSelector(), new DefaultLoadControl());
exoPlayerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
}
Uri uri = Uri.parse("http://www.youtube.com/api/manifest/dash/id/3aa39fa2cc27967f/source/youtube?as=fmp4_audio_clear,fmp4_sd_hd_clear&sparams=ip,ipbits,expire,source,id,as&ip=0.0.0.0&ipbits=0&expire=19000000000&signature=A2716F75795F5D2AF0E88962FFCD10DB79384F29.84308FF04844498CE6FBCE4731507882B8307798&key=ik0");
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
player.addListener(new PlayerEventListener());
player.setRepeatMode(Player.REPEAT_MODE_ALL);
//exoPlayerView.setUseController(false);
}
private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory manifestDataSourceFactory = new DefaultHttpDataSourceFactory("ua");
DashChunkSource.Factory dashChunkSourceFactory = new DefaultDashChunkSource.Factory(new DefaultHttpDataSourceFactory("ua", BANDWIDTH_METER));
return new DashMediaSource.Factory(dashChunkSourceFactory, manifestDataSourceFactory).createMediaSource(uri);
}
gradle
compile 'com.google.android.exoplayer:exoplayer-core:2.7.0'
compile 'com.google.android.exoplayer:exoplayer-dash:2.7.0'
compile 'com.google.android.exoplayer:exoplayer-ui:2.7.0'
Is there Anything I missed ?
It was a silly mistake
instead of new DefaultTrackSelector()
I used
TrackSelection.Factory adaptiveTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
new DefaultTrackSelector(adaptiveTrackSelectionFactory)
if i use the first one from the following codes,
the 1.m3u8 type songs are running.
if i use the second one, mp3 and the other songs are running.
Is there one code series that can run these formats in one? Maybe it is a simple coding but i couldn t do that. Please help me.
#Override
protected String doInBackground(String... a) {
String s = Constant.arrayList_play.get(Constant.playPos).getMp3Url().replace(" ","%20");
try {
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory((MainActivity)Constant.context,
Util.getUserAgent((MainActivity)Constant.context, "onlinemp3"), bandwidthMeter);
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource videoSource = new ExtractorMediaSource(Uri.parse(s),
dataSourceFactory, extractorsFactory, null, null);
Constant.exoPlayer.prepare(videoSource);
Constant.exoPlayer.setPlayWhenReady(true);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
May be you need to read here about exoplayer's supported formats.
I'm new on ExoPlayer , i'm currently heading to use it to play Native Udp Stream ( From french digital Television : 1080p 5-10 mbps in Variable bitrate )
I manage to play some udp stream with some tests videos from http://jell.yfish.us/ on different devices .
I have make some different video decoding test with HLS and Udp Streaming with this code for UDP :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
sufaceview = (SurfaceView) findViewById(R.id.surfaceView2);
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl(
new DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE),
15000, 60000, 2500, 6000);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
Uri uri =
Uri.parse
("udp://#239.192.2.2:1234");
final DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "teveolauncher"), bandwidthMeterA);
extractorsFactory = new DefaultExtractorsFactory();
DataSource.Factory udsf = new UdpDataSource.Factory() {
#Override
public DataSource createDataSource() {
return new UdpDataSource(null, 3000, 100000);
}
};
ExtractorsFactory tsExtractorFactory = new ExtractorsFactory() {
#Override
public Extractor[] createExtractors() {
return new TsExtractor[]{new TsExtractor(MODE_SINGLE_PMT,
new TimestampAdjuster(0), new DefaultTsPayloadReaderFactory())};
}
};
MediaSource videoSource = new ExtractorMediaSource
(uri, udsf, tsExtractorFactory, null, null);
player.setVideoSurfaceView(sufaceview);
player.prepare(videoSource);
player.setPlayWhenReady(true);
}
For HLS I just Change the MediaSource and the datasourceFactory :
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "teveolauncher"), bandwidthMeterA);
MediaSource videoSource = new HlsMediaSource
(uri, dataSourceFactory, null, null);
I Know Udpstreaming is not officialy supported by ExoPlayer but the UdpDataSource class seems to work well.
After all the test , i noticed video with a variable biterate like french DTT can't be decoded correctly but with Constatnt biterate video like Jell yfish the decoding process is perfect .
There is some coding improvement to make VBR video decoded correctly ?
Thank you by advance :)
Sorry for my bad english :)
Uri uri = Uri.parse("udp://#239.192.2.2:1234");
I think UDP is not a protocol - it's a transport, (like TCP). You can't use a tcp://host:port/ URL either.
Or does it work?