0

i have an imageview and button in the xml file . loading the image1 and play song1.mp3 file.when the song is over it should load next image and play the song2.mp3 song it should go on until the last image. button for closing and exit the application.

 import android.media.MediaPlayer;
 import android.media.MediaPlayer.OnCompletionListener;
 import android.os.Bundle;
 import android.widget.ImageView;
 import android.app.Activity;
 import android.content.res.AssetFileDescriptor;

public class MainActivity extends Activity {
 public MediaPlayer mpp;
 final int image[] = {R.drawable.apple,R.drawable.ball,R.drawable.cat};
int i=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            final ImageView img = (ImageView) findViewById(R.id.img);


    String audio[]={"song1.mp3","song2.mp3","song3.mp3"};

   // MediaPlayer mp = new MediaPlayer();

    AssetFileDescriptor descriptor;
            try {
                descriptor = getAssets().openFd(audio[i]);
                mpp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength());
                descriptor.close();
                mpp.prepare();
                mpp.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mpp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer arg0) {
                    // TODO Auto-generated method stub
                    img.setImageResource(image[i]);
                    i++;
                    mpp.start();
                }
            });
}

}

bama
  • 27
  • 6

2 Answers2

0

Hard to say anything without LogCat output but it looks like you have null reference. Before calling img.setImageResource(image[i]) you have to assign ImageView to img variable, if that View is defined in your activity_main.xml you should add following line in onCreate():

img = (ImageView) findViewById(R.id.you_imageview_id);
Leszek
  • 6,568
  • 3
  • 42
  • 53
  • I am getting Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE.Please help.Thanks. – bama Apr 03 '13 at 19:32
  • maybe this answer will be helpful: [link](http://stackoverflow.com/a/3945915/265597), eventually remove some app(if you installed any before) from emulator to make room for new app. – Leszek Apr 03 '13 at 19:38
  • I see filenotfoundException song1.mp3.My songs are on the res/raw folder – bama Apr 03 '13 at 20:36
  • You are using `getAssets()` so your audio files should be in `assets` folder. – Leszek Apr 03 '13 at 20:45
  • Thanks.still not loading the image.Fatal Exception in main.RuntimeException not able to start the activity – bama Apr 03 '13 at 21:00
0

For insufficient memory in test phone (helpful for someone who use the personal phone for development like me)

  1. if the memory is really full then un install some unwanted applications.
  2. If above statement is not true then restart the phone will solve the problem.

I got this error in my Samsung captivate phone. Restart the phone worked all times.

Joseph Selvaraj
  • 2,225
  • 1
  • 21
  • 21