0

In my activity there is a timer which send a message to a handler every second which changes a picture.

Every time user clicks the image a sound is played in onClickListner function of activity using this code:

MediaPlayer.create(this, R.raw.voice).start();

On a xperia arc (Android 4.0.4) the sound is not completely played and as soon as the timer triggers it stops. If I disable the timer there is no problem.

On a HTC Legend (Android 2.2) there is no problem. The sound is played completely.

I can't figure out the problem.

public class ActivityTest extends Activity implements View.OnClickListener, ViewSwitcher.ViewFactory
{

    ImageSwitcher switcher;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_listen);

        switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        switcher.setFactory(this);
        switcher.setOnClickListener(this);

        final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                switcher.setImageDrawable(getResources().getDrawable(R.drawable.image));
                // if this line is commented it works fine
            }
        };

        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run()
            {
                handler.sendEmptyMessage(0);
            }
        }, 0, 1000);
    }

    @Override
    public void onClick(View view)
    {
        MediaPlayer.create(this, R.raw.voice).start();
    }


    @Override
    public View makeView()
    {
        ImageView image = new ImageView(this);
        image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        image.setLayoutParams(new ImageSwitcher.LayoutParams(ImageSwitcher.LayoutParams.FILL_PARENT, ImageSwitcher.LayoutParams.FILL_PARENT));
        return image;
    }
}

Solution -------------------------------------

I moved the declaration of MediaPlayer to class and it fixed the problem.

    MediaPlayer mp;
    @Override
    public void onClick(View view)
    {
        mp =MediaPlayer.create(this, R.raw.voice);
        mp.start();
    }

I don't know why the previous code works on 2.2 but not 4. anyway it worked.

4

1 回答 1

1

因为你演奏的声音很短,所以我推荐一个音池。Soundpool 剪辑保存在内存中,以便快速访问,因为内存很小。我假设您播放的声音很短。在 onCreate 执行此操作以预加载它:

// Load the sound
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
 soundID= soundPool.load(this, R.raw.voice, 1);

  soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                int status) {
            loaded = true;
        }
    });

然后在 onclick 方法中(当用户单击图像时)执行以下操作:

AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            float actualVolume = (float) audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC);
            float maxVolume = (float) audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            float volume = actualVolume / maxVolume;

            if (loaded) {
                soundPool.play(soundID, volume, volume, 1, 0, 1f);
}

其中 soundPool 被声明为实例变量 private SoundPool soundPool 或类似的东西。试试看,让我知道会发生什么。

于 2013-04-30T20:20:45.013 回答