I am trying to add Chromecast function to my Android app. I've been following the code lab tutorial from Google's cast dev page, I've managed to get it working using there test app. But when I try to copy the code to from the test app to my own app i get the option to connect to my test device. But once it connects it just resets my Chromecast, then disconnects again.
This is my CastOptionProvider Class
public final class CastOptionsProvider implements OptionsProvider {
#Override
public CastOptions getCastOptions(Context context) {
return new CastOptions.Builder()
.setReceiverApplicationId(context.getString(R.string.app_id))
.build();
}
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}
}
This is my activities xml that I want to have Chromecast support
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="studios.p9p.harrop99.p9pdocumentaries.Actual_Genres.Taboo">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
The activities java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taboo);
setupActionBar();
mCastContext = CastContext.getSharedInstance(this);
}
private void setupActionBar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Taboo");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private MenuItem mediaRouteMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.browse, menu);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item);
return true;
}
I know the tutorial is not complete yet but by this point in the tutorial I can connect to my test casting device and a casting logo appears on my screen. But in my own app it resets my Chromecast and does not actually connect. Also I have a Google speaker that in the tutorial it will offer to connect to this. It does not even show in my app.
Here is a link to the tutorial I'm following.
Edit
Do I need a custom receiver? If so where would I upload it to?
fixed this issue, it was that i needed a custom Reciever adding to my server. it was crashing because i was trying to use the basic a basic Reciever from Googles dev page.
the basic reciever only allows for mp4 streams and direct video sources. But because im using youtube streams i had to use a package from github. this included a custom reciever to upload.
the basic tutorial i used can be found here
chromecast tutorial
and his github can be found here
github package and sample app
i also want to thank Pier Francescosoffritti for all the hard work he has put in to this
Related
I am doing a java to c# transition, and need help.
in Visual Studio 2019 Pro, Android 9.0 (Pie), I am doing this example:
https://learn.microsoft.com/en-us/xamarin/android/platform/binding-java-library/binding-a-jar
The goal is later to convert my java libs to c# for my big project.
I follow the instructions to the letter, and when I come to the part where I need to create an ImageView, there is the problem. Once I create the ImageView, it has an android:src field, something like this (auto generated):
android:src="#drawable/icon"
The problem here, is when I remove that field, in the VS designer, the ImageView disappears and it does not matter if I have the c# code or not, even when I set the srec to:""
Bellow is literally all the code in MainActivity:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Com.Squareup.Picasso;
namespace App3
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
/***********************************************************************/
/**Picasso Code**/
ImageView imageView = FindViewById<ImageView>(Resource.Id.imageView1);
// Use the Picasso jar library to load and display this image:
Picasso.With(this)
.Load("http://i.imgur.com/DvpvklR.jpg")
.Into(imageView);
/**End Ff Picasso Code**/
/***********************************************************************/
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="#drawable/icon" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView1" />
</LinearLayout>
The app does nothing, and in the emulator, the ImageView is empty.
And there is no error, or anything.
If you need anything more, please let me know.
I hope you can help me, if not, thank you for your time.
Edit 1:
It seems, the problem lies in the C# code.
If I add the android:src field, it still does not show up on the emulator, but when I remove the code (the Picasso code) than the image(drawable/icon) shows. Still no error!!
try this
<ImageView
android:src="#drawable/icon" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="50px"
android:minHeight="50px"
android:id="#+id/imageView1" />
I am new to Java and Android Studio.
I am currently working with Android Studio and I am able to load the YouTube video from the video ID, but in my app, I cant keep the video ID static I need to change the ID frequently without requiring users to download an app update. I have tried Firebase real-time database and remote config, but in both of them, the video lags because they download data asynchronously. What is the optimal solution to this problem?
Thank you for helping in advance.
Here is my YouTube video Fragment:
package shah.vatsal.kinitro
public class youtubefragment extends Fragment {
private static final String API_KEY =
"AIzaSyAXjBSS9NVAewJ2Z0Z86JrlHsJbJzoP_Ns";
private static String VIDEO_ID = "mMMerxh_12U";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.youtube_videos, container,
false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() {
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.loadVideo(VIDEO_ID);
player.play();
}
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
// YouTube error
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
return v;
}
}
YouTube Layout:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/youtube_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/youtube_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I'm sure my suggestion doesn't follow the path you're trying to take, but you could easily achieve that by setting a webpage in which the video is put and then loading it from a WebView so as you could simply change the webpage and the video would be updated next time users run the app if you don't set a cache system or you just clear it whether you have it set.
It's way easier than trying to deal with Firebase development environment (which I find complicated), but you may deal with some lag while loading the page if the page isn't well optimized; however you could solve it today by simply setting a Tumblr space, for instance, and from time to time you could just change the link provided in order to showcase different videos.
Even if you want to show other elements in your activity besided the WebView, you could do it with some limitations or you could just insert what you like on the webpage itself (it depends mostly on your real needs).
Here is a Kotlin WebView sample in case you aren't familiar with it and here you can learn more about WebViews in general.
I am following the code lab tutorial.
My Gradle file looks like this
dependencies {
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:mediarouter-v7:25.0.0'}
This is my CastOptionsProvider class
public class CastOptionsProvider implements OptionsProvider {
#Override
public CastOptions getCastOptions(Context context) {
return new CastOptions.Builder()
.setReceiverApplicationId(context.getString(R.string.chromecast_app_id))
.build();
}
#Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}}
This is the menu xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always" /></menu>
And this is my OnCreateOptionsMenu method in MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.navigation_drawer, menu);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item);
return true;
}
And this in the manifest file
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.mypackage.CastOptionsProvider" />
I have followed the codelab tutorial to its exact form, copy and pasted everything while changing those variable which needs to be changed.
My application and the chromecast device are connected to the same network. The Chromecast button appears in the Youtube app but not on my app. What am I missing ?
Thanks
It might be an invalid app_id.
You might try replacing your chromecast_app_id string resource with the app_id string resource value from the sample project.
(Right now, it's 4F8B3483. See: https://github.com/googlecast/CastVideos-android/blob/master/res/values/strings.xml).
Believe it or not, switching to that value made the cast icon visible in my app.
If everything is done properly, you just need to restart cast device (it does not work with your app_id without restarting after registration).
Check in Google Cast SDK Developer Console if your Application ID is correct and status is Published.
When publish your Application ID, restart your Chromecast device.
For testing purposes, you can use CC1AD845 or 4F8B3483 as Application ID. These are from Google sample apps.
Adding another answer as the accepted answer did nothing for me. The issue happened to me only when building a proguarded version of my app.
I had to add a couple lines to my proguard file (essentially all of the classes referenced by xml as mentioned here: https://stackoverflow.com/a/24578823/1048847)
-keep class com.your.package.CastOptionsProvider { *; }
// I have a custom MediaRouteActionProvider but this may need to be
// android.support.v7.app.MediaRouteActionProvider in the case of OP
-keep class com.your.package.MediaRouteActionProvider { *; }
you are missing attribute icon here
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
android:icon="#drawable/chromebutton" app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always" /></menu>
I created a sample app using JieCao player for android and it works perfectly fine both on portrait and landscape mode (on pressing expand button). But when I tried to integrate the same for the another app that I'm creating I get a black screen on pressing the video expand button but the audio is playing in the background. Couldn't get the reason why this is happening !! I have explained the code that I'm working on below :
Mainactivity.java
jcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.custom_videoplayer_standard);
jcVideoPlayerStandard.setUp("http://2449.vod.myqcloud.com/2449_bfbbfa3cea8f11e5aac3db03cda99974.f20.mp4","");
ImageLoader.getInstance().displayImage("http://p.qpic.cn/videoyun/0/2449_bfbbfa3cea8f11e5aac3db03cda99974_1/640",jcVideoPlayerStandard.thumbImageView);
activity_main.xml
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="#+id/custom_videoplayer_standard"
android:layout_width="match_parent"
android:layout_marginTop="55dp"
android:layout_height="250dp" />
And a separate java file that I include in AndroidManifest file though I'm pretty much sure it has nothing to do with the error. Here is the code :
public class VideoApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
LeakCanary.install(this);
initUniversalImageLoader();
}
private void initUniversalImageLoader() {
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext());
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for releaseAllVideos app
config.defaultDisplayImageOptions(getDefaultDisplayImageOption());
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
}
public static DisplayImageOptions getDefaultDisplayImageOption() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(new ColorDrawable(Color.parseColor("#f0f0f0")))
.resetViewBeforeLoading(true)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(500))
.build();
return options;
}}
If there is anything else needed please let me know, I just want to know where I am going wrong.
Note : People with higher reputation please create a tag "JieCao-player" which might be useful while creating a question related to this category. As I don't have enough reputation, kindly do the needful.
Just need to make sure that "android:hardwareAccelerated" is set to true in all the activities in Manifest file. By doing so the problem got resolved.
I am trying to integrate a qr code scanner into my google cardboard program. I have used the answer here to start a cardboard camera. I am encountering problems when I try to integrate the vision API for barcode reader into this. I need to start camera passing a texture to the vision API's camerasource portion.
final CameraSource cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
How do I integrate the qr code scanner with vision API into my cardboard app?
Google has provided a supremely useful View for managing your CameraSource for preview and detection. You don't have to worry about managing the texture itself or determining the layout of the texture/surface in your view.
It's called CameraSourcePreview, and it's used to manage the operation of your CameraSource along with a SurfaceView.
You can use the CameraSourcePreview like any other view in your layout, for example in this full screen LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<com.google.android.gms.samples.vision.barcodereader.ui.camera.CameraSourcePreview
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.gms.samples.vision.barcodereader.ui.camera.CameraSourcePreview>
</LinearLayout>
And in your activity you set up your BarcodeDetector and CameraSource like normal (probably in your onCreate() method) and then (likely onResume()) start your CameraSourcePreview. It would look something like this:
if (mCameraSource != null) {
try {
mPreview.start(mCameraSource, mGraphicOverlay);
} catch (IOException e) {
Log.e(TAG, "Unable to start camera source.", e);
mCameraSource.release();
mCameraSource = null;
}
}
So really, you'll just be managing your CameraSource through your the CameraSourcePreview, but it handles a lot of the heavy lifting of the layout/preview/etc. so you don't have to. Check out the BarcodeCaptureActivity to see more about how the activity manages these components.
Check out the barcode-reader sample in the android vision sample projects for the complete project sample.
Hope that helps, cheers.