如何创建 Asynctask 将声音加载到加载屏幕上的管理器中
我过去的两个问题很短而且不详细,所以这次我会尽力而为。我有一个大约有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您给出的示例中...
三种类型 URL、Integer 和 Long(称为 Params、Progress、Result)可以是您需要的任何类型。
第一个 (
URL
) 是必须传递给AsyncTask
实例的.execute()
方法的参数类型。更准确地说,当您查看doInBackground()
方法时,您将看到URL...
,这基本上意味着它将接受URL
数组。即使您只需要传递一个URL
,您仍然必须将其作为单项数组传递。在
doInBackground(URL... urls)
方法中,您访问URL
为urls[0]
、urls[1]
等或类似for (URL u:urls)
。本示例中的第二个泛型类型 (
Integer
) 是onProgressUpdate(Integer Progress)
所期望的类型。同样,这必须作为数组传递。例如,如果您要下载 10 个文件,则在下载每个文件后调用它。例如myProgress[0] = 1
表示已成功下载一个文件。这允许您更新某种进度对话框。最后,第三个泛型类型 (
Long
) 再次在内部使用,并且是onDoInBackground(...)
必须返回的类型,并传递给onPostExecute(Long result)
。请注意,这是单个结果而不是数组。根据您的结果将取决于onPostExecute()
的行为方式。正如我所说,您可以使用任何类型,包括通用
Void
(注意大写字母)...在这种情况下,您不会将任何内容传递给
.execute()
并且尽管您仍然可以调用publishProgress()
(调用onProgressUpdate()
),但您无法向其传递任何数据。同样,onPostExecute
将不会收到任何实际结果数据。In the example you give...
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 yourAsyncTask
instance. More accurately when you look at thedoInBackground()
method you will seeURL...
which basically means it will accept an array ofURL
. Even if you only need to pass oneURL
you must still pass it as a single-item arrayIn the
doInBackground(URL... urls)
method you access theURL
s asurls[0]
,urls[1]
etc or something likefor (URL u:urls)
.The second generic type in this example (
Integer
) is the type expected byonProgressUpdate(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 examplemyProgress[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 typeonDoInBackground(...)
must return and is passed toonPostExecute(Long result)
. Note this is a single result and not an array. Depending on what your result is will depend on howonPostExecute()
should behave.As I said you can use any types including generic
Void
(note capital letter)...In this case you don't pass anything to
.execute()
and although you can still callpublishProgress()
(to callonProgressUpdate()
) you can't pass any data to it. Similarly,onPostExecute
will not receive any actual result data.