我想用 soundpool 播放声音文件。我在播放音频文件时收到错误消息。(“不幸的是,galeri 的进程已停止”)如何在滑动页面时播放短声音?谢谢。
代码是;
package com.example.galeri;
import com.example.galeri.R;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
public class MyPagerAdapter extends PagerAdapter{
private static final Context Context = null;
SoundPool snd;
int sescal;
public int getCount() {
return 6;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
snd = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
sescal = snd.load(Context, R.raw.ayna, 1);
View view = null;
int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;
break;
case 1:
resId = R.layout.left; // second
view = inflater.inflate(resId, null);
snd.play(sescal, 0.9f, 0.9f, 1, -1, 1f);
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
case 5:
resId = R.layout.other;
break;
}
((ViewPager) collection).addView(view, 0);
return view;
}
private void setVolumeControlStream(int streamMusic) {
// TODO Auto-generated method stub
}
private void setVolumeControlStream(int streamMusic) {
// TODO Auto-generated method stub
}
@SuppressWarnings("unused")
private Context getBaseContext() {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("unused")
private PagerAdapter findViewById(int myfivepanelpager) {
// TODO Auto-generated method stub
return null;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
public static Integer getItem(int position) {
// TODO Auto-generated method stub
return null;
}
}
日志猫;
09-07 18:37:28.169: E/AndroidRuntime(653): FATAL EXCEPTION: main
09-07 18:37:28.169: E/AndroidRuntime(653): java.lang.NullPointerException
09-07 18:37:28.169: E/AndroidRuntime(653): at android.media.SoundPool.load(SoundPool.java:191)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.example.galeri.MyPagerAdapter.instantiateItem(MyPagerAdapter.java:30)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.populate(ViewPager.java:821)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer.doFrame(Choreographer.java:525)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.os.Handler.handleCallback(Handler.java:615)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.os.Looper.loop(Looper.java:137)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-07 18:37:28.169: E/AndroidRuntime(653): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 18:37:28.169: E/AndroidRuntime(653): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 18:37:28.169: E/AndroidRuntime(653): at dalvik.system.NativeStart.main(Native Method)