Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

java - Android ShoutCast Internet Radio FilenotFoundException

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...