Java 如何使用截击和异步任务播放mp3文件?
你好,朋友,我是android新手,我想从url播放在线mp3文件,我使用异步任务播放在线mp3文件,但问题是处理请求需要太多时间,我听说volley最适合这个目的,现在我使用volley,但当请求发送到服务器时,它会阻止我的主Ui线程,而我听说volley本身管理aysc任务和更快的性能,但在我的情况下,我确实看到了类似的东西,请帮助如何从url播放音频更快,第二次没有阻止主线程的帮助截击这里音频url和我的代码 “”Java 如何使用截击和异步任务播放mp3文件?,java,android,android-volley,networkonmainthread,Java,Android,Android Volley,Networkonmainthread,你好,朋友,我是android新手,我想从url播放在线mp3文件,我使用异步任务播放在线mp3文件,但问题是处理请求需要太多时间,我听说volley最适合这个目的,现在我使用volley,但当请求发送到服务器时,它会阻止我的主Ui线程,而我听说volley本身管理aysc任务和更快的性能,但在我的情况下,我确实看到了类似的东西,请帮助如何从url播放音频更快,第二次没有阻止主线程的帮助截击这里音频url和我的代码 “” RequestQueue queue=Volley.newRequestQ
RequestQueue queue=Volley.newRequestQueue(this);
字符串url=”http://wpaorg.wordproject.com/bibles/app/audio/21/“+booknumber+”/“+chapternumber+”.mp3”;
//从提供的URL请求字符串响应。
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//显示响应字符串的前500个字符。
Toast.makeText(ALLVERSE.this,“+response,Toast.LENGTH_SHORT).show();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(ALLVERSE.this,“+错误,Toast.LENGTH_SHORT.show();
}
});
//将请求添加到RequestQueue。
添加(stringRequest);
当我尝试发送请求时,需要花费很多时间和第二个块ui线程为什么?直接使用ExoPlayer播放音频。它在内部处理远程源。@MohammedAtif如果它也处理aysc任务,那么我的主线程就不会被阻塞?是的,exoplayer在@Mohammed Atif中完美地处理了您的用例,这意味着我不需要手动使用任何aysnch任务,或者您以前使用过它吗?
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://wpaorg.wordproject.com/bibles/app/audio/21/"+booknumber+"/"+chapternumber+".mp3";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Toast.makeText(ALLVERSE.this, ""+response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ALLVERSE.this, ""+error, Toast.LENGTH_SHORT).show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);