bursk
bursk

Reputation: 1667

Android MediaPlayer eventually causing app to stop

I have an app I'm trying to debug on an issue on regarding use of the media player. I have an Activity displayed with a service polling a server behind it. The Activity is displaying dynamic status that the service is collecting from the server. That was working great. I then added some code in a class the service uses to play an audio file if it sees a particular data item. That works fine for the first few times, but then the app will freeze and can even eventually get to the dreaded force close.
I am seeing the following in the log file. Its the warning that concern me and I can't seem to find any info on them.

11-14 10:12:29.742: WARN/AV(28909): #### Attempting to play audio ####
11-14 10:12:29.742: WARN/com.me.messaging.MessageMediator@43c0c518(28909): #### AV - Playing audio ####
11-14 10:12:29.772: INFO/QC_CORE(103): OMXCORE API - OMX_Init 
11-14 10:12:29.772: INFO/QC_CORE(103): OMXCORE API - OMX_ComponentNameEnum 5bf30 128 0
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.decoder.avc
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.decoder.avc
11-14 10:12:29.772: INFO/QC_CORE(103): OMXCORE API - OMX_ComponentNameEnum 5bf30 128 1
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.decoder.mpeg4
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.decoder.mpeg4
11-14 10:12:29.772: INFO/QC_CORE(103): OMXCORE API - OMX_ComponentNameEnum 5bf30 128 2
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.decoder.h263
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.decoder.h263
11-14 10:12:29.772: INFO/QC_CORE(103): OMXCORE API - OMX_ComponentNameEnum 5bf30 128 3
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.encoder.mpeg4
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.encoder.mpeg4
11-14 10:12:29.772: INFO/QC_CORE(103): OMXCORE API - OMX_ComponentNameEnum 5bf30 128 4
11-14 10:12:29.772: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.encoder.h263
11-14 10:12:29.782: INFO/QC_CORE(103): GetRolesOfComponent OMX.qcom.video.encoder.h263
11-14 10:12:29.782: INFO/QC_CORE(103): OMXCORE API - OMX_ComponentNameEnum 5bf30 128 5
11-14 10:12:29.862: WARN/MediaPlayer(28909): info/warning (1, 44)
11-14 10:12:29.862: INFO/MediaPlayer(28909): Info (1,44)

I am using the media player like this:

private void playMedia(int     audioResource,
                       boolean vibrate)
   {
       Log.i("AV", "#### Attempting to play audio ####");

       if (this.lastAlert + VIBRATION_LENGTH <= System.currentTimeMillis())
       {
           this.lastAlert = System.currentTimeMillis();

           Log.i(this.toString(), "#### AV - Playing audio ####");

           try
           {    
              MediaPlayer mediaPlayer = new MediaPlayer();
              AssetFileDescriptor assetFileDescriptor 
                 = this.context.getResources().openRawResourceFd(audioResource);
              mediaPlayer.setOnCompletionListener(this);
              mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                                        assetFileDescriptor.getStartOffset(),
                                        assetFileDescriptor.getLength());
              mediaPlayer.prepare();
              mediaPlayer.start();
           }
           catch (Exception e)
           {
               Log.e(this.toString(), e.toString());
               e.printStackTrace();
           }

           if (vibrate)
           {
              if ( null != this.vibrator )
              {
                 this.vibrator.vibrate(VIBRATION_LENGTH);
              }
           }
       }
       else
       {
           Log.w(this.toString(), "##### AV - Audio is already playing #####");
       }
   }// end playMedia

   @Override
   public void onCompletion(MediaPlayer mediaPlayer)
   {
      mediaPlayer.reset();
      mediaPlayer.release();

      Log.i(this.toString(), "#### ################################### ####");
      Log.i(this.toString(), "#### AV - reset and release media player ####");

   }

The audio files are mp3 format.

Upvotes: 0

Views: 1783

Answers (1)

Michael P
Michael P

Reputation: 690

You have to call close on AssetFileDescriptor each time You set it as data source. Even SDK doc says that You can safely release a file descriptor after call to the setDataSource.

Upvotes: 3

Related Questions