On Sun, Sep 6, 2009 at 2:59 AM, Ludvig
Svenonius<[email protected]> wrote:
>
> I have a rather mysterious problem with the simple test application
> below...
>
> package com.example.helloandroid;
>
> import android.app.Activity;
> import android.media.MediaPlayer;
> import android.os.Bundle;
> import android.widget.ArrayAdapter;
> import android.widget.LinearLayout;
> import android.widget.ListView;
> import android.widget.TextView;
>
> public class HelloAndroid extends Activity {
> static final String[] LIST_ITEMS = new String[] {
> "Item 1",
> "Item 2",
> "Item 3",
> "Item 4",
> "Item 5",
> etc etc...
> };
>
> /** Called when the activity is first created. */
> �...@override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
>
> final LinearLayout root = new LinearLayout(this);
> root.setOrientation(LinearLayout.VERTICAL);
>
> final TextView tv = new TextView(this);
>
> final ListView lv = new ListView(this);
> lv.setAdapter(new ArrayAdapter<String>(this,
> android.R.layout.simple_list_item_1, LIST_ITEMS));
>
> root.addView(tv);
> root.addView(lv);
>
> MediaPlayer mp = new MediaPlayer();
There's your problem.
> String msg = "";
>
> try {
> String path = "/sdcard/MP3/an_mp3_track.MP3";
>
> mp.setDataSource(path);
> mp.prepare();
> mp.start();
> }
> catch (Exception e) {
> msg = e.getClass().getName();
> }
>
> tv.setText(msg);
>
> setContentView(root);
> }
> }
>
>
> As you might see from examining the code, it sets up a very simple
> layout, consisting of a TextView to display a text message above a
> ListView displaying a number of strings. The application also
> instantiates a MediaPlayer and begins playback of an mp3 track on the
> SD card. So far everything works; the interface will be displayed
> correctly and playback of the track will begin... and left on its own
> it will keep working. However, if you start fiddling with the ListView
> - scrolling it or tapping its items, for example - it may cause the
> mp3 playback to suddenly be interrupted, for no apparent reason
> whatsoever.
Your MediaPlayer is a local variable in your onCreate() method, which
is getting garbage collected, at which point playback will stop.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---