文件InputStream抛出异常我无法播放外部存储中的mp3文件

时间:2016-07-16 12:02:20

标签: android android-studio local-storage android-permissions fileinputstream

我将50k mp3文件放在一个文件夹中,然后将文件夹放在我的外部存储器中,现在我试图通过其外部存储路径中的名称播放mp3文件但是在文件输入流之后,我的代码正在移动到catch例外。这是我的代码

if (Environment.getExternalStorageState()
                        .equals(Environment.MEDIA_MOUNTED)) {
                    String filePath = File.separator + "sdcard" + File.separator + "Android" + File.separator + "media" + File.separator + "convertMp3ToDb" + File.separator + "ldoce6_hwd_gb" + File.separator + fileName;
                    try {
                        FileInputStream fis = new FileInputStream(filePath);
                        String zipFileName = "convertMp3ToDb" + File.separator + "ldoce6_hwd_gb" + File.separator + fileName;
                        String entry = null;
                        while ((entry = fis.toString()) != null) {
                            if (entry.toString().endsWith(zipFileName)) {
                                File Mytemp = File.createTempFile("TCL", "mp3", getActivity().getCacheDir());
                                Mytemp.deleteOnExit();
                                FileOutputStream fos = new FileOutputStream(Mytemp);
                                for (int c = fis.read(); c != -1; c = fis.read()) {
                                    try {
                                        fos.write(c);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                mediaPlayer = new MediaPlayer();
                                fos.close();
                                FileInputStream MyFile = new FileInputStream(Mytemp);
                                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                mediaPlayer.setDataSource(MyFile.getFD());
                                mediaPlayer.prepare();
                                mediaPlayer.start();
                                mediaPlayer.setOnCompletionListener(this);
                                mediaPlayer.setOnErrorListener(this);
                            }
                        }
                        fis.close();

                    } catch (IOException e) {
                        e.printStackTrace();

                    } finally {
                        if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
                            mediaPlayer.stop();
                            mediaPlayer = null;
                        }
                    }
                }

0 个答案:

没有答案