title: Android: Display current track Info in mediaplayer using webpage
body: <p>I am a learner and developing an Android App to play live stream. It is working properly except 1 issue. I want to show title of currently playing Track which is available on a webpge. For this purpose I have used a textView and tried to use Asynchronous Task and called a method containing a Runnable in that Asynchronous task with which I can update text of textView at an interval on 30 seconds.In runnable I called a method which loads a webpage and give its content as a string.</p>
  
  <p><strong>Problem: When it tries to update text of textView, app crashes.</strong> THANKS IN ADVANCE.</p>
  
  <p><strong>Following is my code:</strong></p>
  
  <pre><code>import java.io.IOException;
  import android.app.Activity;
  import android.media.AudioManager;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.Toast;
  import android.app.ProgressDialog;
  import android.media.MediaPlayer.OnPreparedListener;
  import android.os.Handler;
  import java.net.URLConnection;
  import java.net.URL;
  import java.io.InputStream;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import android.util.Log;
  import java.net.MalformedURLException;
  import android.widget.TextView;
  import android.os.AsyncTask;
  
  public class MusicAndroidActivity extends Activity {
  
  
  
  static MediaPlayer mPlayer;
      Button buttonPlay;
      Button buttonStop;
      Button buttonPause;
      public TextView txtMessage;
      private StringBuilder response;
      private String text="***";
      ProgressDialog progDailog;
      private final static int INTERVAL = 1000 * 30; //2 minutes
      Handler mHandler;
      int length=0;
      String url = "http://www.s8.voscast.com:9630/;stream.mp3";
      private final Handler handler = new Handler();
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          buttonStop = (Button) findViewById(R.id.stop);
          txtMessage=(TextView)findViewById(R.id.txtMessage);
          buttonStop.setEnabled(false);
          buttonPause=(Button) findViewById(R.id.pause);
          buttonPause.setEnabled(false);
          buttonPlay = (Button) findViewById(R.id.play);
  
          buttonPlay.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  mPlayer = new MediaPlayer();
                  mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  try {
                      buttonPlay.setEnabled(false);
                      buttonStop.setEnabled(true);
                      buttonPause.setEnabled(true);
                      //txtMessage.setText("Loading...");
                      progDailog = ProgressDialog.show(MusicAndroidActivity.this, "", "Buffering ... \n It can take upto 1 Minute, depending upon your internet speed", true);
                      mPlayer.setDataSource(url);
                  } catch (IllegalArgumentException e) {
                      Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                  } catch (SecurityException e) {
                      Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                  } catch (IllegalStateException e) {
                      Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  try {
  
                      mPlayer.setOnPreparedListener(new OnPreparedListener() {
  
                          public void onPrepared(MediaPlayer mPlayer) {
                              // TODO Auto-generated method stub
                              mPlayer.start();
                              progDailog.dismiss();
                              LoadWebPageASYNC task = new LoadWebPageASYNC();
                              task.execute(new String[]{"http://khilare.com/swltest/sms.html"});
  
                          }
                      });
                      mPlayer.prepareAsync();
                  } catch (IllegalStateException e) {
                      Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                  } //catch (IOException e) {
                  //Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                  //}
  
              }
  
          });
  
  
  
          buttonStop.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  if(mPlayer!=null &amp;&amp; mPlayer.isPlaying()){
                      mPlayer.stop();
                      buttonPlay.setEnabled(true);
                      buttonStop.setEnabled(false);
                      buttonPause.setEnabled(false);
                  }
              }
          });
  
          buttonPause.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  if(mPlayer!=null &amp;&amp; mPlayer.isPlaying()){
                      mPlayer.pause();
                      length=mPlayer.getCurrentPosition();
                      buttonPause.setText("Resume");
                      //buttonPlay.setEnabled(true);
                      buttonStop.setEnabled(true);
                      buttonPlay.setEnabled(false);
                      //sapp();
                      //getHTML();
                      //txtMessage.setText(text);
                      txtMessage.setText(text);
                  }
                  else {
                  //mPlayer.seekTo(length);
                      mPlayer.start();
                  //  mPlayer.seekTo(length);
                      buttonPause.setText("Pause");
  
                  }
              }
          });
  
      }
  
      protected void onDestroy() {
          super.onDestroy();
          // TODO Auto-generated method stub
          if (mPlayer != null) {
              mPlayer.release();
              mPlayer = null;
          }
      }
  
  
  
      private class LoadWebPageASYNC extends AsyncTask&lt;String, Void, String&gt; {
  
          @Override
          protected String doInBackground(String... urls) {
              getHTML();
              //sapp();
              txtMessage.setText(text);
              return null;
          }
      }
      private void sapp()
      {
          //Runnable updater1 = new Runnable() {
              handler.postDelayed(new Runnable() {
                  //@Override
                  public void run() {
                      getHTML();
                      txtMessage.setText(text);
                  }
              }, INTERVAL);
  
  
              /*public void run() {
                  getHTML();
                  txtMessage.setText(text);
              }
          };*/
          //handler.post(updater1);
      }
      private void getHTML()
      {
          try {
              URLConnection connection = new URL("http://khilare.com/swltest/sms.html").openConnection();
              connection.setRequestProperty("Accept-Charset", "UTF-8");
              InputStream responseStream = connection.getInputStream();
  
              BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
              response = new StringBuilder();
              String line;
              line = br.readLine();
              {
                  response.append(line);
              }
              text = response.toString();
              //txtMessage.setText(text);
              //Log.i("Output", text);
          } catch (MalformedURLException e) {
              txtMessage.setText(e.getMessage());
              e.printStackTrace();
          } catch (IOException e) {
              txtMessage.setText(e.getMessage());
              e.printStackTrace();
          }
          //textStreamed.setText(text);
      }
  
  }
  </code></pre>
  
  <p>Here is Logcat:</p>
  
  <pre><code>    java.lang.SecurityException: Permission denial: writing to settings requires android.permission.WRITE_SETTINGS
              at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:645)
              at android.content.ContentProvider$Transport.call(ContentProvider.java:279)
              at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273)
              at android.os.Binder.execTransact(Binder.java:388)
              at dalvik.system.NativeStart.run(Native Method)
  05-29 10:56:45.772      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  05-29 10:56:45.772      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  05-29 10:56:48.462      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  05-29 10:56:48.462      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  05-29 10:56:50.702      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  05-29 10:56:50.702      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  05-29 10:56:50.712      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  05-29 10:56:50.712      302-302/? E/Parcel﹕ Reading a NULL string not supported here.
  
  05-29 10:57:01.772  28164-28569/com.prgguru.example E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
      java.lang.RuntimeException: An error occured while executing doInBackground()
              at android.os.AsyncTask$3.done(AsyncTask.java:299)
              at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
              at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
              at java.util.concurrent.FutureTask.run(FutureTask.java:239)
              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
              at java.lang.Thread.run(Thread.java:841)
       Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
              at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5969)
              at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:921)
              at android.view.ViewGroup.invalidateChild(ViewGroup.java:4276)
              at android.view.View.invalidate(View.java:10552)
              at android.view.View.invalidate(View.java:10507)
              at android.widget.TextView.checkForRelayout(TextView.java:6531)
              at android.widget.TextView.setText(TextView.java:3789)
              at android.widget.TextView.setText(TextView.java:3643)
              at android.widget.TextView.setText(TextView.java:3618)
              at com.prgguru.example.MusicAndroidActivity$LoadWebPageASYNC.doInBackground(MusicAndroidActivity.java:158)
              at com.prgguru.example.MusicAndroidActivity$LoadWebPageASYNC.doInBackground(MusicAndroidActivity.java:152)
              at android.os.AsyncTask$2.call(AsyncTask.java:287)
              at java.util.concurrent.FutureTask.run(FutureTask.java:234)
              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
              at java.lang.Thread.run(Thread.java:841)
  </code></pre>