Quickblox Improve video quality - java

I am getting very poor video quality using QuickBlox when the video is sent to the other device. For example, I see myself on my own device with very high quality, but once I look at the other device the quality of what is being received is very poor.
I tried increasing the FPS by using this:
cameraView.setFPS(30);
What can I do to improve the quality of video that is received?
I tested these two tablets on Google Hangouts and the quality was better than my app.
This is the code that can be found on the Sample by QuickBlox.
public class ActivityVideoChat extends Activity
{
private CameraView cameraView;
private OpponentGlSurfaceView opponentView;
private ProgressBar opponentImageLoadingPb;
private VideoChatConfig videoChatConfig;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.video_chat_layout);
initViews();
}
private void initViews()
{
Debugger.logConnection("initViews");
opponentView = (OpponentGlSurfaceView) findViewById(R.id.opponentView);
cameraView = (CameraView) findViewById(R.id.cameraView);
cameraView.setCameraFrameProcess(true);
cameraView.setQBVideoChatListener(qbVideoChatListener);
cameraView.setFPS(30);
cameraView.setOnCameraViewListener(new OnCameraViewListener() {
#Override
public void onCameraSupportedPreviewSizes(List<Camera.Size> supportedPreviewSizes) {
Camera.Size firstFrameSize = supportedPreviewSizes.get(0);
Camera.Size lastFrameSize = supportedPreviewSizes.get(supportedPreviewSizes.size() - 1);
cameraView.setFrameSize(firstFrameSize.width > lastFrameSize.width ? lastFrameSize : firstFrameSize);
}
});
opponentImageLoadingPb = (ProgressBar) findViewById(R.id.opponentImageLoading);
videoChatConfig = getIntent().getParcelableExtra(VideoChatConfig.class.getCanonicalName());
QBVideoChatController.getInstance().setQBVideoChatListener(DataHolder.getInstance().getCurrentQbUser(), qbVideoChatListener);
}
#Override
protected void onResume()
{
super.onResume();
cameraView.reuseCameraView();
}
#Override
protected void onPause()
{
cameraView.closeCamera();
super.onPause();
}
#Override
public void onDestroy()
{
QBVideoChatController.getInstance().finishVideoChat(videoChatConfig);
super.onDestroy();
}
OnQBVideoChatListener qbVideoChatListener = new OnQBVideoChatListener()
{
#Override
public void onCameraDataReceive(byte[] videoData)
{
if (videoChatConfig.getCallType() != CallType.VIDEO_AUDIO)
{
return;
}
QBVideoChatController.getInstance().sendVideo(videoData);
}
#Override
public void onMicrophoneDataReceive(byte[] audioData)
{
QBVideoChatController.getInstance().sendAudio(audioData);
}
#Override
public void onOpponentVideoDataReceive(byte[] videoData)
{
opponentView.loadOpponentImage(videoData);
}
#Override
public void onOpponentAudioDataReceive(byte[] audioData)
{
QBVideoChatController.getInstance().playAudio(audioData);
}
#Override
public void onProgress(boolean progress)
{
opponentImageLoadingPb.setVisibility(progress ? View.VISIBLE : View.GONE);
}
#Override
public void onVideoChatStateChange(CallState callState, VideoChatConfig chat)
{
switch (callState)
{
case ON_CALL_START:
break;
case ON_CANCELED_CALL:
finish();
break;
case ON_CALL_END:
finish();
break;
}
}
};
}

I'm actually unable to even receive video. Are you calling from android to android or android to ios?
Did you do anything special to get video to work? If so, can you please let me know what you did?

Related

Android MediaPlayer return Prepare failed.: status=0x1 on device but runs in emulator

I'm trying to play an audio streaming from a remote http uri trough MediaPlayer.
The code seems to work fine in the emulator audio stream is played (but there's some noise over it) if I try to play on the device the .prepare() call fails throwing an IO exception. The message reports: status=0x1 . The only articles related with this error I found where talking about reading/writing permissions over files and I think this's not the case, some other where related to wrong sequence of calls (.setDataSource, .prepare(), .start()) neither this should be my problem.
This's the code I'm using:
public class MainActivity extends AppCompatActivity {
public static final String KEY_ADDRESS = "address";
private static final int REQUEST_INTERNET = 21;
private ImageButton exit_button;
private ImageButton stop_button;
private ImageButton play_button;
private MediaPlayer media_player;
private String uri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean _skip_init = false;
int check_internet = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.INTERNET);
if (check_internet != PackageManager.PERMISSION_GRANTED) {
Log.i(getClass().getName(), "asking for internet access permission");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.INTERNET},
REQUEST_INTERNET);
_skip_init = true;
}
getSettings(this); // set uri
exit_button = findViewById(R.id.exit_button);
play_button = findViewById(R.id.play_button);
stop_button = findViewById(R.id.stop_button);
exit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
play_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO: start play
if(media_player!=null) {
media_player.start();
disablePlay();
}
}
});
stop_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO: stop play
if(media_player!=null){
media_player.stop();
media_player.reset();
initStreaming(uri);
enablePlay();
}
}
});
play_button.setEnabled(false);
stop_button.setEnabled(false);
play_button.setAlpha(0.5f);
stop_button.setAlpha(0.5f);
if(!_skip_init) {
initMediaPlayer(this);
initStreaming(uri);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if ((requestCode == REQUEST_INTERNET) && (grantResults.length > 0)) {
for (int i = 0; i < permissions.length; i++)
if ((grantResults[i] == PackageManager.PERMISSION_GRANTED)) {
Log.i(getClass().getName(), permissions[i] + " permission granted");
if (Manifest.permission.INTERNET.equals(permissions[i])) {
initMediaPlayer(this);
initStreaming(uri);
}
}
}
}
private void initMediaPlayer(#NonNull final Context context) {
media_player = new MediaPlayer();
media_player.setAudioStreamType(AudioManager.STREAM_MUSIC);
media_player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(getClass().getName(), "onPreparedListener");
play_button.setEnabled(true);
play_button.setAlpha(1f);
}
});
media_player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(getClass().getName(), "onCompletionListener");
Toast.makeText(context, getString(R.string.streaming_completed), Toast.LENGTH_LONG).show();
}
});
}
private void initStreaming(#NonNull final String uri) {
new Thread(
new Runnable() {
#Override
public void run() {
try {
media_player.setDataSource(uri);
media_player.prepareAsync();
} catch (Throwable e) {
Log.e(getClass().getName(), "Exception: " + e.getMessage());
}
}
}
).start();
}
#Override
protected void onDestroy() {
//TODO: stop and free resources?
Log.d(getClass().getName(), "onDestroy");
if(media_player!=null && media_player.isPlaying())
media_player.stop();
super.onDestroy();
}
private void enablePlay() {
play_button.setEnabled(true);
stop_button.setEnabled(false);
play_button.setAlpha(1f);
stop_button.setAlpha(0.5f);
}
private void disablePlay() {
play_button.setEnabled(false);
stop_button.setEnabled(true);
play_button.setAlpha(0.5f);
stop_button.setAlpha(1f);
}
}
Where's the error? Is possible to fix it?
Why the emulator is able to run it without errors?
--- UPDATE ---
same error using .prepareAsync()
--- UPDATE ---
On the very same hardware configuration (Samsung Galaxy S9 stock), where the application worked with the simple fix when uploaded trough adb over usb link once the app is signed in release variant it stopped working and I got again the same error code reported above.
On different hardware like Huawei P30 it works (installed as a signed release .apk).
Can this depend on a firewall rule?
Apparently the problem is fixed by this entry:
android:usesCleartextTraffic="true"
In AndroidManifest.xml file (application section).
-- UPDATE --
Actually the problem was given by some abnormal firewall rules that rejects connections from the same host after a give number of consecutive access.

setOnUtteranceProgressListener not at all working for Text To Speech for API > 21

I want setOnUtteranceProgressListener should notify a Toast after the speech is completed.It seems not working.
I have used setOnUtteranceProgressListener and on the speak function i have mentioned the paramaters as follows..
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, MainActivity.this.getPackageName());
I have given a "UniqueId" while calling speak function as follows.
myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params,"UniqueId");
In My program after the text to speech engine finishes speaking it should run a Toast notifying that it has finished speaking.But the setOnUtteranceProgressListner seems not working.
myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
}
#Override
public void onDone(String utteranceId) {
Toast.makeText(MainActivity.this,"Finished speaking.",Toast.LENGTH_LONG).show();
}
#Override
public void onError(String utteranceId) {
}
});
The all Code is as follows..
public class MainActivity extends AppCompatActivity {
String message;
private TextToSpeech myTTS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(myTTS.getEngines().size() == 0){
Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
}else{
myTTS.setLanguage(Locale.US);
if (status == TextToSpeech.SUCCESS){
//Toast.makeText(MainActivity.this,"Status working.",Toast.LENGTH_LONG).show();
message = "How may i help you.";
}
}
}
});
myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
}
#Override
public void onDone(String utteranceId) {
Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
}
#Override
public void onError(String utteranceId) {
}
});
}
Please give a solution for this.
The main problems are:
1) Setting the progress listener before the tts is initialized.
2) Trying to make a Toast from a background thread.
I also have some other suggested changes but they are not required:
public class MainActivity extends AppCompatActivity {
String message = "How may I help you?";
String mostRecentUtteranceID;
private TextToSpeech myTTS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(myTTS.getEngines().size() == 0){
Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
}else{
if (status == TextToSpeech.SUCCESS){
ttsInitialized();
}
}
}
});
}
private void ttsInitialized() {
// *** set UtteranceProgressListener AFTER tts is initialized ***
myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
}
#Override
// this method will always called from a background thread.
public void onDone(String utteranceId) {
// only respond to the most recent utterance
if (!utteranceId.equals(mostRecentUtteranceID)) {
Log.i("XXX", "onDone() blocked: utterance ID mismatch.");
return;
} // else continue...
boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);
Log.i("XXX", "was onDone() called on a background thread? : " + wasCalledFromBackgroundThread);
Log.i("XXX", "onDone working.");
// for demonstration only... avoid references to
// MainActivity (unless you use a WeakReference)
// inside the onDone() method, as it
// can cause a memory leak.
runOnUiThread(new Runnable() {
#Override
public void run() {
// *** toast will not work if called from a background thread ***
Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onError(String utteranceId) {
}
});
// set Language
myTTS.setLanguage(Locale.US);
// set unique utterance ID for each utterance
mostRecentUtteranceID = (new Random().nextInt() % 9999999) + ""; // "" is String force
// set params
// *** this method will work for more devices: API 19+ ***
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, mostRecentUtteranceID);
myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params);
}
}
If you want to add the call back OnUtteranceProgressListener you have to implement the speak method like this:
myTTS.speak(message,TextToSpeech.QUEUE_FLUSH, null , TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID);
Then it will call the methods that you've already implemented (onStart, onDone, etc)

Android App does not work on run time taking some time to work, how to solve this issue

I am new in android development and learning android apps development. I have created a very basic and simple Flashlight for android device. I am facing the issue when i run the app it takes some time to run like if i press turn on flash light it will take some time (half sec or less but it take some time), i didn't use wait() method in my app. How to run it really fast like user click on it flash turn on or turn off?
public class MainActivity extends AppCompatActivity {
private ImageButton imageButton;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
private Camera.Parameters params;
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.switch_btn);
//Check that Device has supports flash or not
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash){
//If device does not supports Flash
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your current device does not support to Little Flashy! ops");
alert.setButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Close application
finish();
}
});
alert.show();
return;
}
//Get the Camera
getCamera();
//Display button image
toggleButtonImage();
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
} else
{
turnOnFlash();
}
}
});
}
private void toggleButtonImage() {
if (isFlashOn){
imageButton.setImageResource(R.drawable.btn_switch_on);}
else {imageButton.setImageResource(R.drawable.btn_switch_off);}
}
private void getCamera() {
if (camera == null){
try{
camera = camera.open();
params = camera.getParameters();
}catch (RuntimeException e){
Log.d("Camera Error.", e.getMessage());
}
}
}
/*
* Turning On flash
*/
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
/*
* Turning Off flash
*/
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
private void playSound() {
if (isFlashOn){
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);}
else {
mp= MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
//turn off flash when on Pause called
turnOffFlash();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
if (hasFlash) turnOnFlash();
}
#Override
protected void onStart() {
super.onStart();
getCamera();
}
#Override
protected void onStop() {
super.onStop();
if (camera != null){
camera.release();
camera = null;
}
}
}
Before you turn the flash on and off, you call the playSound method, which uses the MediaPlayer. this method is slow and causes your delay. First try to remove it (by commenting it out) and see the difference. Next, you can try to run it from a thread.
Yes, you can ignore the sound feature for current time. Or if you really want this feature than use it through Thread it will show no lag or delay in your app while user turn on or turn off flash.

Loading multiple AdMob videos

For demonstration purposes, the app has one activity that simply offers this:
You click a button, view a rewarded video, and you are rewarded with whatever.
The Problem
How can I load the videos? From what I have seen you can only call mAd.loadAd() once. There are 3 videos, each with their own AD UNIT ID. Each ad unit can have its own listener, but only one video loads so it doesn't matter...
When trying to load multiple videos
For example:
mAd1.loadAd("AD_UNIT_1", new AdRequest.Builder().build());
mAd2.loadAd("AD_UNIT_2", new AdRequest.Builder().build());
mAd3.loadAd("AD_UNIT_3", new AdRequest.Builder().build());
results in only the last video being loaded and this in log:
W/Ads: Loading already in progress, saving this object for future refreshes.
onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAd1 = MobileAds.getRewardedVideoAdInstance(this);
mAd2 = MobileAds.getRewardedVideoAdInstance(this);
mAd3 = MobileAds.getRewardedVideoAdInstance(this);
listeners...
mAd1.loadAd() etc
}
Thank you for your help
Edit: It's clear I am thinking about this problem wrong. I have 5+ ad zones that each will play a rewarded video and give a different reward (for example, one gives coins, one gives a level up, and so on..). There is no reason to load 5 videos. I should load one in onCreate(), so it's ready when needed, then load it again after the item is rewarded so it's ready for next time.
So the question remains, if there is just the one video, and thus one ad zone, being loaded onCreate() then how can I track what reward to give?
Here's a simple solution...
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
#Override
public void onRewarded(RewardItem rewardItem) {
switch(Constants.currentAd) {
case("REWARD1"):
//do something
Constants.currentAd = "";
break;
case("REWARD2"):
//do something
Constants.currentAd = "";
break;
case("REWARD3"):
//do something
Constants.currentAd = "";
break;
}
}
});
mAd.loadAd("REWARDED_VIDEO_UNIT_ID", new AdRequest.Builder().build());
}
public void showRewardedVideo() {
if (mAd.isLoaded()) {
mAd.show();
}
}
Constants.java
public class Constants {
public static String currentAd = "";
}
Showing the ad after button click
rewardButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Constants.currentAd = "REWARD1";
dismiss();
((MainActivity) getActivity()).showRewardedVideo();
}
});
REWARDED_VIDEO_UNIT_ID is one ad unit for rewarded video in AdMob...remove the rest. No need for other units, you can track whatever you like in the listener.
Other simple soluction...
AbstractRewardVideo.java
public abstract class AbstractRewardVideo {
private RewardedVideoAd mAd;
private String adId = "ca-app-pub...";
private Activity activity;
abstract protected RewardedVideoAdListener getListener();
public void init(Activity activity) {
this.activity = activity;
mAd = MobileAds.getRewardedVideoAdInstance(activity);
setAdId(adId);
loadRewardedVideoAd();
}
public Activity getActivity(){
return this.activity;
}
public void loadRewardedVideoAd() {
mAd.loadAd(adId, new AdRequest.Builder().build());
}
public void showVideo(){
setListener(getListener());
if (mAd.isLoaded()) {
mAd.show();
} else {
Utils.exibirToast("Don't loaded!");
}
}
public void setAdId(#NonNull String id){
this.adId = id;
}
public void setListener(RewardedVideoAdListener listener){
mAd.setRewardedVideoAdListener(listener);
}
}
Reward1.java
public class Reward1 extends AbstractRewardVideo {
public Reward1(Activity activity) {
init(activity);
}
#Override
protected RewardedVideoAdListener getListener() {
return new Listener();
}
private class Listener implements RewardedVideoAdListener {
#Override
public void onRewarded(RewardItem rewardItem) {
//Do something...
}
public void onRewardedVideoAdLoaded() {}
public void onRewardedVideoAdOpened() {}
public void onRewardedVideoStarted() {}
public void onRewardedVideoAdClosed() { loadRewardedVideoAd(); }
public void onRewardedVideoAdLeftApplication() {}
public void onRewardedVideoAdFailedToLoad(int i) {}
}
}
Reward2.java
public class Reward2 extends AbstractRewardVideo {
public Reward2(Activity activity) {
init(activity);
}
#Override
protected RewardedVideoAdListener getListener() {
return new Listener();
}
private class Listener implements RewardedVideoAdListener {
#Override
public void onRewarded(RewardItem rewardItem) {
//Do something...
}
public void onRewardedVideoAdLoaded() {}
public void onRewardedVideoAdOpened() {}
public void onRewardedVideoStarted() {}
public void onRewardedVideoAdClosed() { loadRewardedVideoAd(); }
public void onRewardedVideoAdLeftApplication() {}
public void onRewardedVideoAdFailedToLoad(int i) {}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity{
Reward1 reward1;
Reward2 reward2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reward1 = new Reward1(this);
reward2 = new Reward1(this);
...
reward1.showVideo();
...
reward2.showVideo();
}
}
MobileAds.initialize ( this, "ca-app-pub-4761500786576152~8215465788" );
RewardedVideoAd mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(Video_Ad.this);
}
#Override
public void onRewardedVideoAdLoaded() {
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
#Override
public void onRewardedVideoCompleted() {
}

YouTube API does not play videos as expected

I'm attempting to build a very simple video player using the Youtube API for Android. So far I have one video (which can be clicked and played) and two thumbnails below it - my problem is: I need to figure out how I can (correctly) assign different videos to each thumbnail - then play them.
I've attempted assign the thumbnails so using:
public static final String VIDEO1_ID = "HNtBphqE_LA";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "Ak--L4bYly0";
But for some reason I get the correct thumbnail displayed and video played for both youTubeThumbnailLoader1 and youTubeThumbnailLoader2.
Any suggestions? I simply need to tweak/modify the code below to show unique videos/thumbnails (instead of the same one repeated multiple times as it is now) and play them.
Screenshot
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
youTubeThumbnailView1 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview1);
youTubeThumbnailView1.initialize(API_KEY, this);
youTubeThumbnailView2 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview2);
youTubeThumbnailView2.initialize(API_KEY, this);
youTubeThumbnailView1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
youTubeThumbnailView2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(youTubePlayer != null){
youTubePlayer.play();
}
}});
}
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
youTubePlayer = player;
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult error) {
}
#Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader1 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader1.setVideo(VIDEO1_ID);
youTubeThumbnailLoader2 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader2.setVideo(VIDEO2_ID);
}
private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
#Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
}
#Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
}
}
}
P.S.
I think this may have to do with the fact I'm using youTubeThumbnailView1 and youTubeThumbnailView2 to create multiple thumbnails - however I have a feeling this might not be the correct way of doing so.
You didn't set the right video for your thumbnails. There is no difference inside each thumbnailView clickListener!
try something like this in your onCreate
public void onCreate(Bundle icicle){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
youTubeThumbnailView1 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview1);
youTubeThumbnailView1.initialize(API_KEY, this);
youTubeThumbnailView2 = (YouTubeThumbnailView)findViewById(R.id.youtubethumbnailview2);
youTubeThumbnailView2.initialize(API_KEY, this);
youTubeThumbnailView1.setOnClickListener(new OnClickListener(){
if(youTubePlayer != null){
youTubePlayer.loadVideo(VIDEO1_ID)
youTubePlayer.play();
}
});
youTubeThumbnailView2.setOnClickListener(new OnClickListener(){
if(youTubePlayer != null){
youTubePlayer.loadVideo(VIDEO2_ID)
youTubePlayer.play();
}
});
}
Note: I see a lot of messy stuff in your code.
#Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader1 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader1.setVideo(VIDEO1_ID);
youTubeThumbnailLoader2 = thumbnailLoader;
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader2.setVideo(VIDEO2_ID);
}
This part shows that you lack basic understanding of programming. You could simply just write:
#Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
thumbnailLoader.setVideo(VIDEO1_ID);
thumbnailLoader.setVideo(VIDEO2_ID);
}
which is clearly not what you intended. I guess you have to create different Listeners in your onCreate. Dont pass this inside the initialze method of your thumbnailViews and make the same switch I show you for the clickListener.
Also Try to nail down the problem you have and post only relating stuff.

Categories

Resources