Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在自定义视图类中使用声音池?_Java_Android_Android Activity_Android Custom View_Soundpool - Fatal编程技术网

Java 如何在自定义视图类中使用声音池?

Java 如何在自定义视图类中使用声音池?,java,android,android-activity,android-custom-view,soundpool,Java,Android,Android Activity,Android Custom View,Soundpool,我正在制作一个简单的演示,它使用一个自定义视图类扩展视图,该视图希望显示具有随机颜色的随机圆圈,这些圆圈可以随机出现在画布上 我已经随机发布并设置了一个onTouchEvent来判断点击是否在圆圈中,如果圆圈被点击,那么应用程序会播放一个短声音。但在这一步,我遇到了一些问题 我使用SoundPool播放音乐,但由于它不能在customview下使用(我的是MyView.java),所以我将它放回MainActivity extends Activity下onCreate函数下的MainActiv

我正在制作一个简单的演示,它使用一个自定义视图类扩展视图,该视图希望显示具有随机颜色的随机圆圈,这些圆圈可以随机出现在画布上

我已经随机发布并设置了一个onTouchEvent来判断点击是否在圆圈中,如果圆圈被点击,那么应用程序会播放一个短声音。但在这一步,我遇到了一些问题

我使用SoundPool播放音乐,但由于它不能在customview下使用(我的是MyView.java),所以我将它放回MainActivity extends Activity下onCreate函数下的MainActivity.java,设置后,这些函数看起来还可以,但我仍然不能在我的customview.java中使用SoundPool.play

怎么解决?我只是一个新手,所以我还不清楚这些事情,你能帮我吗

代码在这里(MainActivity.java)

package com.example.luckbag2;
导入java.util.HashMap;
导入android.app.Activity;
导入android.media.AudioManager;
导入android.media.SoundPool;
导入android.os.Bundle;
公共类MainActivity扩展了活动{
私人声池声池;
私有HashMap soundMap=新HashMap();
@抑制警告(“弃用”)
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
MyView v=新的MyView(getApplicationContext());
setContentView(v);
声音池=新的声音池(10,AudioManager.STREAM_系统,5);
soundMap.put(1,soundPool.load(this,R.raw.collide,0));
}
公共声音池getSoundPool(){
返回声池;
}
公共void setSoundPool(SoundPool SoundPool){
this.soundPool=soundPool;
}
公共HashMap getSoundMap(){
返回声音图;
}
公共void setSoundMap(HashMap soundMap){
this.soundMap=soundMap;
}
}
Myview.java

package com.example.luckbag2;

import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;

public class MainActivity extends Activity {
private SoundPool soundPool;
private HashMap<Integer,Integer> soundMap= new HashMap<Integer,Integer>();

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    MyView v = new MyView(getApplicationContext());
    setContentView(v);           
    soundPool =new SoundPool(10,AudioManager. STREAM_SYSTEM,5);
    soundMap.put(1,soundPool.load(this,R.raw.collide,0));    
}
public SoundPool getSoundPool() {
    return soundPool;
}
public void setSoundPool(SoundPool soundPool) {
    this.soundPool = soundPool;
}
public HashMap<Integer,Integer> getSoundMap() {
    return soundMap;
}
public void setSoundMap(HashMap<Integer,Integer> soundMap) {
    this.soundMap = soundMap;
}
}
package com.example.luckbag2;

import java.util.HashMap;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


@SuppressWarnings("deprecation")
@SuppressLint({ "DrawAllocation", "ClickableViewAccessibility" }) class MyView extends View
{
 private Handler mHandler;

 private int mColor;

 private float cx;

 private float cy;

 private float radius;

 private float x;

 private float y;

 private int score;


public MyView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    mHandler = new Handler(getMainLooper());

    setBackgroundColor(Color.WHITE);

    Thread mThread = new Thread(new Runnable() {

        @Override
        public void run()
        {
            // TODO Auto-generated method stub
            MyView.this.invalidate();
            mHandler.postDelayed(this, 500);
        }
    });
    mThread.start();


}
private Callback getMainLooper() {
    // TODO Auto-generated method stub
    return null;
}
@Override
protected void onDraw(Canvas canvas)
{

    int w = this.getWidth();
    int h = this.getHeight();
    Log.d("CustomView", "w = " + w + ", h = " + h);

    // TODO Auto-generated method stub
    super.onDraw(canvas);
    update();
    Paint p = new Paint();
    p.setColor(mColor);
    canvas.drawCircle(cx, cy, radius, p);

}

private void update()
{
    Random random = new Random();
    cx =(float)(250+random.nextInt(580));        
    cy =(float)(250+random.nextInt(1057));        
    radius =(float)( 100 + random.nextInt(150));      

    int r = random.nextInt(256);
    int g= random.nextInt(256);
    int b = random.nextInt(256);
    mColor = Color.rgb(r, g, b);                   
}

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

public boolean onTouchEvent(MotionEvent event){

    x = event.getX();
    y = event.getY();

    if ((x - cx) * (x - cx) + 
            (y - cy) * (y - cy) < radius * radius) { 
            Log.d("CustomView", "circle got clicked");
            SoundPool.play(SoundPool.get(1), 1,1,0,0,1);

    }

    return false;

}

public int getScore() {
    return score;
}
public void setScore(int score) {
    this.score = score;
}
package com.example.luckbag2;
导入java.util.HashMap;
导入java.util.Random;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.media.AudioManager;
导入android.media.SoundPool;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.Handler.Callback;
导入android.util.Log;
导入android.view.MotionEvent;
导入android.view.view;
@抑制警告(“弃用”)
@SuppressLint({“DrawAllocation”,“ClickableViewAccessibility”})类MyView扩展了视图
{
私人经理人;
私人内特彩色;
私人浮动cx;
私人股本;
私有浮动半径;
私人浮动x;
私人游船;
个人智力得分;
公共MyView(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
mHandler=新处理程序(getMainLooper());
setBackgroundColor(颜色:白色);
Thread mThread=新线程(new Runnable(){
@凌驾
公开募捐
{
//TODO自动生成的方法存根
MyView.this.invalidate();
mHandler.postDelayed(这个,500);
}
});
mThread.start();
}
私有回调getMainLooper(){
//TODO自动生成的方法存根
返回null;
}
@凌驾
受保护的void onDraw(画布)
{
int w=this.getWidth();
int h=this.getHeight();
Log.d(“CustomView”,“w=“+w+”,h=“+h”);
//TODO自动生成的方法存根
super.onDraw(帆布);
更新();
油漆p=新油漆();
p、 setColor(mColor);
画布绘制圆(cx,cy,半径,p);
}
私有void更新()
{
随机=新随机();
cx=(浮动)(250+随机.nextInt(580));
cy=(浮动)(250+随机.nextInt(1057));
半径=(浮动)(100+随机.nextInt(150));
int r=random.nextInt(256);
int g=random.nextInt(256);
int b=随机的.nextInt(256);
mColor=Color.rgb(r,g,b);
}
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
super.onSizeChanged(w,h,oldw,oldh);
}
公共布尔onTouchEvent(运动事件){
x=event.getX();
y=event.getY();
如果((x-cx)*(x-cx)+
(y-cy)*(y-cy)<半径*半径){
Log.d(“CustomView”,“圆圈被点击”);
SoundPool.play(SoundPool.get(1),1,1,0,0,1);
}
返回false;
}
公共整数getScore(){
返回分数;
}
公共核心(整数分数){
这个分数=分数;
}

}调用
声音池。从
MyView
class pass
MainActivity
类上下文播放
MyView
构造函数:

MainActivity mActivity;
public MyView(Context context,MainActivity mActivity) {
    super(context);
this.mActivity=mActivity;

}
MainActivity
create
MyView
将对象分类为:

MyView v = new MyView(getApplicationContext(),this);
现在使用
mActivity
MainActivity
访问方法:

要在
MyView
类中获取
SoundPool
对象:

SoundPool soundPool= mActivity.getSoundPool();
使用
soundPool
对象调用
play
方法:

SoundPool soundPool= mActivity.getSoundPool();
soundPool.play(mActivity.getSoundMap().get(1), 1,1,0,0,1)

获取当前代码有什么问题?
SoundPool.play(SoundPool.get(1),1,1,0,0,1)
MyView.java中的这一行出现错误。Eclipse对“get”加了下划线,它表明类型soundPool的方法get(int)未定义,但我不知道如何定义它(在我找到的文章中没有看到它),我应该将Mainactivity、mActivity段落放在Mainactivity.java或Myview.java中的哪个位置?@StevenYUAN:in
Myview.java
Ok!这很有效,非常感谢你帮助一个新手~