0

I searched similar topic about how to Prevent embedded YouTube Video from playing on Background on android. However I just can not get it to work. Following snippet is method I tried that I got from hereenter link description here

First solution I tried is

@Override
public void onResume()
{
    super.onResume();
    webView.onResume();
}

@Override
public void onPause()
{
    super.onPause();
    webView.onPause();
}

2nd solution I tried is

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(keyCode==event.KEYCODE_BACK)
    {
        mWebview.loadUrl("");
        mWebview.stopLoading();

        finish();

    }
    return super.onKeyDown(keyCode, event);
}

However both of them did not work. It compiled, but since I am new to coding, I got no clue if I applied the code at the right place.

Any help will be so much appreciated.

here is my unmodified java script, before modifying code mentioned above.

public class VideoDetailActivity extends ActionBarActivity {
 
 private FavDbAdapter mDbHelper;
    private Toolbar mToolbar;

 String date;
 String id;
 String title;
 String description;
 String favorite;
 String image;
 
 ImageLoader imageLoader;
 private TextView mPresentation;
 int mImageHeight;
 int latestAlpha;
 ImageView mImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_youtube_detail);
   
   mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
      setSupportActionBar(mToolbar);
      getSupportActionBar().setDisplayShowHomeEnabled(true);
   //getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)));
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   mToolbar.getBackground().setAlpha(0);
   getSupportActionBar().setDisplayShowTitleEnabled(false);
  
   mPresentation = (TextView)findViewById(R.id.youtubetitle);
   TextView detailsDescription = (TextView)findViewById(R.id.youtubedescription);
   TextView detailsPubdate = (TextView)findViewById(R.id.youtubeurl);
   
   imageLoader = Helper.initializeImageLoader(VideoDetailActivity.this);

   Bundle bundle = this.getIntent().getExtras();
    
   mPresentation.setText(bundle.getString("keyTitle"));
       detailsDescription.setText(bundle.getString("keyDescription"));
       detailsPubdate.setText(bundle.getString("keyDate"));
        title = (bundle.getString("keyTitle"));
        id = (bundle.getString("keyId"));
        date = (bundle.getString("keyDate"));
        description = (bundle.getString("keyDescription"));
        favorite = (bundle.getString("keyFavorites"));
        image = (bundle.getString("keyImage"));
        
      if ((getResources().getString(R.string.ad_visibility).equals("0"))){
          // Look up the AdView as a resource and load a request.
          AdView adView = (AdView) findViewById(R.id.adView);
          AdRequest adRequest = new AdRequest.Builder().build();
          adView.loadAd(adRequest);
   }
      
       mImage = (ImageView) findViewById(R.id.image);
       imageLoader.displayImage(image, mImage);
    mImageHeight = mImage.getLayoutParams().height;

  ((TrackingScrollView) findViewById(R.id.scroller)).setOnScrollChangedListener(
    new TrackingScrollView.OnScrollChangedListener() {
     @Override
     public void onScrollChanged(TrackingScrollView source, int l, int t, int oldl, int oldt) {
      handleScroll(source, t);
     }
    }
  );
      
      ImageButton btnPlay= (ImageButton) findViewById(R.id.playbutton);
      btnPlay.bringToFront();
      //Listening to button event
      btnPlay.setOnClickListener(new View.OnClickListener() {

       public void onClick(View arg0) {
        //Intent intent = new Intent(Intent.ACTION_VIEW);
        //intent.setData(Uri.parse("http://sherdle.com/id/"+ video.getId()));
        //startActivity(intent);  
        Intent intent = new Intent(VideoDetailActivity.this, YouTubePlayerActivity.class);
        intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_ID, id);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
       }
     });
      
      Button btnOpen = (Button) findViewById(R.id.openbutton);

      //Listening to button event
      btnOpen.setOnClickListener(new View.OnClickListener() {

       public void onClick(View arg0) {
             try {  
               Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
                  startActivity(intent);                 
                }catch (ActivityNotFoundException ex){
                  Intent intent=new Intent(Intent.ACTION_VIEW, 
                  Uri.parse("http://www.youtube.com/watch?v="+id));
                  startActivity(intent);
              }
       }
     });
      
      Button btnFav = (Button) findViewById(R.id.favoritebutton);
      
    //Listening to button event
      btnFav.setOnClickListener(new View.OnClickListener() {

       public void onClick(View arg0) {
        mDbHelper = new FavDbAdapter(VideoDetailActivity.this);
              mDbHelper.open();
              
              if(mDbHelper.checkEvent(title, description, date, id, "", "", "youtube")) {
                  // Item is new
               mDbHelper.addFavorite(title, description, date, id, "", "", "youtube");
                Toast toast = Toast.makeText(VideoDetailActivity.this, getResources().getString(R.string.favorite_success), Toast.LENGTH_LONG);
                  toast.show();
              } else {
                  Toast toast = Toast.makeText(VideoDetailActivity.this, getResources().getString(R.string.favorite_duplicate), Toast.LENGTH_LONG);
                  toast.show();
              }
       }
     });
   }
    
    private void handleScroll(TrackingScrollView source, int top) {
  int scrolledImageHeight = Math.min(mImageHeight, Math.max(0, top));

  ViewGroup.MarginLayoutParams imageParams = (ViewGroup.MarginLayoutParams) mImage.getLayoutParams();
  int newImageHeight = mImageHeight - scrolledImageHeight;
  if (imageParams.height != newImageHeight) {
   // Transfer image height to margin top
   imageParams.height = newImageHeight;
   imageParams.topMargin = scrolledImageHeight;

   // Invalidate view
   mImage.setLayoutParams(imageParams);
  }
  
  final int imageheaderHeight = mImage.getHeight() - getSupportActionBar().getHeight();
     //t=how far you scrolled
     //ratio is from 0,0.1,0.2,...1
     final float ratio = (float) Math.min(Math.max(top, 0), imageheaderHeight) / imageheaderHeight;
     //setting the new alpha value from 0-255 or transparent to opaque
     final int newAlpha = (int) (ratio * 255);
     
     if (newAlpha != latestAlpha){
      mToolbar.getBackground().setAlpha(newAlpha);
     }
     
     latestAlpha = newAlpha;
 }

 
 @Override
 public void onPause(){
  super.onPause();
  mToolbar.getBackground().setAlpha(255);
 }
 
 @Override
 public void onResume(){
  super.onPause();
  mToolbar.getBackground().setAlpha(latestAlpha);
 }
    
   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
   case android.R.id.home:
            finish();
            return true;
        case R.id.share:
         String applicationName = getResources().getString(R.string.app_name);
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        
        String urlvalue = getResources().getString(R.string.video_share_begin);
        String seenvalue = getResources().getString(R.string.video_share_middle);
        String appvalue = getResources().getString(R.string.video_share_end);
                                               //this is the text that will be shared
        sendIntent.putExtra(Intent.EXTRA_TEXT, (urlvalue+"http://youtube.com/watch?"+id+seenvalue+applicationName+appvalue));
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, title); //you can replace title with a string of your choice
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.share_header)));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.youtube_detail_menu, menu);
      return true;
  }
  
}
Community
  • 1
  • 1
Jay
  • 1

1 Answers1

0

Have you tried to load a dummy URL instead of an empty one?

mWebview.loadUrl("data://dummy");

This worked for me ;)

bonnyz
  • 13,458
  • 5
  • 46
  • 70