Using AsyncTask for PCM AudioTrack playback without freezing activity
i could need some help with that. here is the problem and what i have done until now.
problem:
i want to play a pcm file that i recorded before. as this file can be larger, i play it by using audiotrack. this works quite nice. but i dont want the activity to freeze. i already tried thread and so on, now im working on the asynctask but the activity still is not responding.
this is what i got:
in the ui activity, i create a new waveplayer object and try to run it.
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Handles ShortClicks for ListView
*/
OnItemClickListener itemlistener = new OnItemClickListener(){
MediaPlayer mp=null;
String currentlyplaying = null;
@Override
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
long id) {
String pathtofile = (String) adaptview.getItemAtPosition(position);
if(pathtofile.contains(".wav"))
{
//HQ!
if(mp==null)
{开发者_开发知识库
clickedview.setSelected(true);
try
{
WavePlayer t = null;
//TODO: //add thread waveplayer to play file!
try {
t = new WavePlayer(pathtofile);
t.execute((Void)null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clickedview.postDelayed(new Deselector(clickedview, t), 1000);
currentlyplaying = pathtofile;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
else
{
}
}
else{
//NO HQ
//you dont need to know that :)
}
}
};
the class deselector:
/**
* This Runnable tries to deselect the view after playing the audio file.
* @author quant
*
*/
class Deselector implements Runnable
{
View view = null;
AsyncTask thread;
Deselector(View view, AsyncTask t)
{
this.view = view ;
this.thread = t;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
view.setSelected(false);
thread.cancel(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
everything concerning playback works nice, i can hear my voice and the playback itself works fine... but still the gui in the main activity does freeze/is not responding.
hope someone can help.
thanks in advance
markus
i solved it by using a remote service. i'm happy with this solution because i had one running for other purpose yet. now the interface is responsive again.
精彩评论