Android的Shoutcast的网络收音机FileNotFoundException异常 [英] Android ShoutCast Internet Radio FilenotFoundException

查看:156
本文介绍了Android的Shoutcast的网络收音机FileNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Andriod的,我能流和播放成功我公司开发Shoutcastinternet无线流媒体( http://123.176.41.8:8256 流URL)。 P>

但问题是:当我执行我的应用程序,我能流和连续播放someextend(20分钟,halfanhour等...),

在此之后,流得到采空(我不是ableto播放流),莫非任何一个可以帮助?

我记录的错误,流得到停止后。

 错误的是:
D / dalvikvm(1238):GC_FOR_MALLOC在80ms的释放51742对象/ 1928072字节(在运行我的实际设备应用程序,我重复这个说法在日志文件中)
 

更新:
 例外:

  04-27 18:31:05.753:V / MediaPlayer的(26201):启动
04-27 18:31:05.753:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.753:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
04-27 18:31:05.753:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.753:E / MediaPlayer的(26201):回调应用
04-27 18:31:05.753:E / MediaPlayer的(26201):从回调回来
04-27 18:31:05.753:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.753:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.757:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.757:V / MediaPlayer的-JNI(26201):复位
04-27 18:31:05.757:V / MediaPlayer的(26201):复位
04-27 18:31:05.757:V / MediaPlayer的-JNI(26201):启动
04-27 18:31:05.757:V / MediaPlayer的(26201):启动
04-27 18:31:05.757:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.757:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
04-27 18:31:05.757:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.757:E / MediaPlayer的(26201):回调应用
04-27 18:31:05.757:E / MediaPlayer的(26201):从回调回来
04-27 18:31:05.757:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.757:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.757:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.757:V / MediaPlayer的-JNI(26201):复位
04-27 18:31:05.757:V / MediaPlayer的(26201):复位
04-27 18:31:05.757:V / MediaPlayer的-JNI(26201):启动
04-27 18:31:05.757:V / MediaPlayer的(26201):启动
04-27 18:31:05.757:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.757:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
04-27 18:31:05.757:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.757:E / MediaPlayer的(26201):回调应用
04-27 18:31:05.757:E / MediaPlayer的(26201):从回调回来
04-27 18:31:05.761:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.761:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.761:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.761:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.761:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.761:V / MediaPlayer的-JNI(26201):复位
04-27 18:31:05.761:V / MediaPlayer的(26201):复位
04-27 18:31:05.761:V / MediaPlayer的-JNI(26201):启动
04-27 18:31:05.761:V / MediaPlayer的(26201):启动
04-27 18:31:05.761:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.761:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
04-27 18:31:05.761:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.761:E / MediaPlayer的(26201):回调应用
04-27 18:31:05.761:E / MediaPlayer的(26201):从回调回来
04-27 18:31:05.765:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.765:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.765:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.765:V / MediaPlayer的-JNI(26201):复位
04-27 18:31:05.765:V / MediaPlayer的(26201):复位
04-27 18:31:05.765:V / MediaPlayer的-JNI(26201):启动
04-27 18:31:05.765:V / MediaPlayer的(26201):启动
04-27 18:31:05.765:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.765:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
04-27 18:31:05.765:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.765:E / MediaPlayer的(26201):回调应用
04-27 18:31:05.765:E / MediaPlayer的(26201):从回调回来
04-27 18:31:05.765:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.765:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.769:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.769:V / MediaPlayer的-JNI(26201):复位
04-27 18:31:05.769:V / MediaPlayer的(26201):复位
04-27 18:31:05.769:V / MediaPlayer的-JNI(26201):启动
04-27 18:31:05.769:V / MediaPlayer的(26201):启动
04-27 18:31:05.769:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.769:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
04-27 18:31:05.769:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.769:E / MediaPlayer的(26201):回调应用
04-27 18:31:05.769:E / MediaPlayer的(26201):从回调回来
04-27 18:31:05.769:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.769:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.769:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.769:V / MediaPlayer的-JNI(26201):复位
04-27 18:31:05.769:V / MediaPlayer的(26201):复位
04-27 18:31:05.773:V / MediaPlayer的-JNI(26201):启动
04-27 18:31:05.773:V / MediaPlayer的(26201):启动
04-27 18:31:05.773:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.773:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
04-27 18:31:05.773:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.773:E / MediaPlayer的(26201):回调应用
04-27 18:31:05.773:E / MediaPlayer的(26201):从回调回来
04-27 18:31:05.773:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.773:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.773:E / setupplayer(26201):java.io.FileNotFoundException:/data/data/com.torilt/cache/downloadingMediaFile591(没有这样的文件或目录)
04-27 18:31:05.773:E / setupplayer(26201):找不到文件。机器人必须有一个清理删除了
04-27 18:31:05.773:E / MediaPlayer的(26201):错误(-38,0)
04-27 18:31:05.773:V / MediaPlayer的-JNI(26201):复位
04-27 18:31:05.773:V / MediaPlayer的(26201):复位
04-27 18:31:05.776:V / MediaPlayer的-JNI(26201):启动
04-27 18:31:05.776:V / MediaPlayer的(26201):启动
04-27 18:31:05.776:E / MediaPlayer的(26201):启动名为状态1
04-27 18:31:05.776:E / MediaPlayer的(26201):收到的消息味精= 100,外部1​​ = -38,EXT2 = 0
 

来源$ C ​​$ C:

 进口java.io.BufferedInputStream中;
进口java.io.BufferedOutputStream;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口java.net.HttpURLConnection中;
进口的java.net.URL;
进口java.net.URLConnection中;
进口的java.util.Calendar;

进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.media.AudioManager;
进口android.media.MediaPlayer;
进口android.os.Handler;
进口android.os.IBinder;
进口android.os.Process;
进口android.util.Log;
/ **
 * MediaPlayer的还不支持的Shoutcast般的外部流
 * URL,这样这个类通过下载提供了一个伪流媒体功能
 *内容增量和放大器;一旦我们得到足够的音频播放中的我们
 *临时存储。
 * /
公共类StreamingMediaPlayer延伸服务{

    最后的静态公共字符串AUDIO_MPEG =音频/ MPEG;
    最后的静态公共字符串BITERATE_HEADER =冰-BR;
    公众诠释INTIAL_KB_BUFFER;
    私人处理程序处理程序;
    // = 96 * 10/8
    最后的公开INT位= 8;
    最终公众诠释秒数= 60;
    INT比特率= ​​56;

    公共文件downloadingMediaFile;
    最终的公共字符串DOWNFILE =downloadingMediaFile;

    公共上下文的背景下;
    公众诠释柜台;
    公众诠释playedcounter;
    公众诠释preparecounter;

    公共MediaPlayer的MP1;
    公共MediaPlayer的MP2;
    公共布尔MP1 prepared;
    公共布尔MP2 prepared;
    公共布尔MP1 preparing;
    公共布尔MP2 preparing;
    公共布尔downloadingformp1;
    公共布尔downloadingformp2;
    公共布尔prepareState;
    公共字符串SONGURL =;

    //玩的就是真的MP1和假的MP2
    公共布尔mp1playing;

    公共布尔开始;
    公共布尔processHasStarted;
    公共布尔processHasPaused;
    公共布尔regularStream;

    公众的BufferedInputStream流;

    公共URL网址;
    公众的URLConnection urlConn;

    公共字符串站;
    公共字符串audiourl;

    公众意向startingIntent = NULL;

    公共布尔停止;
    螺纹preparringthread;

    布尔waitingForPlayer;

    //设置所有的变量
    私人无效setupVars(){
        计数器= 0;
        playedcounter = 0;
        preparecounter = 0;

        MP1 =新的MediaPlayer();
        MP2 =新的MediaPlayer();

        MP1 prepared = FALSE;
        MP2 prepared = FALSE;
        MP1 preparing = FALSE;
        MP2 preparing = FALSE;
        downloadingformp1 = FALSE;
        downloadingformp2 = FALSE;
        prepareState = TRUE;
        mp1playing = FALSE;

        开始= FALSE;
        processHasStarted = FALSE;
        processHasPaused = TRUE;
        regularStream = FALSE;
        流= NULL;

        URL = NULL;
        urlConn = NULL;

        站= NULL;
        audiourl = NULL;

        停止= FALSE;
        preparringthread = NULL;

        waitingForPlayer = FALSE;
    }

    //这个对象将允许其它进程与我们的服务交互
    私人最终IStreamingMediaPlayer.Stub ourBinder =新IStreamingMediaPlayer.Stub(){
        //字符串变量=IStreamingMediaPlayer.Stub;

        公共字符串getStation(){
            // Log.d(TAG,getStation);
            返回站;
        }

        公共字符串的getURL(){
            // Log.d(TAG的getURL);
            返回audiourl;
        }

        公共布尔播放(){
            // Log.d(TAG,玩?);
            返回IsPlaying模块();
        }

        公共布尔暂停(){
            // Log.d(TAG,玩?);
            返回isPause();
        }

        公共无效startAudio(){
            // Log.d(TAG,startAudio);

            可运行R =新的Runnable(){
                公共无效的run(){
                    ONSTART(startingIntent,0);
                }
            };
            新的线程(R)。开始();

        }

        公共无效stopAudio(){
            // Log.d(TAG,stopAudio);
            停止();
        }

    };

    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        上下文=这一点;


    }

    @覆盖
    公共无效ONSTART(意向意图,诠释startId)抛出NullPointerException异常{
        super.onStart(意向,startId);

        //最后一个字符串变量=StreamingMediaPlayer  -  ONSTART;

        上下文=这一点;




        setupVars();

        如果(intent.hasExtra(audiourl)){
            raiseThreadPriority();
            processHasStarted = TRUE;
            processHasPaused = FALSE;

            audiourl = intent.getStringExtra(audiourl);
            站= intent.getStringExtra(站);


            downloadingMediaFile =新的文件(context.getCacheDir(),DOWNFILE +专柜);
            downloadingMediaFile.deleteOnExit();

            可运行R =新的Runnable(){
                公共无效的run(){
                    尝试 {


                        startStreaming(audiourl);

                    }赶上(IOException异常E){
                        // Log.d(TAG,e.toString());
                    }
                }
            };
            线程t =新主题(R);
            t.start();
        }
    }

    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        mp1.stop();
        mp2.stop();
    }



    @覆盖
    公众的IBinder onBind(意向意图){

        startingIntent =意图;
        上下文=这一点;
        返回ourBinder;
    }

    @覆盖
    公共布尔onUnbind(意向意图){
        super.onUnbind(意向);

        stopSelf();

        返回true;
    }

    / **
     * Progressivly媒体下载到一个临时位置,并更新
     * MediaPlayer的新内容变得可用。
     * /
    公共无效startStreaming(最后弦乐mediaUrl)抛出IOException异常{

        尝试 {
            URL =新的URL(mediaUrl);
            urlConn =(HttpURLConnection类)url.openConnection();
            urlConn.setReadTimeout(1000 * 20);
            urlConn.setConnectTimeout(1000 * 5);
            // getContentType方法所使用的方法的getContent确定远程对象的类型;子类可以是方便的覆盖getContentType方法。
            字符串CTYPE = urlConn.getContentType();
            如果(CTYPE == NULL){
                CTYPE =;
            } 其他 {
                CTYPE = ctype.toLowerCase();
            }

            如果(ctype.contains(AUDIO_MPEG)|| ctype.equals()){

                字符串临时= urlConn.getHeaderField(BITERATE_HEADER);

                如果(临时!= NULL){
                    比特率=新的整数(临时).intValue();
                }
            } 其他 {

                stopSelf();
                返回;
            }
        }
        赶上(NullPointerException异常NE)
        {

        }
        赶上(IOException异常IOE){
            // Log.e(TAG,无法连接到+ mediaUrl);
            stopSelf();
            返回;
        }


        如果(!regularStream){

            INTIAL_KB_BUFFER =比特率* SECONDS /位;

            可运行R =新的Runnable(){
                公共无效的run(){
                    尝试 {
                        downloadAudioIncrement(mediaUrl);
                        Log.i(TAG12344444,无法播放);
                        stopSelf();
                        返回;
                    }赶上(IOException异常E){
                        Log.i(TAG123,无法初始化MediaPlayer的音频URL =+ mediaUrl,E);
                        stopSelf();
                        返回;
                    }赶上(NullPointerException异常E){

                        stopSelf();
                        返回;
                    }
                }
            };
            线程t =新主题(R);

            t.start();
        }
    }



    / **
     *下载的URL流到一个临时位置,然后调用
     *的setDataSource为本地文件
     * /
    公共无效downloadAudioIncrement(字符串mediaUrl)抛出IOException异常{

        INT bufsizeForDownload = 8 * 1024;
        INT bufsizeForfile = 64 * 1024;

        流=新的BufferedInputStream(urlConn.getInputStream(),bufsizeForDownload);
        Log.i(BUFSIZE,Integer.toString(urlConn.getInputStream()用()));

        尝试{
            如果(流== NULL || stream.available()== 0){
                stopSelf();
                Log.i(无法创建,流空);
                返回;
            }
        }赶上(NullPointerException异常E){
            stopSelf();
            Log.i(RETURN1,RETURN1);
            返回;
        }


        的BufferedOutputStream回合=新的BufferedOutputStream(新的FileOutputStream(downloadingMediaFile),bufsizeForfile);

        中byte buf [] =新的字节[bufsizeForDownload]
        INT totalBytesRead = 0,totalKbRead = 0,numread = 0;

        做 {
            如果(回合== NULL){
                反++;

                downloadingMediaFile =新的文件(context.getCacheDir(),DOWNFILE +专柜);
                downloadingMediaFile.deleteOnExit();
                布特=新的BufferedOutputStream(新的FileOutputStream(downloadingMediaFile),bufsizeForfile);
            }

            尝试 {

                numread = stream.read(BUF);
            }赶上(IOException异常E){

                Log.d(Downloadingfile,坏读我们不干。);
                // 停止();
                Log.i(RETURN2,RETURN2);
                stopSelf();
                 // 返回;



            }
            赶上(NullPointerException异常E){
                //让我们离开这里吧
                e.printStackTrace();
                打破;
            }

            如果(numread℃,){

                bout.flush();
                stopSelf();


                Log.i(从流中读取错误,从STREAM3坏读);
                如果(流== NULL){
                    urlConn =新的URL(mediaUrl).openConnection();
                    urlConn.setConnectTimeout(1000 * 30);
                    urlConn.connect();
                    流=新的BufferedInputStream(urlConn.getInputStream(),bufsizeForDownload);


                }其他{
                    handler.post(新的Runnable(){
                           公共无效的run(){
                               Log.i(从流中读取错误,从某某不好读);

                              context.stopService(startingIntent);
                               Log.i(return3,return3);
                              返回;
                           }
                        });



                }

            }否则如果(numread> = 1){

                bout.write(BUF,0,numread);

                totalBytesRead + = numread;
                totalKbRead + = totalBytesRead / 1000;
            }

            如果(totalKbRead> = INTIAL_KB_BUFFER和放大器;&放大器;停止=真!){

                bout.flush();

                bout.close();
                布特= NULL;
                如果(开始== FALSE){
                    可运行R =新的Runnable(){
                        公共无效的run(){
                            setupplayer();
                        }
                    };
                    线程t =新主题(R);
                    t.start();
                }

                totalBytesRead = 0;
                totalKbRead = 0;
            }

            如果(停止==真){
                流= NULL;

            }

        }而(流!= NULL);


    }


    / ** oncompletelister媒体播放器** /

    一流的监听器实现MediaPlayer.OnCompletionListener {

        公共无效onCompletion(MediaPlayer的MP){

            waitingForPlayer = FALSE;

            。长timeInMilli = Calendar.getInstance()的getTime()的getTime()。
            长timeToQuit =(1000 * 30)+ timeInMilli; //添加30秒

            如果(mp1playing)
            {
                mp1.reset();
                removefile();
                MP1 prepared = FALSE;
                // Log.d(TAG,MP1是免费的。);
                如果(downloadingformp2){
                    如果(MP2 preparing和放大器;&放大器;停止== FALSE){

                        waitingForPlayer = TRUE;
                    }
                    而(MP2 preparing和放大器;&放大器;停止== FALSE){
                        如果(timeInMilli> timeToQuit){

                            stopSelf();
                        }
                        timeInMilli = Calendar.getInstance()的getTime()的getTime()。
                    }
                }
            } 其他 {
                mp2.reset();
                removefile();
                MP2 prepared = FALSE;

                如果(downloadingformp1){
                    如果(MP1 preparing和放大器;&放大器;停止== FALSE){

                        waitingForPlayer = TRUE;
                    }
                    而(MP1 preparing和放大器;&放大器;停止== FALSE){
                        如果(timeInMilli> timeToQuit){

                            stopSelf();
                        }
                        timeInMilli = Calendar.getInstance()的getTime()的getTime()。
                    }
                }
            }

            如果(waitingForPlayer ==真){
                //我们必须一直在等待
                waitingForPlayer = FALSE;
            }

            如果(停止== FALSE){


                如果(mp1playing){

                    mp2.start();

                    mp1playing = FALSE;
                    可运行R =新的Runnable(){
                        公共无效的run(){
                            setupplayer();
                        }
                    };
                    线程t =新主题(R);
                    t.start();
                } 其他 {

                    mp1.start();

                    mp1playing = TRUE;
                    可运行R =新的Runnable(){
                        公共无效的run(){
                            setupplayer();
                        }
                    };
                    线程t =新主题(R);
                    t.start();
                }
            }
        }
    }

    / **在preparedListener媒体播放器** /

    类preparelistener实现MediaPlayer.On preparedListener {

        公共无效于prepared(MediaPlayer的MP){

            如果(prepareState){
                prepareState = FALSE;
                MP1 preparing = FALSE;
                MP1 prepared = TRUE;

                如果(开始== FALSE){
                    开始= TRUE;

                    mp1.start();
                    mp1playing = TRUE;
                    可运行R =新的Runnable(){
                        公共无效的run(){
                            setupplayer();
                        }
                    };
                    线程t =新主题(R);
                    t.start();
                }
            } 其他 {
                prepareState = TRUE;
                MP2 preparing = FALSE;
                MP2 prepared = TRUE;

            }
        }
    };

    / **
     *设置播放器(S)
     * /
    公共无效setupplayer(){
        最终的字符串变量=setupplayer;

        可运行R =新的Runnable(){
            公共无效的run(){
                尝试 {

                    (!MP1 preparing和放大器;&安培;!MP1 prepared)如果{

                        而(真){
                            downloadingformp1 = TRUE;
                            如果(开始==假)
                                打破;
                            如果(柜> preparecounter)
                                打破;
                        }
                        文件F =新的文件(context.getCacheDir(),DOWNFILE + preparecounter);
                        的FileInputStream插件=新的FileInputStream(F);

                        mp1.setDataSource(ins.getFD());
                        mp1.setAudioStreamType(AudioManager.STREAM_MUSIC); //玩了现场直播


                        mp1.setOnCompletionListener(新监听器());


                        mp1.setOn preparedListener(新preparelistener());


                        如果(开始==虚假|| waitingForPlayer ==真){

                        }



                        。MP1 prepareAsync(); // prepare();
                        MP1 preparing = TRUE;
                        downloadingformp1 = FALSE;
                        preparecounter ++;


                    }否则,如果(MP2 preparing和放大器;!&安培;!MP2 prepared){

                        而(真){
                            downloadingformp2 = TRUE;
                            如果(开始==假)
                                打破;
                            如果(柜> preparecounter)
                                打破;
                        }
                        文件F =新的文件(context.getCacheDir(),DOWNFILE + preparecounter);
                        的FileInputStream插件=新的FileInputStream(F);



                        mp2.setDataSource(ins.getFD());
                        mp2.setAudioStreamType(AudioManager.STREAM_MUSIC);


                        mp2.setOnCompletionListener(新监听器());


                        mp2.setOn preparedListener(新preparelistener());


                        。MP2 prepareAsync();
                        MP2 preparing = TRUE;
                        downloadingformp2 = FALSE;
                        preparecounter ++;

                        //}

                    } 其他
                        Log.d(TAG,没有媒体播放器可设置。);
                        返回;

                }赶上(FileNotFoundException异常E){
                    Log.e(TAG,e.toString());
                    Log.e(TAG,找不到文件的Andr​​oid必须在清理删除了它。);
                    停止();

                    返回;

                }赶上(IllegalStateException异常E){
                    Log.e(TAG,e.toString());
                    停止();

                }赶上(IOException异常E){
                    Log.e(TAG,e.toString());
                    停止();

                }
            }

        };
        preparringthread =新主题(R);
        preparringthread.start();



        尝试 {

            preparringthread.join();
        }赶上(InterruptedException异常E){

            e.printStackTrace();
        }
    }


    私人无效removefile(){

        文件临时=新的文件(context.getCacheDir(),DOWNFILE + playedcounter);

        temp.delete();
        playedcounter ++;
    }


    公共布尔停止(){
        最终的字符串变量=STOP;


        停止= TRUE;
        尝试 {

            如果(mp1.isPlaying()){
                如果(!(流== NULL)){
                    Log.i(在停止,MP1是nill);
                    stopSelf();
                }
                mp1.stop();
            }

            如果(mp2.isPlaying()){
                Log.i(在停止,MP2是nill);

                如果(!(流== NULL)){
                    stopSelf();
                }
                mp2.stop();
            }


        }赶上(例外五){
            Log.e(TAG,错误停止播放器);
        }

        如果(流!= NULL){

            尝试 {
                stream.close();

            }赶上(IOException异常E){
                Log.e(TAG,错误关闭打开的连接);
            }
        }
        流= NULL;

        processHasStarted = FALSE;
        processHasPaused = TRUE;
        如果(preparringthread!= NULL){
            preparringthread.interrupt();
        }

        stopSelf();

        返回true;
    }


    公共布尔IsPlaying模块(){

        返回processHasStarted;
    }

    公共布尔isPause(){

        返回processHasPaused;
    }

    私人无效raiseThreadPriority(){

        Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);

    }
}
 

解决方案

的消息

  GC_FOR_MALLOC在80毫秒释放51742对象/ 1928072字节
 

是不是一个真正的错误,它只是你继续分配内存和释放它迫使垃圾收集器经常运行的标志。这不是很好的性能,但不会导致您的流停下来。

真正的错误文件未发现异常。

通过您的code去很快,我认为正在发生的事情是,你保持存储所有的流数据到一个文件中(不删除的内容已被饰演),因此该文件会无限增大设备最终将用完空间。当它越来越接近,机器人开始清理临时文件,最终包括因为它位于缓存的流媒体文件目录。

我的建议是使用 的PipedInputStream 的PipedOutputStream 代替。你必须确保,以确保使之足够大的缓冲流(但因为这不是太大会在内存中)。但你不必担心文件越来越大。

如果不很好地工作(因为某种原因,你不能缓冲足够的数据在内存中,而不会遇到问题,网络速度的波动流畅播放),那么你可能需要创建自己的InputStream和OutputStream类。也许用两个文件将是最简单的方法(缓冲在两个文件中,来回走这两个之间,当读指针移动到同一个文件中写指针,你知道你可以放弃其他的)。

I developed Shoutcastinternet Radio Streaming(Stream URL:http://123.176.41.8:8256) in Andriod and i'm able to stream and play Successfully.

But the problem is: when i execute my application,I'm able to stream and play Continuously for someextend(20min,halfanhour etc...),

after that the stream is getting stoped(I'm not ableto play the stream),Could any one help?

I logged the Error,after Stream get Stopped.

The Error is:
D/dalvikvm(1238): GC_FOR_MALLOC freed 51742 objects / 1928072 bytes in 80ms(I'm repeating this statement in the log file while running my application on real Device)

Update:
Exception:

  04-27 18:31:05.753: V/MediaPlayer(26201): start
04-27 18:31:05.753: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.753: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0
04-27 18:31:05.753: E/MediaPlayer(26201): error (-38, 0)
04-27 18:31:05.753: E/MediaPlayer(26201): callback application
04-27 18:31:05.753: E/MediaPlayer(26201): back from callback
04-27 18:31:05.753: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.753: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.757: E/MediaPlayer(26201): Error (-38,0)
04-27 18:31:05.757: V/MediaPlayer-JNI(26201): reset
04-27 18:31:05.757: V/MediaPlayer(26201): reset
04-27 18:31:05.757: V/MediaPlayer-JNI(26201): start
04-27 18:31:05.757: V/MediaPlayer(26201): start
04-27 18:31:05.757: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.757: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0
04-27 18:31:05.757: E/MediaPlayer(26201): error (-38, 0)
04-27 18:31:05.757: E/MediaPlayer(26201): callback application
04-27 18:31:05.757: E/MediaPlayer(26201): back from callback
04-27 18:31:05.757: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.757: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.757: E/MediaPlayer(26201): Error (-38,0)
04-27 18:31:05.757: V/MediaPlayer-JNI(26201): reset
04-27 18:31:05.757: V/MediaPlayer(26201): reset
04-27 18:31:05.757: V/MediaPlayer-JNI(26201): start
04-27 18:31:05.757: V/MediaPlayer(26201): start
04-27 18:31:05.757: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.757: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0
04-27 18:31:05.757: E/MediaPlayer(26201): error (-38, 0)
04-27 18:31:05.757: E/MediaPlayer(26201): callback application
04-27 18:31:05.757: E/MediaPlayer(26201): back from callback
04-27 18:31:05.761: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.761: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.761: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.761: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.761: E/MediaPlayer(26201): Error (-38,0)
04-27 18:31:05.761: V/MediaPlayer-JNI(26201): reset
04-27 18:31:05.761: V/MediaPlayer(26201): reset
04-27 18:31:05.761: V/MediaPlayer-JNI(26201): start
04-27 18:31:05.761: V/MediaPlayer(26201): start
04-27 18:31:05.761: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.761: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0
04-27 18:31:05.761: E/MediaPlayer(26201): error (-38, 0)
04-27 18:31:05.761: E/MediaPlayer(26201): callback application
04-27 18:31:05.761: E/MediaPlayer(26201): back from callback
04-27 18:31:05.765: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.765: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.765: E/MediaPlayer(26201): Error (-38,0)
04-27 18:31:05.765: V/MediaPlayer-JNI(26201): reset
04-27 18:31:05.765: V/MediaPlayer(26201): reset
04-27 18:31:05.765: V/MediaPlayer-JNI(26201): start
04-27 18:31:05.765: V/MediaPlayer(26201): start
04-27 18:31:05.765: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.765: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0
04-27 18:31:05.765: E/MediaPlayer(26201): error (-38, 0)
04-27 18:31:05.765: E/MediaPlayer(26201): callback application
04-27 18:31:05.765: E/MediaPlayer(26201): back from callback
04-27 18:31:05.765: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.765: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.769: E/MediaPlayer(26201): Error (-38,0)
04-27 18:31:05.769: V/MediaPlayer-JNI(26201): reset
04-27 18:31:05.769: V/MediaPlayer(26201): reset
04-27 18:31:05.769: V/MediaPlayer-JNI(26201): start
04-27 18:31:05.769: V/MediaPlayer(26201): start
04-27 18:31:05.769: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.769: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0
04-27 18:31:05.769: E/MediaPlayer(26201): error (-38, 0)
04-27 18:31:05.769: E/MediaPlayer(26201): callback application
04-27 18:31:05.769: E/MediaPlayer(26201): back from callback
04-27 18:31:05.769: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.769: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.769: E/MediaPlayer(26201): Error (-38,0)
04-27 18:31:05.769: V/MediaPlayer-JNI(26201): reset
04-27 18:31:05.769: V/MediaPlayer(26201): reset
04-27 18:31:05.773: V/MediaPlayer-JNI(26201): start
04-27 18:31:05.773: V/MediaPlayer(26201): start
04-27 18:31:05.773: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.773: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0
04-27 18:31:05.773: E/MediaPlayer(26201): error (-38, 0)
04-27 18:31:05.773: E/MediaPlayer(26201): callback application
04-27 18:31:05.773: E/MediaPlayer(26201): back from callback
04-27 18:31:05.773: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.773: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.773: E/setupplayer(26201): java.io.FileNotFoundException: /data/data/com.torilt/cache/downloadingMediaFile591 (No such file or directory)
04-27 18:31:05.773: E/setupplayer(26201): Can't find file. Android must have deleted it on a clean up 
04-27 18:31:05.773: E/MediaPlayer(26201): Error (-38,0)
04-27 18:31:05.773: V/MediaPlayer-JNI(26201): reset
04-27 18:31:05.773: V/MediaPlayer(26201): reset
04-27 18:31:05.776: V/MediaPlayer-JNI(26201): start
04-27 18:31:05.776: V/MediaPlayer(26201): start
04-27 18:31:05.776: E/MediaPlayer(26201): start called in state 1
04-27 18:31:05.776: E/MediaPlayer(26201): message received msg=100, ext1=-38, ext2=0

Source Code:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Process;
import android.util.Log;
/** 
 * MediaPlayer does not yet support "Shoutcast"-like streaming from external
 * URLs so this class provides a pseudo-streaming function by downloading the
 * content incrementally & playing as soon as we get enough audio in our
 * temporary storage.
 */
public class StreamingMediaPlayer extends Service {

    final static public String AUDIO_MPEG = "audio/mpeg";
    final static public String BITERATE_HEADER = "icy-br";
    public int INTIAL_KB_BUFFER ;
    private Handler handler;
    //= 96*10/8
    final public int BIT = 8;
    final public int SECONDS = 60;
    int bitrate = 56;

    public File downloadingMediaFile;
    final public String DOWNFILE = "downloadingMediaFile";

    public Context context;
    public int counter;
    public int playedcounter;
    public int preparecounter;

    public MediaPlayer mp1;
    public MediaPlayer mp2;
    public boolean mp1prepared;
    public boolean mp2prepared;
    public boolean mp1preparing;
    public boolean mp2preparing;
    public boolean downloadingformp1;
    public boolean downloadingformp2;
    public boolean prepareState;
    public String SONGURL = "";

    // playing is "true" for mp1 and "false" for mp2
    public boolean mp1playing;

    public boolean started;
    public boolean processHasStarted;
    public boolean processHasPaused;
    public boolean regularStream;

    public BufferedInputStream stream;

    public URL url;
    public URLConnection urlConn;

    public String station;
    public String audiourl;

    public Intent startingIntent = null;

    public boolean stopping;
    Thread preparringthread;

    boolean waitingForPlayer;

    // Setup all the variables
    private void setupVars() {
        counter = 0;
        playedcounter = 0;
        preparecounter = 0;

        mp1 = new MediaPlayer();
        mp2 = new MediaPlayer();

        mp1prepared = false;
        mp2prepared = false;
        mp1preparing = false;
        mp2preparing = false;
        downloadingformp1 = false;
        downloadingformp2 = false;
        prepareState = true;
        mp1playing = false;

        started = false;
        processHasStarted = false;
        processHasPaused = true;
        regularStream = false;
        stream = null;

        url = null;
        urlConn = null;

        station = null;
        audiourl = null;

        stopping = false;
        preparringthread = null;

        waitingForPlayer = false;
    }

    // This object will allow other processes to interact with our service
    private final IStreamingMediaPlayer.Stub ourBinder = new IStreamingMediaPlayer.Stub() {
        // String TAG = "IStreamingMediaPlayer.Stub";

        public String getStation() {
            // Log.d(TAG, "getStation");
            return station;
        }

        public String getUrl() {
            // Log.d(TAG, "getUrl");
            return audiourl;
        }

        public boolean playing() {
            // Log.d(TAG, "playing?");
            return isPlaying();
        }

        public boolean pause() {
            // Log.d(TAG, "playing?");
            return isPause();
        }

        public void startAudio() {
            // Log.d(TAG, "startAudio");

            Runnable r = new Runnable() {
                public void run() {
                    onStart(startingIntent, 0);
                }
            };
            new Thread(r).start();

        }

        public void stopAudio() {
            // Log.d(TAG, "stopAudio");
            stop();
        }

    };

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;


    }

    @Override
    public void onStart(Intent intent, int startId) throws NullPointerException {
        super.onStart(intent, startId);

        // final String TAG = "StreamingMediaPlayer - onStart";

        context = this;




        setupVars();

        if (intent.hasExtra("audiourl")) {
            raiseThreadPriority();
            processHasStarted = true;
            processHasPaused = false;

            audiourl = intent.getStringExtra("audiourl");
            station = intent.getStringExtra("station");


            downloadingMediaFile = new File(context.getCacheDir(), DOWNFILE+ counter);
            downloadingMediaFile.deleteOnExit();

            Runnable r = new Runnable() {
                public void run() {
                    try {


                        startStreaming(audiourl);

                    } catch (IOException e) {
                        // Log.d(TAG, e.toString());
                    }
                }
            };
            Thread t = new Thread(r);
            t.start();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mp1.stop();
        mp2.stop();
    }



    @Override
    public IBinder onBind(Intent intent) {

        startingIntent = intent;
        context = this;
        return ourBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        super.onUnbind(intent);

        stopSelf();

        return true;
    }

    /**
     * Progressivly download the media to a temporary location and update the
     * MediaPlayer as new content becomes available.
     */
    public void startStreaming(final String mediaUrl) throws IOException {

        try {
            url = new URL(mediaUrl);
            urlConn = (HttpURLConnection) url.openConnection();
            urlConn.setReadTimeout(1000 * 20);
            urlConn.setConnectTimeout(1000 * 5);
            //The getContentType method is used by the getContent method to determine the type of the remote object; subclasses may find it convenient to override the getContentType method. 
            String ctype = urlConn.getContentType();
            if (ctype == null) {
                ctype = "";
            } else {
                ctype = ctype.toLowerCase();
            }

            if (ctype.contains(AUDIO_MPEG) || ctype.equals("")) {

                String temp = urlConn.getHeaderField(BITERATE_HEADER);

                if (temp != null) {
                    bitrate = new Integer(temp).intValue();
                }
            } else {

                stopSelf();
                return;
            }
        }
        catch(NullPointerException ne)
        {

        }
        catch (IOException ioe) {
            // Log.e(TAG, "Could not connect to " + mediaUrl);
            stopSelf();
            return;
        }


        if (!regularStream) {

            INTIAL_KB_BUFFER = bitrate * SECONDS / BIT;

            Runnable r = new Runnable() {
                public void run() {
                    try {
                        downloadAudioIncrement(mediaUrl);
                        Log.i("TAG12344444", "Unable to play");
                        stopSelf();
                        return;
                    } catch (IOException e) {
                        Log.i("TAG123", "Unable to initialize the MediaPlayer for Audio Url = "+mediaUrl, e);
                        stopSelf();
                        return;
                    } catch (NullPointerException e) {

                        stopSelf();
                        return;
                    }
                }
            };
            Thread t = new Thread(r);

            t.start();
        }
    }



    /**
     * Download the url stream to a temporary location and then call the
     * setDataSource for that local file
     */
    public void downloadAudioIncrement(String mediaUrl) throws IOException{

        int bufsizeForDownload = 8 * 1024;
        int bufsizeForfile = 64 * 1024;

        stream = new BufferedInputStream(urlConn.getInputStream(),bufsizeForDownload);
        Log.i("bufsize",Integer.toString(urlConn.getInputStream().available()));

        try{
            if(stream == null || stream.available() == 0){
                stopSelf();
                Log.i("unable to create ","stream null");
                return;
            }
        }catch (NullPointerException e) {
            stopSelf();
            Log.i("return1","return1");
            return;
        }


        BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(downloadingMediaFile), bufsizeForfile);

        byte buf[] = new byte[bufsizeForDownload];
        int totalBytesRead = 0, totalKbRead = 0, numread = 0;

        do {
            if (bout == null) {
                counter++;

                downloadingMediaFile = new File(context.getCacheDir(), DOWNFILE+ counter);
                downloadingMediaFile.deleteOnExit();
                bout = new BufferedOutputStream(new FileOutputStream(downloadingMediaFile), bufsizeForfile);
            }

            try {

                numread = stream.read(buf);
            } catch (IOException e) {

                Log.d("Downloadingfile", "Bad read. Let's quit.");
                // stop();
                Log.i("return2","return2");
                stopSelf();
                 // return;



            }
            catch (NullPointerException e) {
                // Let's get out of here
                e.printStackTrace();
                break;
            }

            if (numread < 0) {

                bout.flush();
                stopSelf();


                Log.i("Bad read from stream", "Bad read from stream3");
                if(stream == null){
                    urlConn = new URL(mediaUrl).openConnection();
                    urlConn.setConnectTimeout(1000 * 30);
                    urlConn.connect();
                    stream = new BufferedInputStream(urlConn.getInputStream(),bufsizeForDownload);


                }else{
                    handler.post(new Runnable() {  
                           public void run() {
                               Log.i("Bad read from stream", "Bad read from xyz");

                              context.stopService(startingIntent);
                               Log.i("return3","return3");
                              return;
                           }  
                        });  



                }

            } else if (numread >= 1) {

                bout.write(buf, 0, numread);

                totalBytesRead += numread;
                totalKbRead += totalBytesRead / 1000;
            }

            if (totalKbRead >= INTIAL_KB_BUFFER && stopping != true) {

                bout.flush();

                bout.close();
                bout = null;
                if (started == false) {
                    Runnable r = new Runnable() {
                        public void run() {
                            setupplayer();
                        }
                    };
                    Thread t = new Thread(r);
                    t.start();
                }

                totalBytesRead = 0;
                totalKbRead = 0;
            }

            if (stopping == true) {
                stream = null;

            }

        } while (stream != null);


    }


    /** oncompletelister for media player **/

    class listener implements MediaPlayer.OnCompletionListener {

        public void onCompletion(MediaPlayer mp) {

            waitingForPlayer = false;

            long timeInMilli = Calendar.getInstance().getTime().getTime();
            long timeToQuit = (1000 * 30) + timeInMilli; // add 30 seconds

            if (mp1playing) 
            {
                mp1.reset();
                removefile();
                mp1prepared = false;
                // Log.d(TAG, "mp1 is Free.");
                if (downloadingformp2) {
                    if (mp2preparing && stopping == false) {

                        waitingForPlayer = true;
                    }
                    while (mp2preparing && stopping == false) {
                        if (timeInMilli > timeToQuit) {

                            stopSelf();
                        }
                        timeInMilli = Calendar.getInstance().getTime().getTime();
                    }
                }
            } else {
                mp2.reset();
                removefile();
                mp2prepared = false;

                if (downloadingformp1) {
                    if (mp1preparing && stopping == false) {

                        waitingForPlayer = true;
                    }
                    while (mp1preparing && stopping == false) {
                        if (timeInMilli > timeToQuit) {

                            stopSelf();
                        }
                        timeInMilli = Calendar.getInstance().getTime().getTime();
                    }
                }
            }

            if (waitingForPlayer == true) {
                // we must have been waiting
                waitingForPlayer = false;
            }

            if (stopping == false) {


                if (mp1playing) {

                    mp2.start();

                    mp1playing = false;
                    Runnable r = new Runnable() {
                        public void run() {
                            setupplayer();
                        }
                    };
                    Thread t = new Thread(r);
                    t.start();
                } else {

                    mp1.start();

                    mp1playing = true;
                    Runnable r = new Runnable() {
                        public void run() {
                            setupplayer();
                        }
                    };
                    Thread t = new Thread(r);
                    t.start();
                }
            }
        }
    }

    /** OnPreparedListener for media player **/

    class preparelistener implements MediaPlayer.OnPreparedListener {

        public void onPrepared(MediaPlayer mp) {

            if (prepareState) {
                prepareState = false;
                mp1preparing = false;
                mp1prepared = true;

                if (started == false) {
                    started = true;

                    mp1.start();
                    mp1playing = true;
                    Runnable r = new Runnable() {
                        public void run() {
                            setupplayer();
                        }
                    };
                    Thread t = new Thread(r);
                    t.start();
                }
            } else {
                prepareState = true;
                mp2preparing = false;
                mp2prepared = true;

            }
        }
    };

    /**
     * Set Up player(s)
     */
    public void setupplayer() {
        final String TAG = "setupplayer";

        Runnable r = new Runnable() {
            public void run() {
                try {

                    if (!mp1preparing && !mp1prepared) {

                        while (true) {
                            downloadingformp1 = true;
                            if (started == false)
                                break;
                            if (counter > preparecounter)
                                break;
                        }
                        File f = new File(context.getCacheDir(), DOWNFILE+ preparecounter);
                        FileInputStream ins = new FileInputStream(f);

                        mp1.setDataSource(ins.getFD());
                        mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);//playing for live streaming


                        mp1.setOnCompletionListener(new listener());


                        mp1.setOnPreparedListener(new preparelistener());


                        if (started == false || waitingForPlayer == true){

                        }



                        mp1.prepareAsync();// .prepare();
                        mp1preparing = true;
                        downloadingformp1 = false;
                        preparecounter++;


                    } else if (!mp2preparing && !mp2prepared) {

                        while (true) {
                            downloadingformp2 = true;
                            if (started == false)
                                break;
                            if (counter > preparecounter)
                                break;
                        }
                        File f = new File(context.getCacheDir(), DOWNFILE+ preparecounter);
                        FileInputStream ins = new FileInputStream(f);



                        mp2.setDataSource(ins.getFD());
                        mp2.setAudioStreamType(AudioManager.STREAM_MUSIC);


                        mp2.setOnCompletionListener(new listener());


                        mp2.setOnPreparedListener(new preparelistener());


                        mp2.prepareAsync();
                        mp2preparing = true;
                        downloadingformp2 = false;
                        preparecounter++;

                        // }

                    } else
                        Log.d(TAG, "No Media player is available to setup.");
                        return;

                } catch (FileNotFoundException e) {
                    Log.e(TAG, e.toString());
                    Log.e(TAG,"Can't find file. Android must have deleted it on a clean up ");
                    stop();

                    return;

                } catch (IllegalStateException e) {
                    Log.e(TAG, e.toString());
                    stop();

                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                    stop();

                }
            }

        };
        preparringthread = new Thread(r);
        preparringthread.start();



        try {

            preparringthread.join();
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
    }


    private void removefile() {

        File temp = new File(context.getCacheDir(), DOWNFILE + playedcounter);

        temp.delete();
        playedcounter++;
    }


    public boolean stop() {
        final String TAG = "STOP";


        stopping = true;
        try {

            if (mp1.isPlaying()){
                if (!(stream == null)) {
                    Log.i("IN STOP", "MP1 is nill");
                    stopSelf();
                }
                mp1.stop();
            }

            if (mp2.isPlaying()){
                Log.i("IN STOP", "MP2 is nill");

                if (!(stream == null)){
                    stopSelf();
                }
                mp2.stop();
            }


        } catch (Exception e) {
            Log.e(TAG, "error stopping players");
        }

        if (stream != null) {

            try {
                stream.close();

            } catch (IOException e) {
                Log.e(TAG, "error closing open connection");
            }
        }
        stream = null;

        processHasStarted = false;
        processHasPaused = true;
        if (preparringthread != null) {
            preparringthread.interrupt();
        }

        stopSelf();

        return true;
    }


    public boolean isPlaying() {

        return processHasStarted;
    }

    public boolean isPause() {

        return processHasPaused;
    }

    private void raiseThreadPriority() {

        Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);

    }
}

解决方案

The messages

GC_FOR_MALLOC freed 51742 objects / 1928072 bytes in 80ms

Are not really an error, it's just a sign that you keep allocating memory and releasing it which forces the garbage collector to run often. This is not very good for performance, but not going to cause your streaming to stop.

The real error is the file not found exception.

Going through your code quickly, what I believe is happening is that you keep storing all the streaming data into a file (without deleting what has been played already), so that file grows indefinitely and the device is eventually going to run out of space. When it's getting close to that, Android starts cleaning up temporary files and eventually that includes your streaming file since it is located in the cache directory.

My suggestion would be to use PipedInputStream and PipedOutputStream instead. You'll have to make sure to make sure to make it big enough for buffering the stream (but not too big since this will be in memory). But then you don't have to worry about the file growing.

If that does not work well (because for some reason you cannot buffer enough data in memory to play smoothly without running into problems with the network speed fluctuations), then you might have to create your own InputStream and OutputStream classes. Maybe using two files would be the easiest way (buffer in two files, go back and forth between those two, when the read pointer moves to the same file as the write pointer, you know you can discard the other one).

这篇关于Android的Shoutcast的网络收音机FileNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆