【问题标题】:Issue with soundPool.load ContextsoundPool.load 上下文问题
【发布时间】:2016-10-21 21:32:49
【问题描述】:

我的应用中有一个调用异步任务的按钮。 这个异步任务应该在每个 ProgressUpdate 上播放音频文件。 到目前为止,我想出了很多,但我一直坚持:

我打电话

        this.strengthSound[0] = this.soundPool.load(this, R.raw.strength0,1);
        this.strengthSound[1] = this.soundPool.load(this, R.raw.strength1,1);
        this.strengthSound[2] = this.soundPool.load(this, R.raw.strength2,1);
        this.strengthSound[3] = this.soundPool.load(this, R.raw.strength3,1);
        this.strengthSound[4] = this.soundPool.load(this, R.raw.strength4,1);

用strengthx.mp3 填充一个数组。

无论如何:我在加载语句中收到“this”错误:

Error:(165, 57) error: incompatible types: AIM_start.start_AIM cannot be converted to Context

我必须在这个 soundPool.load(Context, ResourceID, ..) 中使用什么上下文?

提前感谢您的帮助... :)

【问题讨论】:

    标签: android audio android-context soundpool


    【解决方案1】:
    private Context mContext;
    
    onCreate(...) {
        mContext = this;
    }
    
    
    // inside your AsyncTask 
    ...
    
        onProgressUpdate(int progress) {
          this.strengthSound[0] = this.soundPool.load(mContext, R.raw.strength0,1);
          ...
        }
    

    我忘记了 AsyncTasks 的实际方法签名,但这是基本的概要。你在滥用this

    您的活动中的this 和您的 AsyncTask 中的this 是不同的。 AsyncTask 中的this 指的是自身,即AsyncTask,但您的onCreate 中的this (再次)指的是自身,即Activity 本身。

    不确定这是否有帮助,但请随时详细说明评论:)

    【讨论】:

      猜你喜欢
      • 2011-12-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 2018-02-04
      相关资源
      最近更新 更多