Java 声音不在gridview中播放

Java 声音不在gridview中播放,java,android,audio,gridview,Java,Android,Audio,Gridview,我正在triyng创建一个应用程序,在android网格视图中点击图像播放声音。我写了一些代码,但声音不播放。这是我的主要活动 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma

我正在triyng创建一个应用程序,在android网格视图中点击图像播放声音。我写了一些代码,但声音不播放。这是我的主要活动

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="true"
tools:context="cf.droiddev.animalsoundskids.MainActivity" >

<GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center" />


</RelativeLayout>

有谁能告诉我有什么问题吗?我已经将图像存储在drawable文件夹中,将声音存储在asssets文件夹中。

尝试手动给定音量,如下例所示

  if(afd != null) {
                        MediaPlayer mp = new MediaPlayer();
                        mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.setVolume(1.0f, 1.0f);
                        mp.start();
                    }

如果您有短的声音样本,那么最好使用soundpool

SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

 /** soundId for Later handling of sound pool **/ 
 int soundId = sp.load(context, R.raw.sample_sound, 1); // in 2nd param u have to pass your desire ringtone

 sp.play(soundId, 1, 1, 0, 0, 1);
试试这个

MediaPlayer m=新的MediaPlayer; 试一试{ AssetFileDescriptor descriptor=AMBEMAARTIACTIVITY.this.getAssets.openFdmyfile.mp3; m、 setDataSourcedescriptor.getFileDescriptor,descriptor.getStartOffset,descriptor.getLength; 描述符。关闭; m、 准备; m、 开始; }卡奇{ //在此处处理错误。。
}

您在logcat中得到什么信息了吗?您在单击某个项目时收到toast消息了吗?是的,它会显示带有图像编号@Bossput的tost。如果出现其他情况,请检查afd是否为空
  if(afd != null) {
                        MediaPlayer mp = new MediaPlayer();
                        mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
                        mp.prepare();
                        mp.setVolume(1.0f, 1.0f);
                        mp.start();
                    }
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

 /** soundId for Later handling of sound pool **/ 
 int soundId = sp.load(context, R.raw.sample_sound, 1); // in 2nd param u have to pass your desire ringtone

 sp.play(soundId, 1, 1, 0, 0, 1);