1. Are you ready for the Galaxy S20? Here is everything we know so far!

Problem with MediaPlayer.create() when recreating with same files after mediaPlayer.release()

Discussion in 'Android Development' started by AndroidGUIIsComplicated, Oct 13, 2020.

  1. AndroidGUIIsComplicated

    Thread Starter

    I have a simple media player app that works fine for a while (like each song will play about 50 times) and then starts to crash on almost all the media files (a few still work but lag really bad; there is no lag until the errors). These errors continue even after stopping the activity and reopening!

    These same media files work with other media players after they quit working in mine, so there is clearly something very wrong. I have to restart my device for my Activity to be able to call MediaPlayer.create() on the files without errors again. This behavior is so obscure!

    One create:
    Code (Text):
    1.  
    2. E/MediaPlayer: error (1, -38)
    3. E/MediaPlayer: error (1, -2147483648)
    4. E/MediaPlayer: Error (1,-38)
    5. E/MediaPlayer: error (1, -2147479551)
    6. E/MediaPlayer: error (1, -38)
    7.  
    Another:
    Code (Text):
    1.  
    2. E/MediaPlayer: error (1, -2147479551)
    3. E/MediaPlayer: Error (1,-2147479551)
    4. E/MediaPlayer: error (1, -38)
    5.  
    Yet another:
    Code (Text):
    1.  
    2. E/MediaPlayer: error (1, -38)
    3. E/MediaPlayer: error (1, -2147479551)
    4. E/MediaPlayer: error (1, -38)
    5. E/MediaPlayer: Error (1,-38)
    6.  
    So the errors are inconsistent in order, but there are recurring numbers. One of the numbers are not in the MediaPlayer.java file for the target SDK 30, but the device I am testing is using Android 6.0.1 which I believe is SDK 23. I am not entirely sure how to find the relevent Android source. Using Google search "site:https://android.googlesource.com/ mediaplayer.java" is the best way I have found.

    I did find
    Code (Text):
    1. MEDIA_ERROR_SYSTEM = -2147483648
    and
    Code (Text):
    1. MEDIA_ERROR_UNKNOWN = 1
    . I have an unknown media error from the system.

    What about -2147479551? It appears to be MEDIA_ERROR_SYSTEM | 0x1001. 0x1001 does not appear in MediaPlayer.java. It is another unknown media error. What extra information was attached? What about -38?!

    What could cause such a bug. I don't even know how to wrap my head around this behavior. Is MediaPlayer not releasing correctly? Am I leaking resources that don't go away when the app closes? My code is fairly simple and it appears to release everything like it is supposed to. How come a restart fixes the issue?

    Here is the relevant code:
    Code (Text):
    1.  
    2.     Playlist masterPlaylist;
    3.     public boolean surfaceCreated;
    4.     AudioURI currentSong;
    5.     AudioURI nextSong;
    6.     MediaPlayer mediaPlayer;
    7.     MediaPlayer nextMediaPlayer;
    8.     VideoView videoView;
    9.     final Object lock = new Object();
    10.  
    11.     MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
    12.         @Override
    13.         public void onCompletion(MediaPlayer mediaPlayer2) {
    14.             synchronized (lock) {
    15.                 Log.i(TAG, "onCompletion started");
    16.                 currentSong = nextSong;
    17.                 mediaPlayer.release();
    18.                 mediaPlayer = nextMediaPlayer;
    19.                 if (surfaceCreated) {
    20.                     mediaPlayer.setDisplay(videoView.getHolder());
    21.                     mediaPlayer.start();
    22.                 }
    23.                 nextSong = masterPlaylist.next();
    24.                 executorService.submit(runnableNextMediaPlayer);
    25.                 Log.i(TAG, "onCompletion ended");
    26.             }
    27.         }
    28.     };
    29.  
    30.     View.OnClickListener onClickListener = new View.OnClickListener() {
    31.         @Override
    32.         public void onClick(View view) {
    33.             Log.i(TAG, "onClick started");
    34.             currentSong = nextSong;
    35.             mediaPlayer.release();
    36.             mediaPlayer = nextMediaPlayer;
    37.             if (surfaceCreated) {
    38.                 mediaPlayer.setDisplay(videoView.getHolder());
    39.                 mediaPlayer.start();
    40.             }
    41.             nextSong = masterPlaylist.next();
    42.             executorService.submit(runnableNextMediaPlayer);
    43.             Log.i(TAG, "onClick ended");
    44.         }
    45.     };
    46.  
    47.     ExecutorService executorService = Executors.newSingleThreadExecutor();
    48.  
    49.     Runnable runnableNextMediaPlayer = new Runnable() {
    50.         @Override
    51.         public void run() {
    52.             synchronized (lock) {
    53.                 Log.i(TAG, "creating next MediaPlayer");
    54.                 nextMediaPlayer = MediaPlayer.create(getApplicationContext(), nextSong.getUri());                nextMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    55.                 nextMediaPlayer.setOnCompletionListener(onCompletionListener);
    56.                 Log.i(TAG, "next MediaPlayer created");
    57.             }
    58.         }
    59.     };
    60.  
    61.     public void calledAtTheEndOfOnCreate() {
    62.         videoView = findViewById(R.id.video_view);
    63.         currentSong = masterPlaylist.next();
    64.         mediaPlayer = MediaPlayer.create(getApplicationContext(), currentSong.getUri());
    65.         mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    66.         mediaPlayer.setOnCompletionListener(onCompletionListener);
    67.         nextSong = masterPlaylist.next();
    68.         executorService.submit(runnableNextMediaPlayer);
    69.         videoView.getHolder().addCallback(new SurfaceHolder.Callback() {
    70.             @Override
    71.             public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
    72.                 surfaceCreated = true;
    73.                 mediaPlayer.setDisplay(surfaceHolder);
    74.                 mediaPlayer.start();
    75.             }
    76.  
    77.             @Override
    78.             public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {}
    79.  
    80.             @Override
    81.             public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {surfaceCreated = false;}
    82.         });
    83.         FloatingActionButton floatingActionButton = findViewById(R.id.fab);
    84.         floatingActionButton.setOnClickListener(onClickListener);
    85.     }
    86.  
     


Loading...
Similar Threads - Problem MediaPlayer create
  1. RLScott
    Replies:
    0
    Views:
    118
  2. lionheart8329
    Replies:
    3
    Views:
    230
  3. wastar0
    Replies:
    0
    Views:
    138
  4. Daronee
    Replies:
    0
    Views:
    903
  5. Smitherten
    Replies:
    3
    Views:
    245
  6. Eddy155
    Replies:
    3
    Views:
    417
  7. mike WRIGHT
    Replies:
    2
    Views:
    230
  8. Tim W
    Replies:
    0
    Views:
    254
  9. Kiran123456789
    Replies:
    6
    Views:
    341
  10. Kiran123456789
    Replies:
    0
    Views:
    110

Share This Page

Loading...