Updating UI while playing audio track

All your problems with Audio, Video and Images.

Updating UI while playing audio track

Postby dickwan » Sat Mar 26, 2011 1:25 pm

Hello,
I'm playing around with raw audio recording and playback for a couple of days now. I'm trying to update the UI from the AsynchTask responsible for the audio rendering. So I wrote the code for updating the progress bar (or other UI Views ) in the AsynchTask containing the AudioTrack, but now I have the following effect the the audio rendering is crackling. This happens even if I'm only updating a simple TextView while playing back the PCM file.

I really don't know how to explain this behavior. Maybe it's is because I'm working with the emulator.

here is the subclass of Asynchtask that i use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private class PlayTask extends AsyncTask<WavItem, Integer, Void> {
  2.                
  3.                 private String TAG = PlayTask.class.getName();
  4.                 @Override
  5.                 protected void onPreExecute() {
  6.                         super.onPreExecute();
  7.                         controlStatus.setText("Playing...");
  8.                 }
  9.  
  10.                 @Override
  11.                 protected Void doInBackground(WavItem... wavItem) {
  12.                        
  13.                         byte[] audioData = wavItem[0].getAudioData();
  14.                        
  15.                         int frequency = wavItem[0].getFrequency();
  16.                         int channelConfiguration = wavItem[0].getChannelConfiguration();
  17.                         int audioEncoding = wavItem[0].getAudioEnconding();
  18.                         int bufferSize = AudioTrack.getMinBufferSize(frequency,
  19.                                         channelConfiguration, audioEncoding);
  20.                         short blockAlign = wavItem[0].getBlockAlign();
  21.                        
  22.                         /* Byte array to read the samples from the input stream.*/
  23.                          byte[] audioDataChunk = new byte[bufferSize];
  24.                          int readed = 0;
  25.                          int duration = 0;
  26.                         try {
  27.                                 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(audioData));
  28.                                
  29.                                 AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  30.                                                                                                                 frequency,
  31.                                                                                                                 channelConfiguration,
  32.                                                                                                                 audioEncoding,
  33.                                                                                                                 bufferSize*8,
  34.                                                                                                                 AudioTrack.MODE_STREAM);
  35.                                 audioTrack.play();
  36.                                
  37.                                 while(isPlaying && dis.available() > 0){
  38.                                         int i = 0;
  39.                                         while(dis.available() > 0 && i < audioDataChunk.length){
  40.                                                 audioDataChunk[i] = dis.readByte();
  41.                                                 i++;
  42.                                         }
  43.                                         readed = audioData.length - dis.available();
  44.                                         /* Compute duration*/
  45.                                         duration = readed/blockAlign/frequency;
  46.                                         publishProgress(new Integer(duration),new Integer(readed));
  47.                                        
  48.                                         audioTrack.write(audioDataChunk, 0, audioDataChunk.length);
  49.                                 }
  50.                                 /* No more data to play or stop pressed.*/
  51.                                 // Close the stream.
  52.                                 dis.close();
  53.                                 audioTrack.flush();
  54.                                 // Stop playing audio data.
  55.                                 audioTrack.stop();
  56.                                 // Release AudioTrack resources.
  57.                                 audioTrack.release();
  58.                                 // Set AudioTrack object to null
  59.                                 audioTrack = null;
  60.                         } catch (Exception e) {
  61.                                 if (e.getMessage() != null){
  62.                                         Log.e(TAG, "doInBackground(): "+e.getMessage());
  63.                                 }
  64.                                 else{
  65.                                         Log.e(TAG, "doInBackground(): Playeback failed");
  66.                                 }
  67.                         }
  68.                                                
  69.                         return null;
  70.                 }
  71.  
  72.                 @Override
  73.                 protected void onProgressUpdate(Integer... values) {
  74.                         super.onProgressUpdate(values);
  75.                         String timeLength=String.format("%02d:%02d:%02d", values[0]/3600, (values[0]%3600)/60, (values[0]%60));
  76.                         trackDuration.setText(timeLength);
  77.                         trackPlaybackProgress.setProgress(values[1]);
  78.                 }
  79.  
  80.                 @Override
  81.                 protected void onPostExecute(Void result) {
  82.                         super.onPostExecute(result);
  83.                         controlStatus.setText("Stopped");
  84.                 }
  85.         }
  86.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


So what I have is when I comment the lines setText(Sting arg) for both the progress bar and the TextView that I've declarated in the Main Activity class, the audio rendering is seamless (in the emulator). But when I remove the comments and leave the progress bar and or the TextView being updated from the onProgressUpdate(Integer... values) method, the playback is crackling and slow. Is it a problem with the emulator? What did I forgot. Please correct me if i'm wrong here, I'm kind of lost.



Thanks for taking a look at this.

Dickwan
dickwan
Freshman
Freshman
 
Posts: 8
Joined: Sun Dec 27, 2009 11:39 pm

Top

Re: Updating UI while playing audio track

Postby imbroken » Mon Apr 04, 2011 2:39 pm

Maybe you are updating to ofen in that while loop.
imbroken
Senior Developer
Senior Developer
 
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay

Re: Updating UI while playing audio track

Postby mapiko » Tue Apr 05, 2011 6:54 pm

http://www.helloandroid.com/tutorials/m ... yer-part-i

This is exatclly what do you need. Backgroung service and updater UI
mapiko
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat May 15, 2010 5:19 pm

Re: Updating UI while playing audio track

Postby dickwan » Wed Apr 06, 2011 10:51 am

mapiko wrote:http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i

This is exatclly what do you need. Backgroung service and updater UI

Hi Mapiko,

thanks for the link. I did briefly look at the project and the way I see it, they are not working with the AudioPlay class like I'm. I've already design a small apps with an interface. Here is what it looks like:

Image

My problems occur while updating the progress bar and the textview for the time. like imbroken said, maybe I'm updating too often. I don't know if it's related to the emulator or my code. I'll try to update like every second or half-seconds and see if it's still crackling.

Note: Actually while playing back, whenever there is some UI activity like scrlling the playlist, the playback sound crackles.

Thanks' y'all for the replies and advices.

Dickwan
dickwan
Freshman
Freshman
 
Posts: 8
Joined: Sun Dec 27, 2009 11:39 pm

Re: Updating UI while playing audio track

Postby dickwan » Wed Apr 06, 2011 4:13 pm

Huh OK this is a little bit embarrassing... I had the chance to test the code I've wrote on a real device and everything went just fine i.e. The playback as well as updating the playback progress.
So I suppose it was really because of the emulator that playback did not combined so well with UI-update.
Last edited by dickwan on Thu Apr 07, 2011 9:31 am, edited 1 time in total.
dickwan
Freshman
Freshman
 
Posts: 8
Joined: Sun Dec 27, 2009 11:39 pm

Re: Updating UI while playing audio track

Postby mapiko » Thu Apr 07, 2011 7:55 am

Yes. Emulator is not so good testing tool as is should be. I myself don't like testing app functionality in emulator. For looking at UI at different screen sizes is ok, but not for much more.

IMO
mapiko
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat May 15, 2010 5:19 pm

Top

Re: Updating UI while playing audio track

Postby dickwan » Thu Apr 07, 2011 9:39 am

mapiko wrote:Yes. Emulator is not so good testing tool as is should be. I myself don't like testing app functionality in emulator. For looking at UI at different screen sizes is ok, but not for much more.

IMO

I kind of co-sign that. But still, working with the emulator is a good way to see how expensive the written code is (loops & recursion, I/O, threads,...). I mean these kind of operations are reaaaaally slow on the emulator and developer may be motivated to write more efficient user friendly code.

Anyway thanks for confirming my suspicions about the Emu.
dickwan
Freshman
Freshman
 
Posts: 8
Joined: Sun Dec 27, 2009 11:39 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 7 guests