1

i'm new to android and creating an image gallery project

I've create a rotating image gallery successfully

Now i wish to play a sound file but what is happening is that as soon as the image is changed, the music interrupts or stops

I've tried to play the music in different thread but of no use. Here is my code:

public class MainActivity extends Activity {
Timer timer;
TimerTask task;
int cnt = -1;
ImageView icon;        

String[] images = {"alienaquacamera", "alienaquadesktop","alienaquaexcel"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    icon = (ImageView) findViewById(R.id.img);        

    MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.ghatotkach);
    mPlayer.start();

    final Handler mHandler = new Handler();

    // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            changeImage();
        }
    };        

    timer = new Timer();

    task = new TimerTask() {

        @Override
        public void run() {
            mHandler.post(mUpdateResults);
        }
    };

    timer.schedule(task, 0, 3000);
}

public void changeImage(){


    ++cnt;

    if(cnt==images.length)
        cnt = 0;

    Resources res = getResources();

    String drawableName = images[cnt];

    int resID = res.getIdentifier(drawableName , "drawable", getPackageName());
System.out.println("cnt = " + cnt + " , image = " + drawableName + " , ID = " + resID);
    if(resID>0)
        icon.setImageResource(resID);
}
}

I'm not able to understand what is wrong

0 Answers0