SoundPool 类型中的方法 load(Context, int, int) 不适用,方法 getSystemService(String) 未定义

发布于 2024-11-30 10:39:29 字数 1879 浏览 1 评论 0原文

注意:我有点菜鸟所以不知道这些错误意味着什么。

这是我的类代码:

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

我收到两个错误,第一个错误在这里:

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

错误显示 SoundPool 类型中的方法 load(Context, int, int) 不适用于参数 (MySingleton, int ,整数) 第二个错误在这里:

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

并且错误显示 The method getSystemService(String) is undefined for the type MySingleton

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

This is my class 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));

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

and the error says The method getSystemService(String) is undefined for the type MySingleton

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别挽留 2024-12-07 10:39:29

除非传入 applicationContext 或 Activity,否则无法访问非 Activity 类中的系统服务。您需要在 extends Activity 类中执行此代码。

您需要在构造函数中包含上下文才能访问声音提供程序提供的服务,或者传入声音提供程序管理对象才能访问这些对象。

代码应该很简单,只需在类中声明一个 SoundPool 对象并将其传递到构造函数中即可。

You can't access system services in a non-activity class unless you pass in the applicationContext or activity. You need to do this code in your extends Activity class.

You need to include the context in your constructor in order to access services provided by the sound provider, or pass in the sound provider management object in order to access these objects.

The code should be trivial, just declare a SoundPool object in your class and pass it into the constructor.

反目相谮 2024-12-07 10:39:29

您需要一个上下文才能使用这些方法。 getSystemService 是 Context 实例 myActivity.getSystemService() 的一个方法。 load() 还希望您传入 Context 实例 (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.

世界如花海般美丽 2024-12-07 10:39:29

load 方法需要一个Context 实例(即ActivityService)。由于您的单例没有该实例,因此您需要首先设置 Context 的实例,然后才使用该实例调用 load 方法。

因此,您不能在构造函数中执行此操作。

The method load needs an instance of a Context (i.e. an Activity or a Service). As your singleton does not have that instance, you need to first set an instance of Context, and only after that, call the load method, using that instance.

Hence, you cannot do this in your constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文