如何创建 Asynctask 将声音加载到加载屏幕上的管理器中

发布于 2024-12-25 13:09:23 字数 604 浏览 3 评论 0原文

我过去的两个问题很短而且不详细,所以这次我会尽力而为。我有一个大约有 430 个声音的大音板。它太大了,我必须在某些设备上创建 2 个声音管理器。无论如何,在加载屏幕上,我正在尝试实现 AsyncTask。我大致了解它的类型和4个步骤,但我不了解参数。这里有一个简单的AsyncTask供参考。

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {

    }
    return null;
}

protected void onProgressUpdate(Integer... progress) {

}

protected void onPostExecute(Long result) {

}
}

我需要在后台做的就是向我的经理添加声音,如下所示: SoundManager2.addSound(415, R.raw.rubber);

拜托,这是我的第三个问题,所以如果您需要任何其他信息,请随时询问,我将在接下来的 20 分钟内观看此帖子,我将快速使用信息进行编辑!

My past two questions were short and not detailed so I'll try my best this time. I have a large soundboard with around 430 sounds. It is so big, I have to create 2 soundmanagers on some devices. Anyway, on the loading screen, I am trying to implement an AsyncTask. I generally understand its types and its 4 steps, but I do not understand the parameters. Here is a simple AsyncTask for reference.

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {

    }
    return null;
}

protected void onProgressUpdate(Integer... progress) {

}

protected void onPostExecute(Long result) {

}
}

What I need to do in the background is add sounds to my manager like this:
SoundManager2.addSound(415, R.raw.rubber);

Please, this is my 3rd question here so if you need ANY other info, don't hesitate to ask, I'll be watching this thread for the next 20 minutes and I'll edit it with the information quickly!

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

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

发布评论

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

评论(1

も星光 2025-01-01 13:09:24

在您给出的示例中...

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {...}

三种类型 URL、Integer 和 Long(称为 Params、Progress、Result)可以是您需要的任何类型。

第一个 (URL) 是必须传递给 AsyncTask 实例的 .execute() 方法的参数类型。更准确地说,当您查看 doInBackground() 方法时,您将看到 URL...,这基本上意味着它将接受 URL 数组。即使您只需要传递一个 URL,您仍然必须将其作为单项数组传递。

URL[] myURLs = new URL[] {<comma-separated URLs here>};
new DownloadFilesTask().execute(myURLs);

doInBackground(URL... urls) 方法中,您访问 URLurls[0]urls[1] 等或类似 for (URL u:urls)

本示例中的第二个泛型类型 (Integer) 是 onProgressUpdate(Integer Progress) 所期望的类型。同样,这必须作为数组传递。例如,如果您要下载 10 个文件,则在下载每个文件后调用它。例如 myProgress[0] = 1 表示已成功下载一个文件。这允许您更新某种进度对话框。

最后,第三个泛型类型 (Long) 再次在内部使用,并且是 onDoInBackground(...) 必须返回的类型,并传递给 onPostExecute(Long result)。请注意,这是单个结果而不是数组。根据您的结果将取决于 onPostExecute() 的行为方式。

正如我所说,您可以使用任何类型,包括通用 Void (注意大写字母)...

private class MyAysncTask extends AsyncTask<Void, Void, Void>

在这种情况下,您不会将任何内容传递给 .execute() 并且尽管您仍然可以调用 publishProgress() (调用 onProgressUpdate()),但您无法向其传递任何数据。同样,onPostExecute 将不会收到任何实际结果数据。

In the example you give...

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {...}

The three types URL, Integer and Long (known as Params, Progress, Result) can be anything you need them to be.

The first (URL) is the type of parameter you must pass to the .execute(<params>) method of your AsyncTask instance. More accurately when you look at the doInBackground() method you will see URL... which basically means it will accept an array of URL. Even if you only need to pass one URL you must still pass it as a single-item array

URL[] myURLs = new URL[] {<comma-separated URLs here>};
new DownloadFilesTask().execute(myURLs);

In the doInBackground(URL... urls) method you access the URLs as urls[0], urls[1] etc or something like for (URL u:urls).

The second generic type in this example (Integer) is the type expected by onProgressUpdate(Integer progress). Again this must be passed as an array. For instance if you are downloading 10 files then call it after each file has been downloaded. For example myProgress[0] = 1 to indicate one file has been successfully downloaded. This allows you to update a progress dialog of some sort.

Finally the third generic type (Long) is again used internally and is the type onDoInBackground(...) must return and is passed to onPostExecute(Long result). Note this is a single result and not an array. Depending on what your result is will depend on how onPostExecute() should behave.

As I said you can use any types including generic Void (note capital letter)...

private class MyAysncTask extends AsyncTask<Void, Void, Void>

In this case you don't pass anything to .execute() and although you can still call publishProgress() (to call onProgressUpdate()) you can't pass any data to it. Similarly, onPostExecute will not receive any actual result data.

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