Android Issue In YouTubeAndroidPlayerAPI.? - android

Hi facing issue in YouTubeAndroidPlayerAPI, here is the error i'm facing
W/YouTubeAndroidPlayerAPI: Forcefully created overlay:acqm#cada64
helper:Lazy#241a4cd view:null status: ....... {...}
NOTE - i have two activity class if user enter FirstActivity video is working fine. If user change activity from FirstActivity to SecondActivity in SecondActivity also video working fine, if user using system back button navigate to FirstActivity video not playing continuously loading happening but video not playing.(Added image for reference)
loading same Activity if user clicking button like this(FirstActivity and SecondActivity are same in my case, just for under standing purpose explained two activityes)
Intent intent = new Intent(YoutubeVideoPlay.this, YoutubeVideoPlay.class);
startActivity(intent);
Here is the code sample i'm using
FirstActivity.java
public class FirstActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String DEVELOPER_KEY = "xxxxxxxxxxxxxxxxxxx";
private static final int RECOVERY_DIALOG_REQUEST = 1;
private String url = "xxxxxxxxxxxxxxxxxxxx";
private String VIDEO_ID;
private YouTubePlayerFragment myYouTubePlayerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube);
myYouTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
myYouTubePlayerFragment.initialize(DEVELOPER_KEY, this);
VIDEO_ID = getYoutubeVideoId(url);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity .this, FirstActivity .class);
startActivity(intent);
}
});
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format("There was an error initializing the YouTubePlayer (%1$s)",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DEVELOPER_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtubeplayerfragment);
}
public static String getYoutubeVideoId(String youtubeUrl) {
String video_id = "";
if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http")) {
String expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
CharSequence input = youtubeUrl;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
String groupIndex1 = matcher.group(7);
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
}
return video_id;
}
}

yes , youtube main single global player if you start new player ,previous player will release automatically.
So you need restart your youtube player again when resume your activity

Related

Youtube Video 400 network Error when try to play video

I am try to play video using youtube video API
When I put static ID means declare ID in file at that time code is working.
But when I try to fetch ID from getIntent() I also got ID but video not play.
I got There was problem with network.
Below is my code. Its working in this condition.
this is Video_Activity.java
public class Video_Activity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
TextView textView1, textView2;
ScrollView scrollView;
WebView webView;
VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
private YouTubePlayerView youTubeView;
private static final int RECOVERY_DIALOG_REQUEST = 1;
Video video;
public String videourl;
// Google Console APIs developer key
// Replace this key with your's
String YOUTUBE_VIDEO_CODE;
String DEVELOPER_KEY;
//String Video_ids;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_);
DEVELOPER_KEY = "AIzaSyC0rjDLc0jkYY0Z66QgOnkNmkOAyOqpqnI";
// YouTube video id
YOUTUBE_VIDEO_CODE = "yExScIwJftE";
getSupportActionBar().setTitle(Html.fromHtml("<font color='#fdfafa'> Video View </font>"));
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
YouTubePlayerSupportFragment frag = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_view);
if (frag == null) {
frag = YouTubePlayerSupportFragment.newInstance();
getSupportFragmentManager().beginTransaction().add(R.id.youtube_view, frag).commit();
}
frag.initialize(DEVELOPER_KEY, this);
Intent intent = getIntent();
if (null != intent) {
String Video_Title = intent.getStringExtra("Video_title");
String Video_Desc = intent.getStringExtra("Vide_desc");
//Video_ids = intent.getStringExtra("Video_ID");
//Log.i("Video_Url", " " + YouTube_VideoPlayer);
textView1 = (TextView) findViewById(R.id.activity_video_Title);
textView1.setText(Video_Title);
textView2 = (TextView) findViewById(R.id.activity_video_desc);
textView2.setText(Video_Desc);
scrollView = (ScrollView) findViewById(R.id.Scroller_Id_video);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.smoothScrollTo(0, textView2.getBottom());
}
});
}
}
#Override
public void onInitializationFailure (YouTubePlayer.Provider provider,YouTubeInitializationResult errorReason){
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
}
else
{
//String YouTube_VidePlayer = getIntent().getStringExtra("Video_Player");
String YouTube_VideoPlayer = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, YouTube_VideoPlayer, Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess (YouTubePlayer.Provider provider,YouTubePlayer player,boolean wasRestored){
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.cueVideo(YOUTUBE_VIDEO_CODE);
//player.cueVideo(Video_ids);
// Hiding player controls
//player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
}
}
#Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Video_Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Now when i Used
public class Video_Activity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
TextView textView1, textView2;
ScrollView scrollView;
WebView webView;
VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
private YouTubePlayerView youTubeView;
private static final int RECOVERY_DIALOG_REQUEST = 1;
Video video;
public String videourl;
// Google Console APIs developer key
// Replace this key with your's
public static final String DEVELOPER_KEY = "AIzaSyC0rjDLc0jkYY0Z66QgOnkNmkOAyOqpqnI";
// YouTube video id
//public static final String YOUTUBE_VIDEO_CODE = "yExScIwJftE";
String Video_ids;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#fdfafa'> Video View </font>"));
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
YouTubePlayerSupportFragment frag = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_view);
if (frag == null) {
frag = YouTubePlayerSupportFragment.newInstance();
getSupportFragmentManager().beginTransaction().add(R.id.youtube_view, frag).commit();
}
frag.initialize(DEVELOPER_KEY, this);
Intent intent = getIntent();
if (null != intent) {
String Video_Title = intent.getStringExtra("Video_title");
String Video_Desc = intent.getStringExtra("Vide_desc");
Video_ids = intent.getStringExtra("Video_ID");
//Log.i("Video_Url", " " + YouTube_VideoPlayer);
textView1 = (TextView) findViewById(R.id.activity_video_Title);
textView1.setText(Video_Title);
textView2 = (TextView) findViewById(R.id.activity_video_desc);
textView2.setText(Video_Desc);
scrollView = (ScrollView) findViewById(R.id.Scroller_Id_video);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.smoothScrollTo(0, textView2.getBottom());
}
});
}
}
#Override
public void onInitializationFailure (YouTubePlayer.Provider provider,YouTubeInitializationResult errorReason){
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
}
else
{
//String YouTube_VidePlayer = getIntent().getStringExtra("Video_Player");
String YouTube_VideoPlayer = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, YouTube_VideoPlayer, Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess (YouTubePlayer.Provider provider,YouTubePlayer player,boolean wasRestored){
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
//player.cueVideo(YouTube_VideoPlayer);
player.cueVideo(Video_ids);
// Hiding player controls
//player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
}
}
#Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Video_Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
YouTube id extraction is actually a different question but since this can solve your problem I am answering this. I usually do like this and it's working.
public void String getYoutubeId(String ytUrl)
{
String youtubeID ="";
if (ytUrl.contains("://youtu.be/")){
youtubeID=ytUrl.substring(ytUrl.lastIndexOf("/") + 1);
ytUrl="http://youtube.com/watch?v=" + youtubeID;
}else if (ytUrl.contains("watch?v=")){
if (ytUrl.contains("&")){
ytUrl=ytUrl.substring(0, ytUrl.indexOf('&'));
}
if (ytUrl.contains("https")){
ytUrl.replace("https", "http");
}
youtubeID=ytUrl.substring(ytUrl.indexOf("watch?v=") + 8);
}
return youtubeID;
}
Call getYoutubeId(youtube url) to get youtube id. Also print the id and compare it with the url you are passing and see if both matches.

GET YOUTUBE KEY_VIDEO_ID

How to get youtube key_vedio_id please help me to find-out this problem.
This is my code please and correct me if i wrong any where.
public class YouTubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
public static final String KEY_VIDEO_ID = "KEY_VIDEO_ID";
private String mVideoId;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_youtube);
final Bundle arguments = getIntent().getExtras();
if (arguments != null && arguments.containsKey(KEY_VIDEO_ID)) {
mVideoId = arguments.getString(KEY_VIDEO_ID);
}
final YouTubePlayerView playerView = (YouTubePlayerView) findViewById(R.id.youTubePlayerView);
playerView.initialize(getString(R.string.DEVELOPER_KEY_YOU_TUBE), this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean restored) {
//Here we can set some flags on the player
//This flag tells the player to switch to landscape when in fullscreen, it will also return to portrait
//when leaving fullscreen
youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
//This flag tells the player to automatically enter fullscreen when in landscape. Since we don't have
//landscape layout for this activity, this is a good way to allow the user rotate the video player.
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
//This flag controls the system UI such as the status and navigation bar, hiding and showing them
//alongside the player UI
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if (mVideoId != null) {
if (restored) {
youTubePlayer.play();
} else {
youTubePlayer.loadVideo(mVideoId);
}
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
//Handle the failure
Toast.makeText(this, R.string.error_init_failure, Toast.LENGTH_LONG).show();
}
}
}

list youtube playlist from my playlist with ID PLSt1J_r1AmrzOUCGZ6sWNxvOePK4nOwIk into my android application

List youtube playlist from my playlist with ID PLSt1J_r1AmrzOUCGZ6sWNxvOePK4nOwIk into my android application but it is showing one video at a time but on want it to show the whole list of videos before I play any video.
My main activity code
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{
public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String PlayList_ID = "PLP7qPet500dfglA7FFTxBmB_snxCaMHDJ";
private YouTubePlayer youTubePlayer;
private YouTubePlayerFragment youTubePlayerFragment;
private TextView textVideoLog;
private Button btnViewFullScreen;
private static final int RQS_ErrorDialog = 1;
String log = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
youTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager()
.findFragmentById(R.id.youtubeplayerfragment);
youTubePlayerFragment.initialize(API_KEY, this);
textVideoLog = (TextView)findViewById(R.id.videolog);
btnViewFullScreen = (Button)findViewById(R.id.btnviewfullscreen);
btnViewFullScreen.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
youTubePlayer.setFullscreen(true);
}});
}
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
if (result.isUserRecoverableError()) {
result.getErrorDialog(this, RQS_ErrorDialog).show();
} else {
Toast.makeText(this,
"YouTubePlayer.onInitializationFailure(): " + result.toString(),
Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
youTubePlayer = player;
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationSuccess()",
Toast.LENGTH_LONG).show();
if (!wasRestored) {
//player.cueVideo(VIDEO_ID);
player.cuePlaylist(PlayList_ID);
}
}
}
The playlist id, are the characters after "PL".
You can use recyclerView for the list. I have done this using Youtube api rest calls. You will need to get a browser key from the google developer console. I've explained this in my answer here:
https://stackoverflow.com/a/41201084/3689744

app resets after restarting

I am trying to develop a simple app that when I buy an activity it makes the button enabled.
My code works fine but the problem is when I exit the app and reopen it, the buttons that was previously enabled after the purchase they become disabled however it doesn't allow to repurchase again.
(I need to do it on time purchase)
So how to keep the enabled buttons stays enabled when I restart the app?
Here is my code:
public class MainScreen extends ActionBarActivity {
private static final String TAG = "com.aseng90_test.smiap2";
IabHelper mHelper;
static final String ITEM_SKU = "com.aseng90_test.smiap2_button5";
static final String ITEM_SKU2 = "com.aseng90_test.smiap2_buyact2";
static final String ITEM_SKU3 = "com.aseng90_test.smiap2_buyact3";
static final String ITEM_SKU4 = "com.aseng90_test.smiap2_buyall";
public Button Activity1;
public Button Activity2;
public Button Activity3;
public Button buyButton;
public Button buyAct2;
public Button buyAct3;
public Button buyAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
buyButton = (Button) findViewById(R.id.buyButton);
buyAct2 = (Button) findViewById(R.id.buyact2);
buyAct3 = (Button) findViewById(R.id.buyact3);
buyAll = (Button) findViewById(R.id.buyall);
Activity1 = (Button) findViewById(R.id.act1);
Activity2 = (Button) findViewById(R.id.act2);
Activity3 = (Button) findViewById(R.id.act3);
Activity1.setEnabled(false);
Activity2.setEnabled(false);
Activity3.setEnabled(false);
String base64EncodedPublicKey =
"";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new
IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " +
result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
}
});
}
public void buyClick(View view) {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "mypurchasetoken");
}
public void buyAct2 (View view){
mHelper.launchPurchaseFlow(this, ITEM_SKU2, 10002, mPurchaseFinishedListener, "buyact2");
}
public void buyAct3 (View view){
mHelper.launchPurchaseFlow(this,ITEM_SKU3, 10003, mPurchaseFinishedListener, "buyact3");
}
public void buyAll (View view){
mHelper.launchPurchaseFlow(this, ITEM_SKU4,10004, mPurchaseFinishedListener, "buyall");
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase)
{
if (result.isFailure()) {
// Handle error
return;
}
if (purchase.getSku().equals(ITEM_SKU)) {
Activity1.setEnabled(true);
buyButton.setEnabled(false);
}
if (purchase.getSku().equals(ITEM_SKU2)){
Activity2.setEnabled(true);
buyAct2.setEnabled(false);
}
if (purchase.getSku().equals(ITEM_SKU3)){
Activity3.setEnabled(true);
buyAct3.setEnabled(false);
}
if (purchase.getSku().equals(ITEM_SKU4)) {
Activity1.setEnabled(true);
Activity2.setEnabled(true);
Activity3.setEnabled(true);
buyAll.setEnabled(false);
buyButton.setEnabled(false);
buyAct2.setEnabled(false);
buyAct3.setEnabled(false);
}
}
};
public void Activity1 (View view)
{
startActivity(new Intent(MainScreen.this, Click1.class));
}
public void Activity2 (View view){
startActivity(new Intent(MainScreen.this, Activity2.class));
}
public void Activity3 (View view){
startActivity(new Intent(MainScreen.this, Activity3.class));
}
#Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null ) mHelper.dispose();
mHelper = null ;
}
Thanks a lot
Ok so I used shared preference to save my activity state but it only works fine when I try to enable a button with another free button, but it doesn't work with the in app purchase button (simply it let me do the purchase but the deactivated button never turns enabled after the purchase) So I don't know if there is a conflict between in app purchase and shared preference in my code?
Here is my edited code:
package com.aseng90_test.smiap2;
import android.content.Intent; import
android.content.SharedPreferences; import
android.support.v7.app.ActionBarActivity; import android.os.Bundle;
import android.util.Log; import android.view.View; import
android.widget.Button; import android.widget.Toast;
import com.aseng90_test.smiap2.util.IabHelper; import
com.aseng90_test.smiap2.util.IabResult; import
com.aseng90_test.smiap2.util.Purchase;
public class MainScreen extends ActionBarActivity {
private static final String TAG = "com.aseng90_test.smiap2";
IabHelper mHelper;
static final String ITEM_SKU = "com.aseng90_test.smiap2_button55";
static final String ITEM_SKU2 = "com.aseng90_test.smiap2_buyact22";
static final String ITEM_SKU3 = "com.aseng90_test.smiap2_buyact33";
static final String ITEM_SKU4 = "com.aseng90_test.smiap2_buyall_1";
private Button Activity1;
private Button Activity2;
private Button Activity3;
private Button buyButton;
private Button buyAct2;
private Button buyAct3;
private Button buyAll;
private Button EAct4; private Button Act4;
private SharedPreferences prefs;
private String prefName = "MyPref";
boolean Activity1_isEnabled;
boolean Activity2_isEnabled;
boolean Activity3_isEnabled;
boolean Act4_isEnabled;
boolean buyButton_isEnabled;
boolean buyAct2_isEnabled;
boolean buyAct3_isEnabled;
boolean buyAll_isEnabled;
boolean EAct4_isEnabled;
private static final String Activity1_state = "Activity1_state";
private static final String Activity2_State = "Activity2_state";
private static final String Activity3_State = "Activity3_state";
private static final String buyButton_State = "buyButton_state";
private static final String buyAct2_State = "buyAct2_state";
private static final String buyAct3_State = "buyAct3_state";
private static final String buyAll_State = "buyAll_state";
private static final String Act4_State = "Act4_state";
private static final String EAct4_State = "EAct4_state";
public void buyClick(View view) {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "mypurchasetoken");
}
public void buyAct2 (View view){
mHelper.launchPurchaseFlow(this, ITEM_SKU2, 10002, mPurchaseFinishedListener, "buyact2");
}
public void buyAct3 (View view){
mHelper.launchPurchaseFlow(this,ITEM_SKU3, 10003, mPurchaseFinishedListener, "buyact3");
}
public void buyAll (View view){
mHelper.launchPurchaseFlow(this, ITEM_SKU4,10004, mPurchaseFinishedListener, "buyall");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
buyButton = (Button) findViewById(R.id.buyButton);
buyAct2 = (Button) findViewById(R.id.buyact2);
buyAct3 = (Button) findViewById(R.id.buyact3);
buyAll = (Button) findViewById(R.id.buyall);
Activity1 = (Button) findViewById(R.id.act1);
Activity2 = (Button) findViewById(R.id.act2);
Activity3 = (Button) findViewById(R.id.act3);
EAct4 = (Button) findViewById(R.id.eact4);
Act4 = (Button) findViewById(R.id.act4);
String base64EncodedPublicKey =
"";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new
IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " + result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
}
});
}
public void EACT4 (View view) {
EAct4.setEnabled(false);
Act4.setEnabled(true);
}
public void ACT4 (View view){
Toast.makeText(MainScreen.this,
"ACt4 Clicked", Toast.LENGTH_LONG).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase)
{
if (result.isFailure()) {
// Handle error
return;
}
if (purchase.getSku().equals(ITEM_SKU)) {
Activity1.setEnabled(true);
buyButton.setEnabled(false);
}
if (purchase.getSku().equals(ITEM_SKU2)){
Activity2.setEnabled(true);
buyAct2.setEnabled(false);
}
if (purchase.getSku().equals(ITEM_SKU3)){
Activity3.setEnabled(true);
buyAct3.setEnabled(false);
}
if (purchase.getSku().equals(ITEM_SKU4)) {
Activity1.setEnabled(true);
Activity2.setEnabled(true);
Activity3.setEnabled(true);
buyAll.setEnabled(false);
buyButton.setEnabled(false);
buyAct2.setEnabled(false);
buyAct3.setEnabled(false);
}
}
};
public void Activity1 (View view)
{
startActivity(new Intent(MainScreen.this, Click1.class));
}
public void Activity2 (View view){
startActivity(new Intent(MainScreen.this, Activity2.class));
}
public void Activity3 (View view){
startActivity(new Intent(MainScreen.this, Activity3.class));
}
#Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null ) mHelper.dispose();
mHelper = null ;
}
#Override
protected void onPause(){
super.onPause();
if (Act4.isEnabled()){
Act4_isEnabled = true;
}
else {
Act4_isEnabled = false;
}
if (Activity1.isEnabled()){
Activity1_isEnabled = true;
}
else {
Activity1_isEnabled = false;
}
if (Activity2.isEnabled()){
Activity2_isEnabled = true;
}
else {
Activity2_isEnabled = false;
}
if (Activity3.isEnabled()){
Activity3_isEnabled = true;
}
else {
Activity3_isEnabled = false;
}
if (buyButton.isEnabled()){
buyButton_isEnabled = true;
}
else {
buyButton_isEnabled = false;
}
if (buyAct2.isEnabled()){
buyAct2_isEnabled = true;
}
else {
buyAct2_isEnabled = false;
}
if (buyAct3.isEnabled()){
buyAct3_isEnabled = true;
}
else {
buyAct3_isEnabled = false;
}
if (buyAll.isEnabled()){
buyAll_isEnabled = true;
}
else {
buyAll_isEnabled = false;
}
prefs = getSharedPreferences(prefName,MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(Act4_State,Act4_isEnabled);
editor.putBoolean(EAct4_State,EAct4_isEnabled);
editor.putBoolean(Activity1_state,Activity1_isEnabled);
editor.putBoolean(Activity2_State,Activity2_isEnabled);
editor.putBoolean(Activity3_State,Activity3_isEnabled);
editor.putBoolean(buyButton_State,buyButton_isEnabled);
editor.putBoolean(buyAct2_State,buyAct2_isEnabled);
editor.putBoolean(buyAct3_State,buyAct3_isEnabled);
editor.putBoolean(buyAll_State,buyAll_isEnabled);
editor.apply();
}
#Override
protected void onResume(){
super.onResume();
prefs = getSharedPreferences(prefName,MODE_PRIVATE);
Act4.setEnabled(prefs.getBoolean(Act4_State,false));
Activity1.setEnabled(prefs.getBoolean(Activity1_state,false));
Activity2.setEnabled(prefs.getBoolean(Activity2_State,false));
Activity3.setEnabled(prefs.getBoolean(Activity3_State,false));
EAct4.setEnabled(prefs.getBoolean(EAct4_State,true));
buyButton.setEnabled(prefs.getBoolean(buyButton_State,true));
buyAct2.setEnabled(prefs.getBoolean(buyAct2_State,true));
buyAct3.setEnabled(prefs.getBoolean(buyAct3_State,true));
buyAll.setEnabled(prefs.getBoolean(buyAll_State,true));
}
Thanks again
When your activity is restarted, it does not save the state of its views from the previous instance. Thus you probably need to save your state data somewhere so that when you restart your activity, you can then reset your views looking at this saved state data.You can make use of "SharedPreferences" class. SharedPreferences lets you store information in key-value pair. I guess that serves your purpose.
SharedPreferences pref = getApplicationContext().getSharedPreferences("ProductCache", 0);
// 0 - for private mode
//First parameter is the name of your preference file.Could be "ProductCache" in your case.
// Default 0 for private access.
SharedPreferences.Editor editor = pref.edit(); // edit your preference file
editor.putString("KEY","VALUE");//save strings
editor.putBoolean("KEY",true);//save booleans or any other type of data.
editor.commit(); //commit your changes
You can set that your product is already bought in your mPurchaseFinishedListener.Next time your app is opened you can check the same file by using the same "keys" that you used to store information to check if the product is already bought and then you can set your views accordingly.
If this doesn't serve your purpose, then you might have to start implementing a SQLite database that keeps track of all purchases. Please go through this article on Android Developers as it might help you further:
http://developer.android.com/training/basics/data-storage/index.html
Hope this helps. Cheers!
Use CheckOut library it does everything for you, you can check for the purchased items and based on the sku you can enable or disable purchase button.

which file to render and play in android youtube player

I am successfully integrate YouTube Android Player API for my apps. I am curious to know about which file is rendered in the player. Every single video file may have several formats like 320dpi,720dpi & etc.
If my users, is in lower bandwidth; can i choose the file format or the API automatically detects which version would be played in that situation and vice-verse. My code:
public class YoutubeVideoActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, YouTubePlayer.OnFullscreenListener {
Activity activity = YoutubeVideoActivity.this;
public static final String API_KEY = "AIzaSyDN6Q9Pv4seQZqIcjB*********Po5k";
// public static final String VIDEO_ID = "psY0Botpi84";
public String new_id;
private boolean fullscreen;
private YouTubePlayerView playerView;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_youtube_video);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent intent = getIntent();
String video_link = intent.getExtras().getString("video_link");
try {
new_id = video_link.substring("http://www.youtube.com/watch?v="
.length());
if (new_id.equals("")) {
IndepententTVUtils.showCustomAlert(activity,
"Data is not availble. Press back key",
R.drawable.ic_new_launcher);
} else {
playerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
playerView.initialize(API_KEY, this);
}
} catch (Exception e) {
IndepententTVUtils.showCustomAlert(activity,
"Data is not availble", R.drawable.ic_new_launcher);
}
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
Toast.makeText(getApplicationContext(),
"To See this Video, Install Latest YouTube Application",
Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
boolean wasRestored) {
player.setOnFullscreenListener(this);
if (!wasRestored && new_id != null) {
player.cueVideo(new_id);
}
}
#Override
public void onFullscreen(boolean isFullscreen) {
fullscreen = isFullscreen;
}
}
The player sets video quality automatically according to the users bandwidth or internet speed, if it was not so there would be an option specifying the setPlaybackQuality in player.
There are only restricted options to change the player settings that are
here

Categories

Resources