开发者

Exception when calling setDataSource(FileDescriptor) method (failed.: status=0x80000000)

I'm developing a video streaming application and I'm getting stuck when calling set setDataSource with a FileDescriptor. I want my application to play the video as it is being downloaded, so once I get a minimum number of bytes, I move those bytes to another file so it can be played in another file while it's being downloaded in the original file.

So, I check if I can start the media palyer every packet like this:

if (mediaPlayer == null) {
                    // Only create the MediaPlayer once we have the minimum
                    // buffered data
                    if (totalKbRead >= INTIAL_KB_BUFFER) {
                        try {
                            startMediaPlayer();
                        } catch (Exception e) {
                            Log.e(getClass().getName(),
                                    "Error co开发者_运维技巧pying buffered conent.", e);
                        }
                    }
                } else if (mediaPlayer.getDuration()
                        - mediaPlayer.getCurrentPosition() <= 1000) {
                    transferBufferToMediaPlayer();
                }
}

This is the startMediaPlayer method code:

private void startMediaPlayer() {
        try {
            File bufferedFile = new File(context.getCacheDir(), "playingMedia"
                    + (counter++) + ".dat");

            // bufferedFile is the one that'll be played
            moveFile(downloadingMediaFile, bufferedFile);

            mediaPlayer = createMediaPlayer(bufferedFile);

            mediaPlayer.start();

            playButton.setEnabled(true);
        } catch (IOException e) {
            Log.e(getClass().getName(), "Error initializing the MediaPlayer.",
                    e);
            return;
}

I move the file with the following code:

public void moveFile(File oldLocation, File newLocation) throws IOException {

        if (oldLocation.exists()) {
            BufferedInputStream reader = new BufferedInputStream(
                    new FileInputStream(oldLocation));
            BufferedOutputStream writer = new BufferedOutputStream(
                    new FileOutputStream(newLocation, false));
            try {
                byte[] buff = new byte[8192];
                int numChars;
                while ((numChars = reader.read(buff, 0, buff.length)) != -1) {
                    writer.write(buff, 0, numChars);
                }
            } catch (IOException ex) {
                throw new IOException("IOException when transferring "
                        + oldLocation.getPath() + " to "
                        + newLocation.getPath());
            } finally {
                try {
                    if (reader != null) {
                        writer.flush();
                        writer.close();
                        reader.close();
                    }
                } catch (IOException ex) {
                    Log.e(getClass().getName(),
                            "Error closing files when transferring "
                                    + oldLocation.getPath() + " to "
                                    + newLocation.getPath());
                }
            }
        } else {
            throw new IOException(
                    "Old location does not exist when transferring "
                            + oldLocation.getPath() + " to "
                            + newLocation.getPath());
        }
    }
}

And I finally create the MediaPlayer object here:

private MediaPlayer createMediaPlayer(File mediaFile) throws IOException {

        if(mediaPlayer != null){
            mediaPlayer.release();
        }

        MediaPlayer mPlayer = new MediaPlayer();
        mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e(getClass().getName(), "Error in MediaPlayer: (" + what
                        + ") with extra (" + extra + ")");
                return false;
            }
        });

        // It appears that for security/permission reasons, it is better to pass
        // a FileDescriptor rather than a direct path to the File.
        // Also I have seen errors such as "PVMFErrNotSupported" and
        // "Prepare failed.: status=0x1" if a file path String is passed to
        // setDataSource().
        FileInputStream fis = new FileInputStream(mediaFile);

        mPlayer.reset();
        FileDescriptor fd = fis.getFD();
        mPlayer.setDataSource(fd);       // IM GETTING THE EXCEPTION HERE
        mPlayer.setDisplay(mHolder);
        mPlayer.prepare();
        return mPlayer;
    }

This is the excepction I get:

01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229): Error initializing the MediaPlayer.
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229): java.io.IOException: setDataSourceFD failed.: status=0x80000000
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at android.media.MediaPlayer.setDataSource(Native Method)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:854)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at org.pfc.utils.StreamingMediaPlayer.createMediaPlayer(StreamingMediaPlayer.java:266)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at org.pfc.utils.StreamingMediaPlayer.startMediaPlayer(StreamingMediaPlayer.java:226)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at org.pfc.utils.StreamingMediaPlayer.access$4(StreamingMediaPlayer.java:203)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at org.pfc.utils.StreamingMediaPlayer$2.run(StreamingMediaPlayer.java:183)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at android.os.Looper.loop(Looper.java:144)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at android.app.ActivityThread.main(ActivityThread.java:4937)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at java.lang.reflect.Method.invoke(Method.java:521)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-25 16:03:15.663: ERROR/org.pfc.utils.StreamingMediaPlayer(2229):     at dalvik.system.NativeStart.main(Native Method)

I've been the whole morning stuck here and I really find no information on that error. Some people have told me to use the file path, but I get the other exception I talk about in the comments (right over the FileInputStream creation).

I'm really lost here, any help would be very appreciated


Don't forgot permission

<uses-permission android:name="android.permission.INTERNET" /> 


Okay, I've arrived to the conclusion that errors like:

Prepare failed.: status=0x1 (when calling prepare() )

and

setDataSourceFD failed.: status=0x80000000 (when calling setDataSourceFD() )

have to do with the file format and probably mean that the file is incomplete, corrupted or something like that...

As I have post in this link, I've found an specific video which works fine while streaming it (though I use setDataSource, not setDataSourceFD), but it'll not work with most of the videos.


From what I have read, certain video file formats have their "header" information on the END of the file. Thus your FD must be support seek function to get the "header" from the end of the file. I suspect your input file to media player fails when it seeks to the "end" of the file.

We are working on the same issues have you gotten further?

Sean


having the same error, and having read the answer above on file format, I abandonded trying to setDataSource with my .mov file and instead created a video with my Android Telefon Camera which gave me an .mp4 file. I put this in the directory Pictures/. This worked - I cound setDataSource without errors. I hope this is useful to someone.

File mediaStorageDir = new         File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),     "MyDirectoryUnderPictures");
File mediaFile_mp4_android;
mediaFile_mp4_android = new File(mediaStorageDir.getPath()
                    + File.separator
                    + "mp4_test"
                    + ".mp4"); //video taken with android camera
String filePath_mp4_android = String.valueOf(mediaFile_mp4_android);
File file_mp4_android = new File(filePath_mp4_android);
Uri contentUri = Uri.fromFile(file_mp4_android);
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(String.valueOf(contentUri));


I also have that problem. When I select the audio from SDCard by library it works, and if I press stop and start again, too. But when restarting the app, and I play without open library, only with url saved, it stops working, and returns that error: 'java.io.IOException: setDataSource failed.: status=0x80000000'

Global Vars:

MediaPlayer media = new MediaPlayer();
TextView idenss;
static private int PICKFILE_RESULT_CODE = 1;
Boolean selectusicFromBworser = false;
Button playGlobal;

My code dialog:

private void viewDialogSounds(final Button sountlabel){
    final View dialogView = View.inflate(this, R.layout.dialoge_selectsouns, null);
    final androidx.appcompat.app.AlertDialog alertDialog = new androidx.appcompat.app.AlertDialog.Builder(this).create();

    alertDialog.setTitle(context.getString(R.string.sound));

    Button exitW, play, pause, sound_set;
    ListView listsounds;
    SoundSteucture strAdapHistory = new SoundSteucture();
    ArrayList<SoundSteucture> structureHistoryArr;
    SoundAdapter _adapterHistory = null;

    final TextView fileSelectedName;

    exitW = (Button) dialogView.findViewById(R.id.exitW);
    play = (Button) dialogView.findViewById(R.id.play);
    pause = (Button) dialogView.findViewById(R.id.pause);
    sound_set = (Button) dialogView.findViewById(R.id.sound_set);
    listsounds = (ListView) dialogView.findViewById(R.id.listsounds);
    fileSelectedName = (TextView) dialogView.findViewById(R.id.fileSelectedName);
    idenss = fileSelectedName;
    playGlobal = play;

    structureHistoryArr = new ArrayList<SoundSteucture>();

    fileSelectedName.setText(sountlabel.getText().toString());

    String [] LabelNames={"A desayunar","A la valenciana","Al currele","Apagas o apagas","Arroz 3 delicias","Clásico e infalible","Con café mejor",
                        "De buen humor","El coche fantástico","El gallo Claudio","Energía positiva","Final destroyer","Fresas con nata","Manos arriba","Profundidad",
                        "Sabanas pegadas","Sax o Phone","Tocando el cielo"};//indices

    final String [] fileNames={"a_desayunar","a_la_valenciana","al_currele","apagas_o_apagas","arroz_3_delicias","clasico_e_infalible","con_cafe_mejor",
                        "de_buen_humor","el_coche_fantastico","el_gallo_claudio","energia_positiva","final_destroyer","fresas_con_nata","manos_arriba","profundidad",
                        "sabanas_pegadas","sax_o_phone","tocando_el_cielo"};//archivos

    if (_adapterHistory != null) {
        _adapterHistory.clear(); //clear se usa solo con los arrayadapter, si fuera simple adapter llevaria removeAllViews
        _adapterHistory.notifyDataSetChanged();
    }
    for (int i=0;i<LabelNames.length;i++){
        strAdapHistory.setNames(LabelNames[i]);
        strAdapHistory.setIdentif(fileNames[i]);
        if(fileSelectedName.getText().toString().equals(fileNames[i].replace("_"," ")))
            strAdapHistory.setCheck("1"); //consultador
        else
            strAdapHistory.setCheck("0");

        structureHistoryArr.add(strAdapHistory);
        strAdapHistory = new SoundSteucture();
    }

    if (structureHistoryArr != null) {
        _adapterHistory = new SoundAdapter(this, structureHistoryArr,pause, play, fileSelectedName, listsounds, PICKFILE_RESULT_CODE);
        listsounds.setAdapter(_adapterHistory);
        _adapterHistory.notifyDataSetChanged();
    }

    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectusicFromBworser = false;
            try {
                if (media != null) {
                    media.stop(); //error
                    media = null;
                    media = new MediaPlayer();
                }
            } catch(Exception e){
                Log.d("Nitif Activity", e.toString());
            }
        }
    });

    final Button pauseFinal = pause;
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!fileSelectedName.getText().toString().equals(getString(R.string.sound))) {
                pauseFinal.callOnClick();

                int resID = 0;
                if (!fileSelectedName.getText().toString().contains("/")) {
                    String tono = fileSelectedName.getText().toString().replace(" ","_");
                    resID = getResources().getIdentifier(tono, "raw", getPackageName());
                    media = MediaPlayer.create(view.getContext(), resID);
                    media.setVolume(1000, 1000);
                    media.start();
                } else {
                    try {
                        if (!selectusicFromBworser) {
                            media.setDataSource(context, Uri.parse(fileSelectedName.getText().toString()));
                        }
                        media.prepareAsync();
                        media.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mediaPlayer) {
                                mediaPlayer.start();
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("eeee???", e.toString());
                    }
                }
            }
        }
    });

    exitW.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pauseFinal.callOnClick();
            alertDialog.dismiss();
            selectusicFromBworser = false;
        }
    });
    sound_set.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectusicFromBworser = false;
            if((!fileSelectedName.getText().toString().equals(""))&&(!fileSelectedName.getText().toString().equals("fileB")))
                sountlabel.setText(fileSelectedName.getText().toString().replace("_"," "));
            pauseFinal.callOnClick();
            alertDialog.dismiss();
        }
    });

    alertDialog.setView(dialogView);
    alertDialog.show();
}

My code ArrayAdapter:

public class SoundAdapter extends ArrayAdapter<SoundSteucture> {
List<SoundSteucture> imageAndTexts1 =null;
Activity activity;
Button pause,play;
TextView fileSelectedName;
ListView listItems;
int PICKFILE_RESULT_CODE;


public SoundAdapter(Activity activity, List<SoundSteucture> imageAndTexts, Button pause, Button play, TextView fileSelectedName, ListView listItems, int PICKFILE_RESULT_CODE) {
    super(activity, 0, imageAndTexts);
    imageAndTexts1 = imageAndTexts;
    this.pause = pause;
    this.play = play;
    this.fileSelectedName = fileSelectedName;
    this.listItems = listItems;
    this.PICKFILE_RESULT_CODE = PICKFILE_RESULT_CODE;
}

static class ViewHolder {
    TextView labelFile;
    TextView fileName;
    ImageView deint;
}

public View getView(final int position, View rowView, ViewGroup parent) {
    activity = (Activity) getContext();

    ViewHolder holder = new ViewHolder();

    if (rowView == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.adapter_sounds, parent, false);

        holder.labelFile = (TextView)rowView.findViewById(R.id.labelFile);
        holder.fileName = (TextView)rowView.findViewById(R.id.fileName);
        holder.deint = (ImageView) rowView.findViewById(R.id.deint);

        rowView.setTag(holder);
    }else {
        holder = (ViewHolder) rowView.getTag();
    }

    final String labelf = imageAndTexts1.get(position).getNames();
    final String labelN = imageAndTexts1.get(position).getIdentif();
    final String check = imageAndTexts1.get(position).getCheck();

    holder.labelFile.setText(labelf);
    holder.fileName.setText(labelN);
    if(check.equals("1")){
        holder.deint.setVisibility(View.VISIBLE);
    }else{
        holder.deint.setVisibility(View.GONE);
    }

    final ViewHolder finalHolder = holder;
    holder.labelFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageView imageList;
            for(int a=0; a < listItems.getCount(); a++){
                try {
                    imageList = (ImageView) listItems.getChildAt(a).findViewById(R.id.deint);
                    imageList.setVisibility(View.GONE);
                }
                catch(Exception e1){ //if not checkBox, null View, etc

                }
            }
            for(int j=0; j < imageAndTexts1.size(); j++){
                imageAndTexts1.get(j).setCheck("0");
            }

            if(check.equals("0")){
                finalHolder.deint.setVisibility(View.VISIBLE);
                imageAndTexts1.get(position).setCheck("1");
            }else{
                finalHolder.deint.setVisibility(View.GONE);
                imageAndTexts1.get(position).setCheck("0");
            }
            fileSelectedName.setText(labelN.replace("_"," "));
            if(!finalHolder.labelFile.getText().equals("fileB"))
                play.callOnClick();
            else
                openfilesFolder();
        }
    });

    return rowView;
}

//files browser
private void openfilesFolder(){
    Intent chooseFile;
    Intent intent;
    chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
    chooseFile.setType("audio/*");
    //chooseFile.setType("*/*");
    intent = Intent.createChooser(chooseFile, activity.getString(R.string.choosefile));
    activity.startActivityForResult(intent, PICKFILE_RESULT_CODE);
}

Activity Result in Principal Activity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICKFILE_RESULT_CODE && resultCode == Activity.RESULT_OK) {
        Uri content_describer = data.getData();
        Log.e("eeee???", "ee->" + data.getData().getPath());

        idenss.setText(content_describer.toString());
        selectusicFromBworser = true;

        try {
            media.setDataSource(this, content_describer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        playGlobal.callOnClick();
    }
}


In my case switching from wav file to mp3 solved this exception with status=0x80000000


In my case the problem was because of beasy sdcard when device was mounted as exteranl storage to pc so checking if the file is available solved the problem. Maybe it helps someone


If you're targeting Marshmallow or greater, make sure that you have requested the Manifest.permission.WRITE_EXTERNAL_STORAGE permission properly. I tried many different solutions, including another library that's an alternative to MediaMetadataRetriever, but it turned out that one of my code paths didn't request the proper permission.


I agree with Pedriyoo, I tried reproducing the exception with different video file formats and out of the following video formats: AVI, MPG/MPEG, MOV, mov, mp4, m4v, flv, WMV, I noticed that AVI, MPG/MPEG, and WMV threw an exception for me every time. Better to exclude them before running the method and wrap it with a try-catch.


I was facing the same issue while loading video from obb extension file. i fixed it by replacing:

mPlayer.setDataSource(fd); 

with:

mPlayer.setDataSource(fis.getFileDescriptor(),fis.getStartOffset(),fis.getLength());

where fis is AssetFileDescriptor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜