2

I have the media player playing an mp3 when I load my application. But I had to move this application and now every time I load the application this gives a force close error.

The media player is opened like this:

 final MediaPlayer mp = MediaPlayer.create(Splash.this, R.raw.indra);
                   mp.start();

I know its the media player which causes the error as when I comment the lines above out the application works.

Is there any other ways I can try to load the mp3?

Thanks

Edit:

MediaPlayer mp = new MediaPlayer();    
         AssetFileDescriptor descriptor = contex.getAssets().openFd("indra.mp3");
                mp.setDataSource( descriptor.getFileDescriptor(), 
         descriptor.getStartOffset(), descriptor.getLength() );
                descriptor.close();
         mp.prepare();
                    mp.start();

Edit:

try {
        MediaPlayer mp = new MediaPlayer();    
         AssetFileDescriptor descriptor;

            descriptor = contex.getAssets().openFd("indra.mp3");
                mp.setDataSource( descriptor.getFileDescriptor(), 
         descriptor.getStartOffset(), descriptor.getLength() );
                descriptor.close();
         mp.prepare();
                    mp.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Mycoola
  • 1,135
  • 1
  • 8
  • 29
Tommy
  • 759
  • 2
  • 14
  • 25
  • 1
    Telling us it force closed tells us nothing about your problem. Open your LogCat view, cause a force close, and then take a look at what exception is being raised, and at which line in your code the problem is occurring. – Kevin Coppock Mar 07 '11 at 15:42

1 Answers1

3

Just put your file in asset folder n apply this code..

Media Player mp = new MediaPlayer();    

 AssetFileDescriptor descriptor = contex.getAssets().openFd(fileName);
        mp.setDataSource( descriptor.getFileDescriptor(), 
 descriptor.getStartOffset(), descriptor.getLength() );
        descriptor.close();
 mp.prepare();
            mp.start();
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Thanks for this, but I get an error for contex and the fileName. Both say cannot be resolved. Thanks – Tommy Mar 07 '11 at 12:41
  • I have declared Context contex this clears the contex problem. But the filename still shows an error. – Tommy Mar 07 '11 at 12:46
  • I have edited how I have put this in above, this shows with an error stating surround with try/catch. – Tommy Mar 07 '11 at 12:57
  • Okay thanks, I have added it in try and catch but it still shows force close. I have edited how I done it above. Thanks – Tommy Mar 07 '11 at 13:06
  • 2
    i've been trying to get stupid MediaPlayer to play a video ALL DAY. It turns out you need to set the offset and length params. FACEPALM. – eggie5 Jul 01 '11 at 01:46
  • Hello CapDroid can you solve my problem http://stackoverflow.com/questions/14544848/some-time-music-does-not-stop-android – Siddhpura Amit Jan 27 '13 at 06:12