gpt4 book ai didi

java - Android:JsonObjectRequest 内的媒体播放器

转载 作者:行者123 更新时间:2023-12-01 17:52:03 25 4
gpt4 key购买 nike

我需要在 JsonObjectRequest 中使用 Mediaplayer。我的代码是这样的

    obreq = new JsonObjectRequest(Request.Method.GET, url,
// The third parameter Listener overrides the method onResponse() and passes
//JSONObject as a parameter
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try {

MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.ok);
mediaPlayer.start();

JSONObject obj = response.getJSONObject("result");


}
// Try and catch are included to handle any errors due to JSON
catch (JSONException e) {
// If an error occurs, this prints the error to the log
Log.e("Errore: ", e.getLocalizedMessage());
e.printStackTrace();
}
}
},
// The final parameter overrides the method onErrorResponse() and passes VolleyError
//as a parameter
new Response.ErrorListener() {
@Override
// Handles errors that occur due to Volley
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");

MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.error);
mediaPlayer.start();
throw new NullPointerException("uuuuuu");
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> customHeaders = new HashMap<>();

String login_url = Helper.getConfigValue(getApplicationContext(), "login_url");
String pwd_url = Helper.getConfigValue(getApplicationContext(), "pwd_url");
String credentials = login_url + ":" + pwd_url;

String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

customHeaders.put("Content-Type", "application/json; charset=utf-8");

customHeaders.put("Authorization", auth);


return customHeaders;
}
};

共享编辑删除标志

但我收到错误

"Cannot resolve method 'create(anonymous com.android.volley.Response.Listener, int)"

在 MediaPlayer 行

如何解决这个问题?

谢谢

最佳答案

因为你的“this”指的是你的截击对象

MediaPlayer.create(this,R.raw.ok);

将其替换为您的 Activity 引用,例如

 MediaPlayer.create(yourActivity.this,R.raw.ok);

关于java - Android:JsonObjectRequest 内的媒体播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60780187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com