Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Error, Ads not working. #8152

Closed
MRAndroidZahid opened this issue Nov 3, 2020 · 13 comments
Closed

Getting Error, Ads not working. #8152

MRAndroidZahid opened this issue Nov 3, 2020 · 13 comments
Assignees
Labels

Comments

@MRAndroidZahid
Copy link

Java Code

MediaSourceFactory mediaSourceFactory =
new DefaultMediaSourceFactory(this)
.setAdsLoaderProvider(this::getAdsLoader)
.setAdViewProvider(playerView);

// Create a SimpleExoPlayer and set is as the player for content and ads.
player = new SimpleExoPlayer.Builder(this).setMediaSourceFactory(mediaSourceFactory).build();
playerView.setPlayer(player);
//adsLoader.setPlayer(player);

// DataSource.Factory dataSourceFactory =
// new DefaultDataSourceFactory(this, Util.getUserAgent(this,
// getString(R.string.app_name)));

// ProgressiveMediaSource.Factory mediaSourceFactory =
// new ProgressiveMediaSource.Factory(dataSourceFactory);

// Create the MediaSource for the content you wish to play.
// MediaSource mediaSource =
// mediaSourceFactory.createMediaSource(Uri.parse(getString(R.string.content_url)));

// Create the AdsMediaSource using the AdsLoader and the MediaSource.
//AdsMediaSource adsMediaSource =
// new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView);

// Prepare the content and ad to be played with the SimpleExoPlayer.
// player.prepare(adsMediaSource);

// Set PlayWhenReady. If true, content and ads autoplay.
player.setPlayWhenReady(true);

// A pre-roll ad.
MediaItem preRollAd = MediaItem.fromUri("https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpostpodbumper&cmsid=496&vid=short_onecue&correlator=");
// The start of the content.
MediaItem contentStart =
new MediaItem.Builder()
.setUri(getString(R.string.content_url))
.setClipEndPositionMs(120_000)
.build();
// A mid-roll ad.
MediaItem midRollAd = MediaItem.fromUri("https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpostpodbumper&cmsid=496&vid=short_onecue&correlator=");
// The rest of the content
MediaItem contentEnd =
new MediaItem.Builder()
.setUri(getString(R.string.content_url))
.setClipStartPositionMs(120_000)
.build();

// Build the playlist.
player.addMediaItem(preRollAd);
player.addMediaItem(contentStart);
player.addMediaItem(midRollAd);
player.addMediaItem(contentEnd);

// Prepare the content and ad to be played with the SimpleExoPlayer.
player.prepare();
player.play();

player.addListener(new Player.EventListener() {
@OverRide
public void onPlayerError(ExoPlaybackException error) {
Log.e("errpr", error.getMessage());
}
});
// Set PlayWhenReady. If true, content and ads autoplay.
// player.setPlayWhenReady(true);

Error log

2020-11-03 16:07:07.300 10637-10682/com.e.medrecexoplayer E/ExoPlayerImplInternal: Playback error
com.google.android.exoplayer2.ExoPlaybackException: Source error
at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:554)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: com.google.android.exoplayer2.source.UnrecognizedInputFormatException: None of the available extractors (FlvExtractor, FlacExtractor, WavExtractor, FragmentedMp4Extractor, Mp4Extractor, AmrExtractor, PsExtractor, OggExtractor, TsExtractor, MatroskaExtractor, AdtsExtractor, Ac3Extractor, Ac4Extractor, Mp3Extractor) could read the stream.
at com.google.android.exoplayer2.source.BundledExtractorsAdapter.init(BundledExtractorsAdapter.java:92)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1024)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:415)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
2020-11-03 16:07:07.302 10637-10637/com.e.medrecexoplayer E/errpr: Source error

Build Gradle
implementation 'com.google.android.exoplayer:exoplayer:2.12.0'
implementation 'com.google.android.exoplayer:exoplayer-core:2.12.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.12.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.12.0'
implementation 'com.google.android.exoplayer:extension-ima:2.12.0'

@andrewlewis
Copy link
Collaborator

You are trying to pass an ad tag URI as if it was a MediaItem, and this causes an error because the XML ad tag format is not recognized as a valid media container (like MP4).

I think you can fix this by using the IMA extension. The relevant code snippets in https://exoplayer.dev/ad-insertion.html#declarative-ad-support are:

MediaItem mediaItem =
    new MediaItem.Builder().setUri(videoUri).setAdTagUri(adTagUri).build();

... where you can pass your ad tag URI, and:

MediaSourceFactory mediaSourceFactory =
    new DefaultMediaSourceFactory(context)
        .setAdsLoaderProvider(adsLoaderProvider)
        .setAdViewProvider(playerView);
SimpleExoPlayer player = new SimpleExoPlayer.Builder(context)
    .setMediaSourceFactory(mediaSourceFactory)
    .build();

For this one your adsLoaderProvider should return an ImaAdsLoader instance. See the demo app PlayerActivity for sample code for this.

Side note: I think the confusion comes from the fact that the ad insertion documentation describes how to do ad insertion yourself (here) without using the IMA extension, but for that part of the instructions you'd need to get the actual ad media URLs, rather than just the ad tag URI from which they can be loaded. So I'm suggesting above you just use the IMA extension as it takes care of this for you.

@MRAndroidZahid
Copy link
Author

MRAndroidZahid commented Nov 3, 2020 via email

@andrewlewis
Copy link
Collaborator

IMA extension dependencies so it can’t be import module lonely. How I can run. ImaAdsLoader should come from extension but its taking from library.

I'm afraid I don't understand. Please could you rephrase, or paste the error you are seeing?

When you depend on implementation 'com.google.android.exoplayer:extension-ima:2.12.0' that should also bring in the IMA SDK at the correct version (you don't need to add a dependency on the IMA SDK yourself).

@MRAndroidZahid
Copy link
Author

MRAndroidZahid commented Nov 3, 2020 via email

@andrewlewis
Copy link
Collaborator

My first comment explains the UnrecognizedInputFormatException. You need to remove the code that passes the ad tag URI and use the code I provided there instead.

IMA extension dependencies so it can’t be import module lonely. How I can run. ImaAdsLoader should come from extension but its taking from library.

I don't understand what this means. Please could you try rephrasing it?

@MRAndroidZahid
Copy link
Author

MRAndroidZahid commented Nov 3, 2020 via email

@andrewlewis
Copy link
Collaborator

andrewlewis commented Nov 3, 2020

How I can control the ad numbers as I am getting sometimes 2 and sometimes 3 ads in a sequence.
How I can control the position of the ads (On start, in mid of video or in last of video)

These are controlled by your ad tag, so please configure the behavior you need on your ads server.

When I am trying to add another media item then I am getting below crash.

Playing ads with ImaAdsLoader in playlists is not currently supported, but will be supported soon. Please follow #3750 for updates.

@MRAndroidZahid
Copy link
Author

MRAndroidZahid commented Nov 3, 2020 via email

@andrewlewis
Copy link
Collaborator

You can call construct the ImaAdsLoader and call imaAdsLoader.requestAds to request the ad tag before preparing the player (mentioned in #3636).

@MRAndroidZahid
Copy link
Author

MRAndroidZahid commented Nov 4, 2020 via email

@MRAndroidZahid
Copy link
Author

MRAndroidZahid commented Nov 5, 2020 via email

@andrewlewis
Copy link
Collaborator

I am waiting for revert as I need to release build. Please have a look on java code and let me know what’s he issue. I need to prefetch the video ad and when user click on button, It should start instantly.

Can you create the player before the user clicks the button but call player.pause() so that playback doesn't begin? That will allow the ad media to buffer. In this setup you need to be careful not to overlap the lifetime of multiple players, as you may see errors on some devices if you acquire more than one hardware video decoder at a time. When the user clicks the button call player.play() and playback should start instantly (assuming the ad tag has loaded and the ad media has buffered).

@MRAndroidZahid
Copy link
Author

MRAndroidZahid commented Nov 6, 2020 via email

@google google locked and limited conversation to collaborators Jan 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants