Timer interrupt MediaPlayer
<p>In my activity there is a timer which send a message to a handler every second which changes a picture.</p>

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

<pre><code>MediaPlayer.create(this, R.raw.voice).start();
</code></pre>

<p>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.</p>

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

<p>I can't figure out the problem.</p>

<pre><code>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;
    }
}
</code></pre>

<p>Solution ------------------------------------- </p>

<p>I moved the declaration of MediaPlayer to class and it fixed the problem.</p>

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

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