Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android media player exception timed out after 10 seconds

  1. have set of mp3 in raw file
  2. read them and set them in to an array
  3. get the array size
  4. generate a random number and pass that to media player as the element
  5. so player runs
  6. mp3 clips are less than 2 seconds so i wait 2 seconds and play the next clip and kind of a loop this needs to run till i stop

this is the idea here is the code

package com.hello.musicplayer;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;

import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;


public class AndroidMediaPlayer extends Activity {
    // two
    private MediaPlayer mediaPlayer;
    public TextView songName, duration;
    private double timeElapsed = 0, finalTime = 0;
    private int forwardTime = 2000, backwardTime = 2000;
    private Handler durationHandler = new Handler();
    //private SeekBar seekbar;
    private Field[] fields;
    private String name;
    private int resourceID;
    private List<String> songNames;
    //private List<String> songsDisplay;
    private int nameIntRandom;
    private double timeRemaining;
    private int value;
    private int countinitilaze;
    private int pauseChecker=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        songNames = new ArrayList();
    //  songsDisplay =new ArrayList();

        listRaw();
        initializeViews();

        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {


            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                System.out.println("mediaPlayer error");
                initializeViews();
                return true;
            }
        });

//      seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//          @Override
//          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//
//              System.out.println("progress" + (progress));
//              System.out.println("progress final - " + finalTime);
//
//
//          }
//
//          @Override
//          public void onStartTrackingTouch(SeekBar seekBar) {
//
//          }
//
//          @Override
//          public void onStopTrackingTouch(SeekBar seekBar) {
//
//          }
//      });
    }

    private void listRaw() {
        System.out.println("listRaw listRaw");
        fields = R.raw.class.getFields();
        for (int count = 0; count < fields.length; count++) {

            System.out.println("listRaw names.... " + fields[count].getName());

            try {
                //  String songsStringNames =fields[count].getName();
                //songsDisplay.add(songsStringNames);

                resourceID = fields[count].getInt(fields[count]);
                System.out.println("listRaw resourceIDresourceID " + resourceID);
                name = String.valueOf(resourceID);

                songNames.add(name);
                System.out.println("listRaw songNames.size();" +songNames.size());
                songNames.size();

            } catch (IllegalAccessException e) {
                e.printStackTrace();
                System.out.println("catch" + e);
            }
        }

    }


    public void initializeViews(){



        Random randomGenerator = new Random();
        value = randomGenerator.nextInt((songNames.size()));
        System.out.println("AndroidMediaPlayer value -" + value);
        songName = (TextView) findViewById(R.id.songName);

        System.out.println("AndroidMediaPlayer songnames array-" + songNames);

        nameIntRandom = Integer.parseInt(songNames.get(value));


        mediaPlayer = MediaPlayer.create(this, nameIntRandom);
        //finalTime = mediaPlayer.getDuration();



        //duration = (TextView) findViewById(R.id.songDuration);
        //seekbar = (SeekBar) findViewById(R.id.seekBar);
        //songName.setText(fields[value].getName());

        //seekbar.setMax((int) finalTime);
        //seekbar.setClickable(false);

        if (countinitilaze >0)
        {

            if (finalTime <2000){
                play();
            }

            else {
                playingBigClip();
                System.out.println("playing a big clip ");
            }
                    }
        countinitilaze++;
    }



    ///////
    public void play(){

if (pauseChecker == 0 ){

        System.out.println("AndroidMediaPlayer play ");
//  timeElapsed = mediaPlayer.getCurrentPosition();
//  seekbar.setProgress((int) timeElapsed);
try {
    mediaPlayer.start();

    if(!mediaPlayer.isPlaying()){
        initializeViews();

    }

}
catch (Exception e){


    initializeViews();


}


        int delay = 1000;// in ms

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            public void run() {

            //  mediaPlayer.stop();
                initializeViews();
            }
        }, delay);




}


//      if((timeRemaining/10000) < 1000 ){
//          System.out.println("progress fully compleated ");
//          mediaPlayer.stop();
//          initializeViews();
//
//      }

    }


    ///////////////////////////////////// play mp3 song
    public void play(View view) {
        pauseChecker = 0;
        System.out.println("AndroidMediaPlayer play  onclick");


        if (finalTime <2000){
            play();
        }

        else {
            playingBigClip();
        }
        //durationHandler.postDelayed(updateSeekBarTime, 10);
    }


    public void playingBigClip(){
        System.out.println("play  playingBigClip()");

        //mediaPlayer = MediaPlayer.create(this, nameIntRandom);
        mediaPlayer.start();

        //timeElapsed = mediaPlayer.getCurrentPosition();
        //seekbar.setProgress((int) timeElapsed);



        if((timeRemaining/1000) < 1){
            System.out.println("big clip time remains ");

                initializeViews();
            }

    }

    public void pause(View view) {
pauseChecker = 1;
        System.out.println("AndroidMediaPlayer pause  onclick");
        mediaPlayer.pause();

    }


}

mp3 clips are working fine when this runs like in 25 seconds it stops sometimes it gives this exception

java.util.concurrent.TimeoutException: android.media.MediaPlayer.finalize() timed out after 10 seconds
            at android.media.MediaPlayer.native_finalize(Native Method)
            at android.media.MediaPlayer.finalize(MediaPlayer.java:3284)
            at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:194)
            at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:177)
            at java.lang.Thread.run(Thread.java:818)
01-26 08:15:56.410  12699-12720/? E/DBG_POLICYDM﹕ [com.policydm.XDMBroadcastReceiver$2(108/run)] network is unserviceable

i need to keep play the media player

any help ?

XML 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#333333"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/songName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="songName" />

    <ImageView
        android:id="@+id/mp3Image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
         android:padding="30dp"
        android:src="@drawable/music"
        android:background="#ffffff"
        android:layout_margin="30dp" />

    <TextView
        android:id="@+id/songDuration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="songDuration" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/media_rew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:onClick="rewind"
            android:src="@android:drawable/ic_media_rew" />

        <ImageButton
            android:id="@+id/media_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:onClick="pause"
            android:src="@android:drawable/ic_media_pause" />

        <ImageButton
            android:id="@+id/media_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:onClick="play"
            android:src="@android:drawable/ic_media_play" />

        <ImageButton
            android:id="@+id/media_ff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:onClick="forward"
            android:src="@android:drawable/ic_media_ff" />
    </LinearLayout>

</LinearLayout>
like image 916
Charuක Avatar asked Dec 05 '22 19:12

Charuක


1 Answers

When repeatedly creating MediaPlayer instances as you are, you need to call release() on the previous player before creating the next one. Quoting the MediaPlayer docs:

It is also recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether.

Though you're using a single variable for reference to each new player, the old instances are still "hanging around" for a time, and eventually causing the stated Exception due to not being explicitly released.

like image 53
Mike M. Avatar answered Dec 23 '22 13:12

Mike M.