我想播放一个简短的声音文件(例如mp3,wav)。如何在刷页面时播放短小的声音。如何更改我的代码,我想添加媒体播放器。请帮助我。 (原始文件夹中的声音)
非常感谢,
package ro.ovidiuconeac.horizontalviewswiping;
import ro.ovidiuconeac.horizontalviewswiping.R;
import android.app.Activity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CustomPageChangeListener implements OnPageChangeListener,OnClickListener {
private Button buttonBlue = null;
private Button buttonYellow = null;
private Button buttonRed = null;
private Activity context = null;
/**
* Constructor
*/
public CustomPageChangeListener(Activity context) {
this.context = context;
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int position) {
switch (position) {
case 0 : {
buttonBlue = (Button) context.findViewById(R.id.buttonBlue);
buttonBlue.setOnClickListener(this);
break;
}
case 1 : {
buttonYellow = (Button) context.findViewById(R.id.buttonYellow);
buttonYellow.setOnClickListener(this);
break;
}
case 2 : {
buttonRed = (Button) context.findViewById(R.id.buttonRed);
buttonRed.setOnClickListener(this);
break;
}
}
}
public void onClick(View v) {
// Click button blue
if (buttonBlue != null && v.getId() == buttonBlue.getId()) {
Toast.makeText(context, "Blue screen", Toast.LENGTH_SHORT).show();
}
// Click button yellow
if (buttonYellow != null && v.getId() == buttonYellow.getId()) {
// Toast.makeText(context, "Yellow screen", Toast.LENGTH_SHORT).show();
}
// Click button red
if (buttonRed != null && v.getId() == buttonRed.getId()) {
Toast.makeText(context, "Red screen", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
首先,您应该将pagescroll.mp3文件放在assets文件夹中,然后您可以使用以下代码:
public static void pageScrollSound(Context context) {
private static MediaPlayer player;
private static AssetFileDescriptor assetFileDescriptor;
try {
assetFileDescriptor = context.getResources().getAssets().openFd("pagescroll.mp3");
} catch (IOException e) {
Log.e(Constant.LOG_TAG, "IOException", e);
}
try {
player = new MediaPlayer();
player.setDataSource(assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(),
assetFileDescriptor.getLength());
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
Log.e(Constant.LOG_TAG, "IllegalArgumentException", e);
} catch (IllegalStateException e) {
Log.e(Constant.LOG_TAG, "IllegalStateException", e);
} catch (IOException e) {
Log.e(Constant.LOG_TAG, "IOException", e);
}
}