How to use the new AndroidX Media2? - android

I am using Media App Architecture as a guide for building a music player app. But it uses the classes from support media-compat / Androidx Media.
But now AndroidX Media2 is available in stable channels and I don't see any word of it. What is it?
is AndroidX Media2 supposed to deprecate AndroidX Media?
is there a developer guide or other sources of documentation for AndroidX Media2?
Please, no links to JavaDoc, thanks.

Jetpack Media3 has launched!
https://developer.android.com/jetpack/androidx/releases/media3
This blog post gives a great explanation about how the media libraries evolved. I strongly recommend integrating with androidx.media3.
If for whatever reason you cannot use androidx.media3, my recommendation is to stick with androidx.media instead of androidx.media2 due to the latter not being supported by other media integrations, such as Cast Connect. Integrating Media2 with ExoPlayer is also quite a bit more complex.
From my perspective, the key benefit of switching from Media1 to Media2 is the ability to provide more fine-grained permission controls. See Jaewan Kim's blog post that goes in depth about the more complex SessionPlayerConnector API and permissions to accept or reject connections from a controller in media2.
If you have an existing Media1 implementation using MediaSession (preferably using ExoPlayer with MediaSessionConnector), and have no need for the permission controls in Media2, you can either stick with Media1 or upgrade to Media3.
The “What's next for AndroidX Media and ExoPlayer” talk from Android Dev Summit 2021 goes much more in depth on Media3.

All the documentation I was able to find was useless and outdated. androidx.media and the appcompat media libraries are both superseded by androidx.media2 (but not deprecated for some reason). The most high-level API for media apps appears to be MediaLibraryService for the service and MediaBrowser for the frontend. Just make absolutely sure you declare the right intent filters on the service (which are in the javadoc :P)

Here is my working solution using Media2 libs (AndroidX):
Add 3 dependencies in Build.gradle
implementation "androidx.media2:media2-session:1.2.0"
implementation "androidx.media2:media2-widget:1.2.0"
implementation "androidx.media2:media2-player:1.2.0"
Create layout activity_video_player.xml and put this code into it:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/accompany_agent_details"
style="#style/Layout.Default">
<LinearLayout
android:id="#+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<android.widget.VideoView android:id="#+id/simpleVideoView"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
</RelativeLayout>
<style name="Layout.Default">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
</style>
Create activity class SimpleVideoActivity:
public class VideoPlayerActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player_android_x);
VideoView simpleVideoView = findViewById(R.id.simpleVideoView);
MediaMetadata mediaMetaData = new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_TITLE, "Put your video text here").build();
UriMediaItem item = new UriMediaItem.Builder(Uri.parse("Put your video URL here")).setMetadata(mediaMetaData).build();
MediaPlayer mediaPlayer = new MediaPlayer(this);
simpleVideoView.setPlayer(mediaPlayer);
mediaPlayer.setMediaItem(item);
mediaPlayer.prepare();
mediaPlayer.play();
}
}

Related

show VR tour in android

I want to show VR tour like this http://alfavr.ir/alfavr.ir/to/park.html in my Android app.
how can I do that?
I tried to display with web view because the file format is Html but did not work.
You can try with VRPanormaView which is included in the Google VR SDK. This is an example from https://developers.google.com/vr/develop/android/vrview:
<com.google.vr.sdk.widgets.pano.VrPanoramaView
android:id="#+id/pano_view"
android:layout_margin="5dip"
android:layout_width="match_parent"
android:scrollbars="#null"
android:layout_height="250dip" />
Inside the Activity, the primary method is VrPanoramaView.loadImageFromBitmap(). Call it with a standard Android Bitmap object and an optional VrPanoramaView.Options object. The latter is used to configure the format of the image. The Activity also implements a VrPanoramaEventListener which receives events when the load operation succeeds or fails.
Adding this line make It work.
webView.getSettings().setJavaScriptEnabled(true);

Algolia Android initial search is really slow

I recently implemented Algolia on my app successfully just like the examples.
But the initial search takes about 5 to 7 seconds and I couldn't find a way to make it faster after checking the whole library code and documentation. After the initial search, search becomes very fast.
There is nothing unusual in my implementation but maybe you can see something that I don't. The following code is from the activity where I initialize Algolia:
Searcher searcher = new Searcher(ALGOLIA_APP_ID, ALGOLIA_SEARCH_API_KEY, ALGOLIA_INDEX_NAME);
searcher.setQuery(new Query("word").setExactOnSingleWordQuery(Query.ExactOnSingleWordQuery.ATTRIBUTE));
searcher.addNumericRefinement(new NumericRefinement("CountryId", NumericRefinement.OPERATOR_EQ, 1));
InstantSearch helper = new InstantSearch(this, searcher);
helper.setSearchOnEmptyString(false);
helper.search();
And this is the related xml layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="6dp"
android:paddingRight="10dp"
android:paddingLeft="1dp"
android:paddingTop="6dp">
<com.algolia.instantsearch.ui.views.SearchBox
android:id="#+id/searchBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:queryHint="#string/search_text_hint"
algolia:searchIcon="#drawable/icn_search_big"
algolia:closeIcon="#drawable/icn_clear_filled_big"
android:queryBackground="#drawable/sarch_query_shape"
android:background="#drawable/search_shape"
algolia:autofocus="true"
algolia:submitButtonEnabled="false" />
</FrameLayout>`
<com.algolia.instantsearch.ui.views.Hits
android:id="#+id/hits"
android:layout_width="match_parent"
android:layout_height="match_parent"
algolia:autoHideKeyboard="true"
algolia:hitsPerPage="6"
android:layout_below="#+id/searchBarParentLayout"
algolia:infiniteScroll="false"
algolia:itemLayout="#layout/search_item_algolia_row"/>
Do you have any idea what can be the issue here?
I'm glad that the issue disappeared when you switched to another wifi.
For future readers that may encounter network issues with InstantSearch Android:
First of all, build and run one of our demo applications
If you see no problem running the example application, you can try using a proxy like Charles to investigate what's happening between your app and the network
If your problem persists when running the examples, or if you are following the documentation, send an email to support#algolia.com describing the issue with a sample of your code!

Using Crosswalk in an Android Cordova Project with Embedded WebView

I have an existing Android Cordova project which uses an embedded WebView. What this means is that the Activity does not extend CordovaActivity, but instead embeds the SystemWebView and initializes within the onCreate.
The following is currently how this is being done:
Within the layout XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
.... other layout elements not related to Cordova....
<org.apache.cordova.engine.SystemWebView
android:id="#+id/cdvWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Within the Activity's onCreate:
SystemWebView systemWebView = (SystemWebView) findViewById(R.id.cdvWebView);
CordovaWebView cdvWebView = new CordovaWebViewImpl(new SystemWebViewEngine(systemWebView));
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(this);
cdvWebView.init(this, parser.getPluginEntries(), parser.getPreferences());
Due to the bug in Lollipop versions 5.0.+ missing the "set" button, I want to implement the Crosswalk plugin into the project.
Unfortunately, all the documentation I'm finding assumes that a typical Cordova install is being used. I haven't been able to get the embedding and initialization of the XWalkWebView working correctly and keep getting a blank white screen.
Has anybody has success with a similar scenario?
I'm not sure, but this might answer your question. It seems to show implementing an XWalkWebView outside of a typical cordova project:
https://github.com/kurli/crosswalk-website/wiki/How-to-use-Crosswalk-Embedded-API-on-Android

How to display Google Map in an Android app using Xamarin

I am designing an android app with Xamarin which will have an activity with google maps inside it, but i am struggling to display the map. It shows an empty box with no map display on it. Following is the code which i used for this practice.
My Activity
namespace teslin
{
[Activity(Label = "Map")]
public class MpActivity : Android.GoogleMaps.MapActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MapView);
var map = FindViewById<MapView>(Resource.Id.mapv);
map.Clickable = true;
map.Controller.SetZoom(16);
map.Controller.SetCenter(new GeoPoint((int)40.8270449E6, (int)-73.9279148E6));
}}
xml lay out
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout3"
android:layout_weight="9" />
<com.google.android.maps.MapView
android:id="#+id/mapv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:apiKey="AIzaSyD-UMij5IO6ezjuFCNnF7tRoG3niaPbNEU" />
</LinearLayout>
It looks like you're using the obsolete/outdated Maps API v1. Google removed support for this last year.
Instead you want to use Android Maps API v2 (a part of Google Play Services). Xamarin has some documentation on Maps in Android. It can be a bit tricky to get Maps working in Android, so carefully read these docs. Specifically, you'll want to read the section on the Maps API.
There is an example on Github that shows how to use the Google Play Services Component to add Maps to your application.
For those who are looking for a thorough example, check out this video that demonstrates how to integrate Google Maps with Xamarin Android, hope this helps!

ActionBarSherlock - How does it creates title on each xml files in layout?

Recently i have successfully implemented the ActionBarSherlock Demos sample in eclipse. But there is something that i am not able to understand it that how this library automatically creates the header with one icon and a text "ActionBarSharelock Demos" in tab_navigation.xml by using merely a LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dip">
</LinearLayout>
This library is new for me and i am so anxious to know, how this library creates it?
I created a new android project on target api 15 and attach the ActionBarSherlock library to it. then i found that there some of the files are missing from my new android project when i compared it to demos sample project of android. Those files are listed below.
1.pom.xml
2.bin/classes.dex
3.bin/jarlist.cache
4.bin/resources.ap_
Moreover i got a new error in my activity file on eclipse "R cannot be resolved to a variable."
If you know anything about my problem then please share your views.
To use the simplest example:
public class Simple extends SherlockActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
((TextView)findViewById(R.id.text)).setText(R.string.simple_content);
}
}
All you have to do is let your activity inherit one of the Sherlock activites (e.g., SherlockActivity, SherlockFragmentActivity).
You can program the action bar by using the getSupportActionBar() method of the SherlockActivity, e.g. to set the title: getSupportActionBar().setTitle( R.string.my_title );

Categories

Resources