在类型的Soundpool的方法负荷(背景下,INT,INT)是不适用的方法,getSystemService(字符串)是未定义 [英] The method load(Context, int, int) in the type SoundPool is not applicable, The method getSystemService(String) is undefined

查看:179
本文介绍了在类型的Soundpool的方法负荷(背景下,INT,INT)是不适用的方法,getSystemService(字符串)是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:我是怎样的一个小白,所以真的不知道这些是什么意思误差

Note: I'm kind of a noob so dont really know what these errors mean.

这是我的课code:

package ryan.test;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class MySingleton {

    private MySingleton instance;

    private static SoundPool mSoundPool;
    private HashMap<Integer, Integer> soundPoolMap;

    public static final int A1 = 1;

    private MySingleton() {
        mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);// Just an example
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(A1, mSoundPool.load(this, R.raw.a,    1));
   //   soundPoolMap.put(A5, mSoundPool.load(MyApp.this,       R.raw.a,    1));
    }

    public synchronized MySingleton getInstance() {
        if(instance == null) {
            instance = new MySingleton();
        }
        return instance;
    }

    public void playSound(int sound) {
        AudioManager mgr = (AudioManager)MySingleton.getSystemService(Context.AUDIO_SERVICE);
        float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
        float volume = streamVolumeCurrent / streamVolumeMax;

        mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);     
    }

    public SoundPool getSoundPool() {
        return mSoundPool;
   }
}

和我得到两个错误,第一个错误是在这里:

And I am getting two errors, the first error is here:

soundPoolMap.put(A1, mSoundPool.load(this, R.raw.a,    1));

和错误说在类型的Soundpool的方法负荷(背景下,INT,INT)不适用于参数(MySingleton,INT,INT)
而第二个错误是在这里:

and the error says The method load(Context, int, int) in the type SoundPool is not applicable for the arguments (MySingleton, int, int) and the second error is here:

AudioManager mgr = (AudioManager)MySingleton.getSystemService(Context.AUDIO_SERVICE);

和错误说的方法getSystemService(字符串)是未定义的类型MySingleton

推荐答案

您需要一个环境中使用这些方法。 getSystemService 是上下文实例的方法, myActivity.getSystemService()负荷()也希望您能在上下文实例(myActivity)作为第一个参数传递。这不是建议你保持主要活动之外的上下文的引用,所以你应该考虑把这个逻辑回的活动。你为什么要做这一个单身?播放的背景音乐?使用服务。

You need a Context to use those methods. getSystemService is a method of the Context instance, myActivity.getSystemService(). load() also expects you to pass in the Context instance (myActivity) as the first argument. It's not recommended that you keep a reference to the context outside of the main activity, so you should consider moving this logic back into the activity. Why are you trying to do this in a singleton? Play music in the background? Use a service.

这篇关于在类型的Soundpool的方法负荷(背景下,INT,INT)是不适用的方法,getSystemService(字符串)是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆