Search code examples
androidaudioandroid-contextsoundpool

Issue with soundPool.load Context


I have a Button in my app that calls an Async task. This Async task should play audio files, on every ProgressUpdate. I figured out quite a lot so far, but I am stuck with this:

i call

        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);

to fill an array with the strengthx.mp3 s.

Anyway: I get an error with "this" in the load statement:

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

What context do I have to use with this soundPool.load(Context, ResourceID, ..) ?

Thanks for the help in advance... :)


Solution

  • private Context mContext;
    
    onCreate(...) {
        mContext = this;
    }
    
    
    // inside your AsyncTask 
    ...
    
        onProgressUpdate(int progress) {
          this.strengthSound[0] = this.soundPool.load(mContext, R.raw.strength0,1);
          ...
        }
    

    I forgot the actual method signatures for AsyncTasks but this is basic rundown. You are misusing this.

    this in your activity and this in your AsyncTask is different. this in AsyncTask refers to itself, which is AsyncTask, but this in your onCreate refers to (again) itself, which is the Activity itself.

    Not sure if this helps, but feel free to elaborate on comment :)