Http Range request Streaming video from url into exoplayer - performance

I tried a lot to find the solution to my problem. but I can't make it correct. I want to show and download video from URL. Url will be like
https://imgur.com/a/dW7kLCv
As far my research API in python language and it used partial content with range request in the header. I don't know how to use that API and load video into video view/exoplayer.I tried below way and I got this issue.
" com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 500"
Anyone pls give me a way to use that api to show and download video
mPlayerView = findViewById(R.id.videoView);
mPlayerView.requestFocus();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
mPlayerView.setPlayer(player);
player.setPlayWhenReady(shouldAutoPlay);
MediaSource mediaSource = new ExtractorMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(videoPath));
boolean haveStartPosition = currentWindow != C.INDEX_UNSET;
if (haveStartPosition) {
player.seekTo(currentWindow, playbackPosition);
}
player.prepare(mediaSource, !haveStartPosition, false);
player.addListener(new ExoPlayer.EventListener() {
#Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
}
#Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
#Override
public void onLoadingChanged(boolean isLoading) {
}
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_BUFFERING) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.INVISIBLE);
}
}
#Override
public void onRepeatModeChanged(int repeatMode) {
}
#Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
#Override
public void onPlayerError(ExoPlaybackException error) {
}
#Override
public void onPositionDiscontinuity(int reason) {
}
#Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
#Override
public void onSeekProcessed() {
}
});

Related

Player Name is not getting synced in unity multiplayer

Names are not getting synced for players across network.
Here is the code for syncing names -:
[SerializeField] private TMPro.TextMeshPro nameForThePlayer;
private NetworkVariable<FixedString64Bytes> playerName = new NetworkVariable<FixedString64Bytes>();
private void OnEnable()
{
playerName.OnValueChanged += playerNameHandler;
}
private void OnDisable()
{
playerName.OnValueChanged -= playerNameHandler;
}
public override void OnNetworkSpawn()
{
if (!IsOwner) { return; }
if (IsHost)
{
SyncNames(NetworkManager.Singleton.LocalClientId);
}
else
{
RequestSyncNamesServerRpc(NetworkManager.Singleton.LocalClientId);
}
}
[ServerRpc]
public void RequestSyncNamesServerRpc(ulong clientId)
{
SyncNames(clientId);
}
private void SyncNames(ulong _clientId)
{
PlayerData? data = ServerGameNetPortal.GetassignedDataForClient(_clientId);
playerName.Value = data.Value.GetPlayerName;
}
private void playerNameHandler(FixedString64Bytes previousValue, FixedString64Bytes newValue)
{
nameForThePlayer.text = newValue.ToString();
}
Using Update instead of OnNetworkSpawn works but I don't want to do it in update as the names should only be synced during connecting only.
EDIT
public override void OnNetworkSpawn()
{
if (!IsOwner)
{
RequestSyncNamesServerRpc(OwnerClientId);
}
if (IsHost)
{
SyncNames(NetworkManager.Singleton.LocalClientId);
}
}
[ServerRpc(RequireOwnership = false)]
public void RequestSyncNamesServerRpc(ulong clientId)
{
SyncNames(clientId);
}
The above code works fine but in OnNetworkSpawn why are we checking it for other clients In the !isOwner block as network variable's value gets replicated for every other object spawning beside our object.Right?

Building a Future API on top of Netty

I want to build an API based on Futures (from java.util.concurrent) that is powered by a custom protocol on top of Netty (version 4). Basic idea is to write a simple library that would abstract the underlying Netty implementation and make it easier to make requests.
Using this library, one should be able to write something like this:
Request req = new Request(...);
Future<Response> responseFuture = new ServerIFace(host, port).call(req);
// For example, let's block until this future is resolved
Reponse res = responseFuture.get().getResult();
Underneath this code, a Netty client is connected
public class ServerIFace {
private Bootstrap bootstrap;
private EventLoopGroup workerGroup;
private String host;
private int port;
public ServerIFace(String host, int port) {
this.host = host;
this.port = port;
this.workerGroup = new NioEventLoopGroup();
bootstrap();
}
private void bootstrap() {
bootstrap = new Bootstrap();
bootstrap.group(workerGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
#Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(Response.class.getClassLoader())));
ch.pipeline().addLast("response", new ResponseReceiverChannelHandler());
}
});
}
public Future<Response> call(final Request request) throws InterruptedException {
CompletableFuture<Response> responseFuture = new CompletableFuture<>();
Channel ch = bootstrap.connect(host, port).sync().channel();
ch.writeAndFlush(request).addListener((f) -> {
if (f.isSuccess()) {
System.out.println("Wrote successfully");
} else {
f.cause().printStackTrace();
}
});
ChannelFuture closeFuture = ch.closeFuture();
// Have to 'convert' ChannelFuture to java.util.concurrent.Future
closeFuture.addListener((f) -> {
if (f.isSuccess()) {
// How to get this response?
Response response = ((ResponseReceiverChannelHandler) ch.pipeline().get("response")).getResponse();
responseFuture.complete(response);
} else {
f.cause().printStackTrace();
responseFuture.cancel(true);
}
ch.close();
}).sync();
return responseFuture;
}
}
Now, as you can see, in order to abstract Netty's inner ChannelFuture, I have to 'convert' it to Java's Future (I'm aware that ChannelFuture is derived from Future, but that information doesn't seem useful at this point).
Right now, I'm capturing this Response object in the last handler of my inbound part of the client pipeline, the ResponseReceiverChannelHandler.
public class ResponseReceiverChannelHandler extends ChannelInboundHandlerAdapter {
private Response response = null;
#Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
this.response = (Response)msg;
ctx.close();
}
public Response getResponse() {
return response;
}
}
Since I'm new to Netty and these things in general, I'm looking for a cleaner, thread-safe way of delivering this object to the API user.
Correct me if I'm wrong, but none of the Netty examples show how to achieve this, and most of the Client examples just print out whatever they get from Server.
Please note that my main goal here is to learn more about Netty, and that this code has no production purposes.
For the reference (although I don't think it's that relevant) here's the Server code.
public class Server {
public static class RequestProcessorHandler extends ChannelInboundHandlerAdapter {
#Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ChannelFuture future;
if (msg instanceof Request) {
Request req = (Request)msg;
Response res = some function of req
future = ctx.writeAndFlush(res);
} else {
future = ctx.writeAndFlush("Error, not a request!");
}
future.addListener((f) -> {
if (f.isSuccess()) {
System.out.println("Response sent!");
} else {
System.out.println("Response not sent!");
f.cause().printStackTrace();
}
});
}
}
public int port;
public Server(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
#Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(Request.class.getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
// Not really shutting down this threadpool but it's ok for now
ch.pipeline().addLast(new DefaultEventExecutorGroup(2), new RequestProcessorHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new Server(port).run();
}
}

Xamarin support for AdMob Rewarded Interstitial

Today I noticed AdMob is offering rewarded interstitial option. I'd like to integrate it in my game. Does the current Xamarin.GooglePlayServices.Ads support the integration?
Has anyone tried it? Love to hear from your experience.
Thanks!
I have just attempted to integrate them and they work fine for me in debug (yet to try in release mode - will update once I've tried it)
Simply create an interstitial ad as you normally would but use the Reward Video ad unit ID instead of the usual interstitial ad ID.
If you want to try it yourself, the sample Reward Video ad unit ID provided by Google for testing is:
ca-app-pub-3940256099942544/5224354917
#region RewardedViewAd
private IRewardedVideoAd rewardedVideoAd;
private void InitialRewardVideo()
{
rewardedVideoAd = MobileAds.GetRewardedVideoAdInstance(this);
rewardedVideoAd.RewardedVideoAdListener = this;
this.LoadRewardAd();
}
private void LoadRewardAd()
{
if (!rewardedVideoAd.IsLoaded)
{
#if DEBUG
rewardedVideoAd.LoadAd(" ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().Build());
#else
rewardedVideoAd.LoadAd("ca-app-pub-9045308343519031/327467645", new AdRequest.Builder().Build());
#endif
}
}
private void StartRewardedVideoAd()
{
if (rewardedVideoAd.IsLoaded)
{
rewardedVideoAd.Show();
}
}
public void OnRewarded(IRewardItem reward)
{
var coins = reward.Amount;
}
public void OnRewardedVideoAdClosed()
{
this.LoadRewardAd();
}
public void OnRewardedVideoAdFailedToLoad(int errorCode)
{
}
public void OnRewardedVideoAdLeftApplication()
{
}
public void OnRewardedVideoAdLoaded()
{
}
public void OnRewardedVideoAdOpened()
{
}
public void OnRewardedVideoCompleted()
{
}
public void OnRewardedVideoStarted()
{
}
protected override void OnPause()
{
this.rewardedVideoAd.Pause(this);
base.OnPause();
}
protected override void OnResume()
{
this.rewardedVideoAd.Resume(this);
base.OnResume();
}
protected override void OnDestroy()
{
this.rewardedVideoAd.Destroy(this);
base.OnDestroy();
}
#endregion;

Parse Local Datastore e Message "no results found for query"

I am trying to finish this program and i am stuck. This is my first program and now it wont work. I keep getting this error when i add query.fromLocalDatastore(); The code runs fine until i try to get it from the local storage. This is telling me there is nothing there for it to retrieve and i don't know why. When i added my test data it worked fine but when i try to pull data from another table i get the error above. Apparently when i added the test data the server synced with the local datastore. Now it is not. Can someone tell me what I did wrong?
public class DataHolder extends Application {
int age;
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(getApplicationContext());
Parse.initialize(this,key, key);
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
ParseACL.setDefaultACL(defaultACL, true);
}
public class MainActivity extends ActionBarActivity implements Disclaimer.DisclaimerListener {
protected void continueToRun() {
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapter, View v, int x, long lng) {
final ParseQuery<ParseObject> query = ParseQuery.getQuery("Phone_Numbers");
query.fromLocalDatastore();
if (x == 1) {
final Intent intent = new Intent(getBaseContext(), Protocol_Template.class);
query.fromLocalDatastore();
query.whereEqualTo("objectId", "uGANULyrdL");
startActivity(intent);
}
}
public class Protocol_Template extends Activity {
DataHolder global;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_protocol__template);
final TextView protocol = (TextView) findViewById(R.id.txt02);
findViewById(R.id.btn2timesUpperLeft);
final ParseQuery<ParseObject> query = ParseQuery.getQuery("Phone_Numbers");
query.fromLocalDatastore();
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
final String protocols = object.get("PhoneNumber").toString();
protocol.setText(protocols);
} else {
protocol.setText(e.getMessage());
}
}
});
}

display image in GWT

I have created a widget to display the slideshow.In firefox,everything is fine but in chrome nothing happens. After I refresh with many times, the slideshow is displayed. I don't know Why. Can you give me some ideas? Tks
This is my GWT client:
public SlideClient() {
super();
setStyleName("flexslider");
setHeight("100%");
setWidth("100%");
}
#Override
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
this.client = client;
this.paintableId = uidl.getId();
listImage = Arrays.asList(uidl.getStringArrayAttribute("listImage"));
listUrl = Arrays.asList(uidl.getStringArrayAttribute("listUrl"));
loadImage();
checkImagesLoadedTimer.run();
}
public void display() {
m.setStyleName("slides");
m.setHeight("100%");
m.setWidth("100%");
add(m);
}
public native void slideshow() /*-{
$wnd.$('.flexslider').flexslider({slideshowSpeed: 2000});
}-*/;
public native String getURL(String url)/*-{
return $wnd.open(url,
'target=_blank')
}-*/;
private Timer checkImagesLoadedTimer = new Timer() {
#Override
public void run() {
if (loadedImageElements.size() == toLoad) {
display();
} else {
add(new Label("đang load "+loadedImageElements.size()));
checkImagesLoadedTimer.schedule(2000);
}
}
};
private void loadImage() {
for (String tmp : listImage) {
AbsolutePanel panel = new AbsolutePanel();
final Image ima = new Image(tmp);
add(new Label("before put"));
ima.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
loadedImageElements.put(toLoad+"", ima);
slideshow();
add(new Label("đang put "+loadedImageElements.size()));
}
});
add(new Label("after put"));
panel.add(ima);
m.add(panel);
if (toLoad != 0) {
panel.setVisible(false);
}
toLoad++;
}
}
}
Did you implement an Image Loader to prepare your images before they are displayed? A clean solution would be to add the image elements to your page root as an invisible istance, wait for them to load and then use them elsewhere.
You should check out the tutorials about ImageBundling as well: ImageResource
Here's a little extract from one of my image loader classes as you requested, altough there are different ways to realize that:
private HashMap<String,ImageElement> loadedImageElements = new HashMap<String,ImageElement>();
private int toLoad = 0;
private void loadImage(final String name, String url){
final Image tempImage = new Image(url);
RootPanel.get().add(tempImage);
++toLoad;
tempImage.addLoadHandler(new LoadHandler(){
public void onLoad(LoadEvent event) {
loadedImageElements.put(name,ImageElement.as(tempImage.getElement()));
tempImage.setVisible(false);
}
});
}
The image url is retrieved via a ClientBundle-Interface pointing towards the real positions of the images.
I also implemented a timer running in the background to check if all the images have been loaded:
private Timer checkImagesLoadedTimer = new Timer(){
public void run() {
System.out.println("Loaded " + loadedImageElements.size() + "/" + toLoad + " Images.");
if(loadedImageElements.size() == toLoad){
buildWidget();
}else{
checkImagesLoadedTimer.schedule(50);
}
}
};
After everythign is ready, the original widget/page is created.
But as I said there are many ways to implement image loaders. Try out different implementations and select one that suits your needs best.

Resources