开发者

async task problem in android

开发者 https://www.devze.com 2023-03-07 16:24 出处:网络
i want to do code for song is should play for 5 seconds only after 5 seconds player should stop the song play. i use AsyncTask in following way it does not stop after 5 seconds and i do not know where

i want to do code for song is should play for 5 seconds only after 5 seconds player should stop the song play. i use AsyncTask in following way it does not stop after 5 seconds and i do not know where to write code for player stop coding in the following. the task.execute coding just delayed for 5 seconds to start the activity. please help me. in the following player is playing the song but not stop. my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text=(TextView)findViewById(R.id.text);       
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            Log.e("bpm", "in run method");
            processor = new BPM2SampleProcessor();
            processor.setSampleSize(1024);
            EnergyOutputAudioDevice output = new EnergyOutputAudioDevice(processor);     
            output.setAverageLength(1024);  
            try {
                player = new Player(new FileInputStream("/sdcard/taxi.mp3"), output);
                player.play();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (JavaLayerException e) {
                e.printStackTrace();
            }
           Log.e("bpm","  bpm is  "+processor.getBPM());            
            return null;
        }           
        protected void onProgressUpdate(Void.开发者_StackOverflow社区.. params) 
        {
            text.setText("bpm is  "+processor.getBPM());
        }            
    };
    try {           
        task.execute((Void)null).get(5, TimeUnit.SECONDS);
        player.close();
    } catch(Exception e) {
        e.printStackTrace();
    }       
} 


You shouldn't be closing the player after you set this task. Instead, player.close() should be called 5 seconds after the task is created. This could be done by using a Handler object to post a Runnable object in your implementation of doInBackground() that stops the player.

0

精彩评论

暂无评论...
验证码 换一张
取 消