在 Eclipse 中使用数组向图库图像添加声音
add sound to gallery image with array in Eclipse
我来自印度尼西亚,很抱歉我的英语不好。我有如下源代码:
public class LihatHuruf 扩展 Activity {
MediaPlayer soundofletter;
int indeks=0;
String[] letter = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int [] sound = {R.raw.a , R.raw.b , R.raw.c , R.raw.d , R.raw.e , R.raw.f ,
R.raw.g , R.raw.h , R.raw.i , R.raw.j , R.raw.k , R.raw.l ,
R.raw.m , R.raw.n , R.raw.o , R.raw.p , R.raw.q , R.raw.r ,
R.raw.s , R.raw.t , R.raw.u , R.raw.v , R.raw.w , R.raw.x , R.raw.y , R.raw.z};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.lihathuruf);
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
Toast.makeText(LihatHuruf.this, "" + letter[position], Toast.LENGTH_SHORT).show();
}
});
}
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(250, 300));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
程序输出是如果我单击一个图像(字母 A),它会向吐司显示一个字母 A。但是我想用 res/raw 中的音频数组更改字母数组。我不知道确切的代码,也许任何人都可以给我代码,每个答案对我都非常有帮助。先谢谢了
检查 MediaPlayer class http://developer.android.com/reference/android/media/MediaPlayer.html
您的 OnItemClick
方法中的类似内容应该可以解决问题:
MediaPlayer player=MediaPlayer.create(LihatHuruf.this, sound[position]);
player.start();
我来自印度尼西亚,很抱歉我的英语不好。我有如下源代码: public class LihatHuruf 扩展 Activity {
MediaPlayer soundofletter;
int indeks=0;
String[] letter = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int [] sound = {R.raw.a , R.raw.b , R.raw.c , R.raw.d , R.raw.e , R.raw.f ,
R.raw.g , R.raw.h , R.raw.i , R.raw.j , R.raw.k , R.raw.l ,
R.raw.m , R.raw.n , R.raw.o , R.raw.p , R.raw.q , R.raw.r ,
R.raw.s , R.raw.t , R.raw.u , R.raw.v , R.raw.w , R.raw.x , R.raw.y , R.raw.z};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.lihathuruf);
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
Toast.makeText(LihatHuruf.this, "" + letter[position], Toast.LENGTH_SHORT).show();
}
});
}
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(250, 300));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
} }
程序输出是如果我单击一个图像(字母 A),它会向吐司显示一个字母 A。但是我想用 res/raw 中的音频数组更改字母数组。我不知道确切的代码,也许任何人都可以给我代码,每个答案对我都非常有帮助。先谢谢了
检查 MediaPlayer class http://developer.android.com/reference/android/media/MediaPlayer.html
您的 OnItemClick
方法中的类似内容应该可以解决问题:
MediaPlayer player=MediaPlayer.create(LihatHuruf.this, sound[position]);
player.start();