Android媒体播放器异常在10秒后超时

时间:2016-01-30 02:50:20

标签: android performance android-mediaplayer

  1. 在原始文件中设置了mp3
  2. 读取它们并将它们设置为数组
  3. 获取数组大小
  4. 生成一个随机数并将其作为元素传递给媒体播放器
  5. 所以玩家运行
  6. mp3剪辑不到2秒所以我等待2秒并播放下一个剪辑和一个循环,这需要运行直到我停止
  7. 这是个主意 这是代码

    包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剪辑工作正常 当它在25秒内运行时它会停止 有时它会给出这个例外

    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
    

    我需要继续播放媒体播放器

    任何帮助?

    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>
    

1 个答案:

答案 0 :(得分:13)

如果您按原样重复创建release()个实例,则需要在创建下一个播放器之前调用前一个播放器上的release()。引用MediaPlayer docs

  

还建议一旦不再使用MediaPlayer对象,立即调用release(),以便可以立即释放与MediaPlayer对象关联的内部播放器引擎使用的资源。资源可能包括单例资源,例如硬件加速组件,调用失败stack可能导致MediaPlayer对象的后续实例回退到软件实现或完全失败。

虽然您使用单个变量来引用每个新玩家,但旧的实例仍然会在#34;周围徘徊。一段时间,并最终由于没有明确释放而导致所述的异常。