android中的异步任务问题

发布于 2024-11-09 04:34:58 字数 1564 浏览 1 评论 0原文

我想做歌曲的代码,只有在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();
    }       
} 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-11-16 04:34:58

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文