声音没有在gridview中播放

时间:2015-09-24 04:14:46

标签: java android audio gridview

我想在Android网格视图中创建一个在图像上播放声音的应用程序。我写了一些代码,但声音没有播放。这是我的main_activity.xml

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

这是我的MainActivity.java

   package cf.droiddev.animalsoundskids;

   import java.io.IOException;
   import android.app.Activity;
   import android.content.res.AssetFileDescriptor;
   import android.media.MediaPlayer;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.AdapterView;
   import android.widget.AdapterView.OnItemClickListener;
   import android.widget.GridView;
   import android.widget.Toast;

  public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            try {
                String filename="sound_" + Integer.toString(position) + ".wav";
                AssetFileDescriptor afd = getAssets().openFd(filename); 
                if(afd != null) {
                    MediaPlayer mp = new MediaPlayer();
                    mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
                    mp.prepare();
                    mp.start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
  }
}

这是ImageAdapter.java

package cf.droiddev.animalsoundskids;

import android.widget.BaseAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.alligator, R.drawable.bear,
        R.drawable.cat, R.drawable.cattle,
        R.drawable.chickens, R.drawable.chimpanzee,
        R.drawable.cow, R.drawable.crocodile,
        R.drawable.crow, R.drawable.doberman_pincher,
        R.drawable.dolphin, R.drawable.donkey,
        R.drawable.duck, R.drawable.elephant,
        R.drawable.goat, R.drawable.gorilla,
        R.drawable.hawk, R.drawable.horse,
        R.drawable.hound, R.drawable.lamb,
        R.drawable.lion, R.drawable.monkey,
        R.drawable.mosquito, R.drawable.nightingale,
        R.drawable.owl, R.drawable.peacock,
        R.drawable.penguin, R.drawable.rooster,
        R.drawable.sample_29
   };
   }

任何人都可以告诉我它有什么问题我把我的图像存储在drawtable文件夹和asssets文件夹中的声音。

3 个答案:

答案 0 :(得分:0)

尝试手动提供音量,如下面的示例

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

答案 1 :(得分:0)

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

<TextInput
    placeholder = "FirstTextInput"
    returnKeyType = { "next" }
    onSubmitEditing={() => { this.secondTextInput.focus(); }}
    blurOnSubmit={false}
/>

<TextInput
    ref={(input) => { this.secondTextInput = input; }}
    placeholder = "secondTextInput"
/>

答案 2 :(得分:0)

试试这个,

MediaPlayer m = new MediaPlayer(); try{ AssetFileDescriptor descriptor = AmbeMaAartiActivity.this.getAssets().openFd("myfile.mp3"); m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() ); descriptor.close(); m.prepare(); m.start(); } catch(Exception e){ // handle error here.. }