【问题标题】:async task problem in androidandroid中的异步任务问题
【发布时间】:2011-05-21 05:20:50
【问题描述】:

我想为歌曲仅在 5 秒播放器停止播放歌曲后才播放 5 秒编写代码。我以以下方式使用 AsyncTask,它不会在 5 秒后停止,我不知道在哪里为播放器停止编码编写代码。 task.execute 编码只是延迟了 5 秒以启动活动。请帮我。 在下面的播放器正在播放歌曲但没有停止。 我的代码:

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... params) 
        {
            text.setText("bpm is  "+processor.getBPM());
        }            
    };
    try {           
        task.execute((Void)null).get(5, TimeUnit.SECONDS);
        player.close();
    } catch(Exception e) {
        e.printStackTrace();
    }       
} 

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    您不应该在设置此任务后关闭播放器。相反,player.close() 应该在任务创建 5 秒后调用。这可以通过使用 Handler 对象在您的 doInBackground() 实现中发布一个可停止播放器的 Runnable 对象来完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多