媒体播放器被AsyncTask停止 - MediaPlayer在没有发布的情况下完成

时间:2013-02-21 14:13:49

标签: android android-asynctask android-mediaplayer

我通过下载新文件启动我的应用程序,如果有任何开始播放循环的话。然后,我每隔x秒启动一项任务以查找新媒体。一旦调用该任务,视频将停止播放错误 MediaPlayer最终确定而未被释放

它应该下载新媒体,如果有的话,只需更新播放列表。

从它的日志记录我可以看到第一个播放被调用,然后错误出现然后。它只是一次又一次地执行任务。它就像卡在那里一样?

我确定这是一个我在这里失踪的愚蠢问题。我不确定为什么媒体播放器会停止?

MainActivity

public class MainActivity extends Activity implements OnCompletionListener {

    int playListIndex;
    int currentMedia;
    String mediaName;
    ArrayList<Integer> playList = new ArrayList<Integer>();
    File[] filelist;
    private VideoView videoView;
    private ImageView imageView;
    Uri mediaPath;
    private Handler mHandler = new Handler();
    private SurfaceHolder holder;


//  Get this shit from login!
    public String channelid = "5e6299eb-asd6600d58fc9";
    public String username = "tvasd";
    public String linkid = "70casd6600d3f7d7";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

//      File mediadir = getDir("tvr", Context.MODE_PRIVATE);
//        filelist = mediadir.listFiles();
        try {
            downloadMedia(this);
            updateMediaTask(this);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

//       Delete all files in folder
//        if (mediadir.isDirectory()) {
//            String[] children = mediadir.list();
//            for (int i = 0; i < children.length; i++) {
//                new File(mediadir, children[i]).delete();
//            }
//        }
    }
    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.i("media player", "play next please!");
        if (mp != null) {
            mp.release();
        }
//      play next video
        currentMedia++;
        if (currentMedia > playList.size() - 1) {
            currentMedia = 0;
        }
        Log.d("MEDIA_COUNTER", String.format("%d", currentMedia));
        try {
            playMedia(currentMedia);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void playMedia(int playListIndex) throws IOException {
        Log.i("media player", "play media!");
        String path = filelist[playListIndex].getAbsolutePath();
        FileInputStream fileInputStream = new FileInputStream(path);
        final Uri uri = Uri.parse(path);
        String filename = filelist[playListIndex].getName();
        Log.i("FILENAME", path);
        if (filename.contains("image")) {
            imageView = (ImageView)findViewById(R.id.imageView);
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageURI(uri);
            mHandler.postDelayed(new Runnable() {
                public void run() {
                    imageView.setVisibility(View.GONE);
                    imageView.setImageURI(uri);
                    onCompletion(null);
                }
            }, 4000);
        } else if (filename.contains("video")) {

            Log.d("PLAY",String.format("%s", fileInputStream.getFD()));
            Log.i("media player", "play video!");
            SurfaceView surface = (SurfaceView) findViewById(R.id.surface);
            holder = surface.getHolder();
            holder.addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    Log.i("SURFACE", "Created");
                }
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {}
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
            });
            MediaPlayer pl = new MediaPlayer();
            pl.setOnCompletionListener(this);
            pl.setDisplay(holder);
            pl.setDataSource(fileInputStream.getFD());
            pl.prepare();
            pl.start();
        }
    }



    void createPlayList(Context context) {
        ContextWrapper cw = new ContextWrapper(context);
        File mediadir = cw.getDir("tvr", Context.MODE_PRIVATE);
        filelist = mediadir.listFiles();
        if (filelist != null)
        {
            for ( int i = 0;i<filelist.length;i++)
            {
               Log.i("FOKKER", filelist[i].getName());
               Log.i("FOKKER", filelist[i].getAbsolutePath());
            }
        }

        currentMedia = 0;

        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                try {
                    playMedia(currentMedia);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    private void updateMediaTask(final MainActivity a) throws IOException, JSONException {
        int delay = 1000; // delay for 1 sec. 
        int period = 3000; // repeat every 10 minutes. 60000 
        Timer timer = new Timer(); 
        timer.scheduleAtFixedRate(new TimerTask() { 
            public void run()  { 
                new updateFiles(a).execute(getBaseContext(),"update");
            } 
        }, delay, period); 
    }

    private void downloadMedia(Context context) throws IOException, JSONException {
        new updateFiles(this).execute(context,"new");
    }

}

这是 updateFiles 类:

public class updateFiles extends AsyncTask<Object, Integer, Long> {



     public MainActivity activity;

        public updateFiles(MainActivity a) { activity = a;}

    @Override
    protected Long doInBackground(Object... params) {
        Context context = (Context) params[0];
        String method = (String) params[1];
        File mediadir = context.getDir("tvr", Context.MODE_PRIVATE);
        JSONArray channels = Json.getJson("http://192.168.2.136:8080/rest/channel/"+ linkid +"/"+ username, "GET");
        try {
            for (int i=0; i < channels.length(); i++) { 
                JSONObject channel_data = channels.getJSONObject(i);
                String channelID = channel_data.getString("ChannelID").toLowerCase();
                JSONArray json = Json.getJson("http://192.168.2.136:8080/rest/program/"+ linkid +"/"+ username +"/" + channelID, "GET");
                try {
                    for (int j=0; j < json.length(); j++) { 
                        JSONObject json_data = json.getJSONObject(j);
                        String name = json_data.getString("Name").toLowerCase();
                        name = name.replace("-", "_");
                        if (name.contains("mp4") || name.contains("png") || name.contains("jpg") || name.contains("jpeg")) {
                            if (name.contains("mp4")) {
                                name = "/video_"+name;
                            } else if (name.contains("png") || name.contains("jpg") || name.contains("jpeg")) {
                                name = "/image_"+name;
                            }
                            String _name = name.replace("/", "");
                            File file = new File(mediadir, _name);
                            if(file.exists()) {
                                Log.i("FILE FOUND", _name);
                            } else {
                                new Download().execute(context, name, "http://192.168.2.136:8080/rest/transfer/"+ linkid +"/"+ username +"/" + json_data.getString("ID"));
                            }
                        }

                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        Log.i("UPDATEFILES done", method);
        if ( method == "new" ) {
            Log.i("XXX", "This is a new method, so play now");
            activity.createPlayList(context);
        } else {
            Log.i("XXX", "This is a update method, so just update, dont play again. Go on playing");
//          activity.filelist = mediadir.listFiles();
        }
        return null;

    }

}

记录

02-21 16:16:13.429: I/FILE FOUND(24010): video_tvr_webtrailer2.mp4
02-21 16:16:13.429: I/FILE FOUND(24010): video_tvr_consumerintro.mp4
02-21 16:16:13.429: I/JSON(24010): http://192.168.2.136:8080/rest/program/70c8223f-e054-4f33asd3f7d7/asdco/6cfbasdbe-a16600d5ac24
02-21 16:16:13.476: D/JSON result(24010): [{"ChannelID":"6casdd2-47f3-95be-a16600d5ac24","ChannelPath":"\\asda","DateChanged":"2\/15\/2013 10:59:20 AM","DateCreated":"2\/15\/2013 12:58:59 PM","ID":"5e25edasd-b82b-a16600d5f4ef","LastWriteUTC":"\/Date(1352527205000+0200)\/","Name":"Video_05-safety-ccw_mynhardt.wmv","Size":125727228}]
02-21 16:16:13.476: I/JSON(24010): **http://192.asd0/rest/program/7asd4-4f33-9b6b-a1660asd89-be1a-a16600e0bcee**
02-21 16:16:13.523: **D/JSON result(24010): []**
02-21 16:16:13.523: **I/UPDATEFILES done(24010): update**
02-21 16:16:13.523: I/XXX(24010): **This is a update method, so just update, dont play again. Go on playing**

2 个答案:

答案 0 :(得分:1)

很久以前就提出过这个问题了。但问题的解决方案不是异步任务,而是在类中创建MediaPlayer字段,而不是将其作为任何方法的局部变量。

目前在你的情况下它是private void playMedia()方法的一部分。由于这是一种方法,一旦方法完成,它就有资格进行垃圾收集。做这样的事情: -

public class MainActivity extends Activity implements OnCompletionListener {

   private MediaPlayer pl;  // This makes sure mediaplayer is there if activity is alive

答案 1 :(得分:1)

mp.release();无法使用时释放媒体播放器。 它会解决你的问题。

对于前。

mp.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {    
        mp.release();
    }
});