Saturday, 17 August 2013

Integrate tumblr in your App

Hello All,
  
     Today i have do some R&D on Tumblr (blogging platform that allows users to post text, images, videos, links, quotes and audio to their tumblelog) also integrate in my android app. So i would like to share my knowledge with you all. Its very easy to integrate tumblr in your android app. Site also have provided very good documentation in their developer forum.Below link provides the full documentation and api console to test your request easily.

    http://www.tumblr.com/developers

First of all you need to register your app on below link.

    http://www.tumblr.com/oauth/register

Now to use tumblr API in your android application you need library of tumblr. You can download the java project and build jar file form below link. You can also download direct jar file form link.

    https://github.com/tumblr/jumblr

Put this jar file in your lib folder in android project. And build it. Now you can access JumblrClient class to call tumblr api. Some request of this api require access token because they use OAuth 1.0.So you need access token to call some of their request.

Below snippets code shows how to made connection with api and get retrieve logged in user basic information.

// Authenticate via OAuth
  JumblrClient client = new JumblrClient("consumer key", "consumer secret");
  client.setToken("access token", "token secret");

// Make the request
  User user = client.user();
  Log.e("USER", "" + user.getName());
  Log.e("USER", "" + user.getDefaultPostFormat());
  Log.e("USER", "" + user.toString());
  Log.e("USER", "" + user.getFollowingCount());
  Log.e("USER", "" + user.getLikeCount());

Now after successfully getting user basic information try to get your blogs information  using below code.

// Make request to api
  Blog blog = client.blogInfo("your blog name"); // it is something like david.tumblr.com
  Log.e("USER", "" + blog.avatar());
  Log.e("USER", "" + blog.getDescription());
  Log.e("USER", "" + blog.getName());
  Log.e("USER", "" + blog.getTitle());
 
  Below code show how to post simple text post on your blog. To post text on your blog you require to authorize your app which is done by code shown above with access token and token secret.
// Upload new text post
  Map detail = new HashMap();
  detail.put("title", "test from mobile app");
  detail.put("body", "This is a test to upload new post on my blog.");
  try {
   client.postCreate("bhargav3132.tumblr.com", detail);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Okey now you can explore more from here and share or create blog from your android app.

 Cheers!!!

Wednesday, 10 July 2013

Android Audio File List With Play and Progress

Hello All,

     Today i am going to discuss how to get all the audio files available in android device and view in list with play button. This blog also contain how to play particular file with progress update.

To get the list of available audio file we need to call content resolver to get all the file. managedQuery() is now deprecated from api level 11. So we can also user CursorLoader class to fetch the list of audio files from sd card. There are also other way to achieve this.

// fetch data for audio files available in sd card
// Use this below api level 11

//  Cursor mCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media._ID,

//    MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE }, AudioColumns.IS_MUSIC + "!=0", null, "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

// User this above api level 11

  CursorLoader cursorLoader=new CursorLoader(AudioFileListActivity.this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media._ID,

    MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE }, AudioColumns.IS_MUSIC + "!=0", null, null);

Cursor mCursor=cursorLoader.loadInBackground();

After getting all list of audio files set adapter to list view. Make custom list view with play and multi-select facilities.
Now create new media player object.
private MediaPlayer mp = new MediaPlayer();
to start playing do some code as shown below.
mp.reset();
mp.setDataSource(AudioFileListActivity.this, Uri.parse(filePath));
mp.prepare();
mp.start();

To show progress on seek bar create one broadcast receiver and send broadcast the current progress through thread every second.
/**
  * BroadCastReceiver for showing progress of audio file
  * 
  */
 BroadcastReceiver mReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {

   if (intent.getExtras() != null) {
    int currentProgress = intent.getIntExtra("play_position", 0);
    tvTimeReached.setText(mediaTime((long) currentProgress));
    sbPlayProgress.setProgress(currentProgress);
   }

  }
 };

/**
  * (non-Javadoc)
  * 
  * @see java.lang.Runnable#run()
  */
 @Override
 public void run() {

  int currentPosition = 0;
  int total = mp.getDuration();

  while (mp.isPlaying() && currentPosition < total) {
   currentPosition = mp.getCurrentPosition();

   // broadcast the current progress
   callBroadcast(currentPosition);
  }

 }

 /**
  * Call to BroadCast the audio files current position
  * 
  * @param currentPosition
  */
 private void callBroadcast(int currentPosition) {

  Intent intent = new Intent();
  intent.putExtra("play_position", currentPosition);
  intent.setAction(BROADCAST_CURRENT_POSITION);
  sendBroadcast(intent);

 }


This is the simple demo of getting available audio file in sd card and show in list with play and progress facility. You can download the full source code from here.

Cheers...