All Things Techie With Huge, Unstructured, Intuitive Leaps
Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Android Media Player Snippet


I recently tested a bit of code with an Android media player .  The Java code is quite simple.  Toss a media player in layout.xml, import an mp4 video into the raw folder,  and then put the following code in your onCreate() method in the activity:

MediaController mediaController= new MediaController(this);
   mediaController.setAnchorView(contentView);        
   Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video_name);        
   ((VideoView) contentView).setMediaController(mediaController);
   ((VideoView) contentView).setVideoURI(uri);        
   contentView.requestFocus();

   ((VideoView) contentView).start();

Hope this helps someone.

Android Source Code - Turn Off Media Player When You Click Back Button


Android persists activities even when you don't want them unless you explicitly kill them.  Sometimes the media player persists even if you kill the first activity of the app.  If you have an activity that kicks a media player, and you want to turn that media player off from other activities, including hitting the back button, you must do the following:

1) Make the media player into a static class.  The following code should do it:

package com.me.coderzen;

import android.content.Context;
import android.media.MediaPlayer;

public class StaticPlayer {
public static MediaPlayer player;
    public static void SoundPlayer(Context ctx,int raw_id){
            player = MediaPlayer.create(ctx, raw_id);
            player.setLooping(false); 
            player.setVolume(100, 100);
        }

}

On you media player activity you kick the player:

StaticPlayer.SoundPlayer(this, R.raw.sound_file);

Then to kill it from the back button, you override the back button function:

  @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
       if (keyCode == KeyEvent.KEYCODE_BACK ) {
      if (StaticPlayer.player != null)
      StaticPlayer.player.stop();
           this.finish();
           
       }
       return super.onKeyDown(keyCode, event);
   }

I know that there is a newer, easier back button override, but this one works in my code.

And if you have a quit button in your menu, just put the stop in for the StaticPlayer before you call the this.finish().

Hope this helps someone.

Android Development Call A New Activity From ItemDetailFragment

I had to quickly look this up, so others might too.  How do you call a new activity from a fragment or the ItemDetailFragment?  Get the parent.  Like this:

Intent myIntent = new Intent(getActivity(), NameYourClass.class);
getActivity().startActivity(myIntent);
getActivity().startActivity(myIntent);



Hope this helps someone.

Android App Development -- Getting HTTP Post to Work

This could be titled: "How To Make HTTP Get Request To Server - Android Example", or it could be titled "Android HTTP Post Error", or it could be titled "How to overcome a stubborn exception trying to programatically make a call to a server".  Or it could simply be called "HTTP Nut Crush".  OK, kiddie scripters, the internet is full of examples how to use an http client in Android to post to a server -- right?  Right!

Here's the code:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.wasteTime.com?data=crap")
try {    
      // Execute HTTP Post Request  
      HttpResponse response = httpclient.execute(httppost);  
      Log.w("HTTP Response", response.getStatusLine().toString());

    } catch (Exception e) { 

    }

 Piece of cake.  However the f*&K*ing thing doesn't work.  I tried putting in Log.w("Message", e.getMessage()) in the catch block, and it threw a Null Pointer exception.  Get stacktrace didn't work.
Finally in frustration, I put in e.printStackTrace() and I got this jolly frigging error:

02-14 14:50:53.440: W/System.err(10005): android.os.NetworkOnMainThreadException
02-14 14:50:53.440: W/System.err(10005): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)

Obviously I was violating some new turdlet policy that wouldn't let me make the call to the server. 

So what to do?

It turns out that the answer was simple.  You cannot do networking stuff on the main UI thread in Honeycomb SDK and higher.  To fix it, you have to kick a new thread.  Here is the working code.

new Thread(new Runnable() {

 public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.wasteTime.com?data=crap")
try {    
      // Execute HTTP Post Request  
      HttpResponse response = httpclient.execute(httppost);  
      Log.w("HTTP Response", response.getStatusLine().toString());

    } catch (Exception e) {  e.printStackTrace();
}
}
 }).start();

Hope this helps someone.