Android - Playing Youtube Videos using Youtube Player API in ListView - java

I have tried using WebViews to display Youtube Videos in ListView, what happens is when i scroll off screen and back to the cell the Video was located the Video disappears meaning that the WebView does not render back the WebView with the Video in it. So i tried using YoutubePlayerAPI but i am finding it difficult to understand how i can insert these Videos using the YoutubePlayerAPI.
Could someone please advise?
I have tried using YoutubePlayerView in my ArrayAdapter but i get Inflate errors, i have also created YoutubeBaseActivity and YoutubeFragment but cant understand how i get the layout or ui to display these videos in my ListView Cells.

How i did it
Download the youtube android SDK from here then get the jar file and copy it to libs folder on your app.
Enter this in dependency in gradle.build under dependencies
compile files('libs/YouTubeAndroidPlayerApi.jar')
Syc and build.
Create an activity that extends YoutubeBaseActivity like below and implement the YoutubePlayer.OnInitializedListener as shown below. Override the methods on the interace. Android studio can help you do this.
public class Display extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
Then, when it loads successfully,
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean b) {
if (!b) {
player.cueVideo(videoID);
}
}
This will load the youtube player and you play on the API
Happy coding, sorry am late

Related

How to change a photo?

I am trying to change photos in android studio by clicking on my button.
When I put code for changing the photo in my MainActivity.java I keep getting this type of error messages and it says :
Cannot resolve symbol "image"
image.setImageResource(R.drawable.xxx);
I am watching Udemy course for android development and I have done everything same like the professor on that video.
I have tried to restart android studio.
I have tried to make new project.
I have tried to clear invalidate caches and restart.
public void changeImage(View view)
{
ImageView bitcoin = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
}
I hope there is actual error with android studio,because code is clone of the video that I am watching.
You are binding your layout's ImageView in Java file with bitcoin variable and you are trying to set an image on an unknown variable 'image'(maybe it's not defined in the class). So you have to set as below.
ImageView bitcoin = findViewById(R.id.bitcoin);
bitcoin.setImageResource(R.drawable.xxx);
Set Your Code Like this
ImageView image = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
change your this line
image.setImageResource(R.drawable.xxx)
to this one:
bitcoin.setImageResource(R.drawable.xxx)

Loading Images with Picasso into RecyclerView from External Drive - Android App

I recently had an issue where I couldn't work out how to add any photos I take with my app into the phone's gallery.
I have now achieved this. My issue though, is when I am retrieving the photos I take onto the app from a Firebase database.
I have the following code using picasso to get my photos (this is from an Adapter class):
#Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.with(mContext)
.load(uploadCurrent.getImageUrl())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.imageView);
}
If I take photos with the stock camera on the phone and upload these to Firebase via the app, when I come to retrieve them and show on my phone, they are fine. However, if I take a picture with the app I created, it uploads to the Firebase database okay but won't retrieve the picture. There is the placeholder for it with the image title but no actual image. I'm wondering whether it's something to do with the orientation but I'm not sure.
Has anyone got any suggestions? All welcome.
Thanks
On further research, I decided to give Glide a try instead of Picasso and it worked.
So, instead of the above, replace 'Picasso' with 'Glide' and '.fit()' with '.fitCenter()'.
Also, add the following to the gradle (using the latest versions):
implementation 'com.android.support:support-v4:27.0.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
and change this line:
Picasso.with(this).load(mImageUri).into(mImageView);
to this:
Glide.with(this).load(mImageUri).dontAnimate().into(mImageView);
I hope that helps someone else in the future.

React Native: Volume Button Should Only Control Media (Android)

I would like to know how can I force volume buttons to only control media while in my application.
I know this is an android specific question and ios does this by default.
There are two related questions giving android solutions:
Volume Control in android application
How can I manage audio volumes sanely in my Android app?
They both suggest adding setVolumeControlStream(AudioManager.STREAM_MUSIC); to android but I'm a react-native developer and not familiar with java.
There is also this react-native related question:
How to control media volume?
Which gives me no idea on where onCreate() is.
Any Specific suggestions on where to add this line in java part of react-native?
So I did some research and thanks to #VolkanSahin45 for his comment, I figured it out.
Adding setVolumeControlStream(AudioManager.STREAM_MUSIC); was right.
What I had to do was to Override the onCreate() function inside MainActivity.java.
public class MainActivity extends ReactActivity {
/* Any previous code you had here */
[...]
/* Override the onCreate function here */
#Override
protected void onCreate(Bundle savedInstanceState) {
/* Add this line to keep the original behavior of onCreate() */
super.onCreate(savedInstanceState);
/* This one does the trick */
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}

How do I change the main xml file from another activity?

I am very new to Java. I am doing a school project at the moment and I have my main activity, then I have a settings activity. I am trying to modify the xml from the main activity with the settings activity. I am able to modify the settings xml file with the settings.java, but I would like to modify the main activity xml with settings.java
public class Settings extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Get the Intent that started this activity and extract the string
Switch switchButton;
final RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.activity_settings);
final RelativeLayout mRelativeLayoutMain = (RelativeLayout) findViewById(R.id.activity_main);
switchButton = (Switch) findViewById(R.id.switch1);
switchButton.setChecked(true);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}
}
});
if (switchButton.isChecked()) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}}
public void toast1(View view) {
android.widget.Toast.makeText(this, "Created by Cody Walls and Tommy Serfas", android.widget.Toast.LENGTH_LONG).show();
}
/*public void switch1(View view) {
ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollView);
mScrollView.setBackgroundColor(Color.GRAY);
}*/
}
In the Code I am trying to change the background of the main activity xml with :
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
and when I run the app and click the intent it will crash with the error:
"java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null
object reference"
I think the easiest way is to create an PreferenceManager.SharedPreferences, in which I recommend you to store current app data. This will help you not to loose any changes in app after you exit the it. Here is short instructions:
Create button in settings activity which will change something in main activity.
Create onClickListener for your button.
Use .SharedPreferences to store was you button clicked or not. (I recommend storing boolean variables, this way you can store was button clicked or not.)
I both of your activities in onCreate method call .getSharedPreferences to read saved app values. (I mean to read was the button clicked or not.)
Use app values you got from 4. to change any element in activity. (For example if you stored that button was clicked, then change some TextView text or etc.)
I hope you understood the idea.
Link to the Android developer tutorial about App key values storing & saving
Link to the StackOverflow much easier explanation & examples
There are a couple of ways of doing this (Some of which depends on how you are switching back and forth from each activity). It also depends on what things you are changing.
From your settings page, as you are changing different settings, you'll save this content within Preferences. (You can see more how to use Preferences here: https://examples.javacodegeeks.com/android/core/ui/settings/android-settings-example/ or by just Googling it).
On you main activity, depending on how you come back to it (onStart most likely), you can setup the things you need to programmatically.
So, you may need to do a little research on the Android lifecycle and how each cycle works (https://developer.android.com/guide/components/activities/activity-lifecycle.html), how to program the UI programmatically through Java (http://startandroid.ru/en/lessons/220-lesson-16-creating-layout-programmatically-layoutparams.html), and the Preferences Android library to save certain settings.
The xml isn't meant to be "altered". You can change the UI programmatically. It's possible to build an Android app without any xml. When Android was first built, it didn't use the xml to create the UI. It was all done through Java. It was then added to use xml to create your activities or fragments or any UI component. This made things easier for more static activities or activities with very little dynamic content.

Android Creating a MediaController Object

So I have a activity page with a button on it. If you click the button it takes you to a new activity page where it will play an audio file that inside /res/raw. This plays perfectly fine no problem.
My problem is I am unable to attach a mediacontroller object to the mediaplayer and therefore I can't play/pause/stop/rewind the audio file. So my code for the activity page that plays the media file looks something like this:
public class MediaActivity extends Activity {
private MediaController a;
private MediaPlayer b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
a = (MediaController) findViewById(R.id.mediaController1);
b = MediaPlayer.create(MediaActivity.this, R.raw.sound);
b.start();
}
I'm missing a few extra things like the onCreateOptionsMenu function, but that's not relevant here. The problem is when I transition to this activity, the sound plays but I don't see the mediacontroller. I've set the controller to visible, I've tried several things like calling the .show() function but still nothing. I ran across a couple of code snippets but the code is too long for my purposes, I just need to attach the controller to the player.
Any ideas on how I can do this?

Categories