音频文件从raw复制到sd但无法播放

时间:2012-07-28 00:29:55

标签: file audio copy sd-card

我想创建一个android应用程序,它可以将一些音频文件从Eclipse项目的原始文件夹复制到SD卡。文件被复制到SD卡但媒体播放器无法播放下一条消息“播放器不支持这种类型的音频文件”,似乎文件已损坏,但我不知道原因。这是我的代码:

Button copy = (Button) findViewById(R.id.button2);
    copy.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view) {

            File sdCard, directory = null;
            String nombreMP3 = "audio.mp3";
            BufferedInputStream  entrada = null;
            BufferedOutputStream salida = null;
            int len=0;
            sdCard = Environment.getExternalStorageDirectory();
            directory = new File( sdCard.getAbsolutePath() + "/CarpetaAplicacion2");
            if (!directory.exists()){
                directory.mkdir();
            }
            File f = new File(directory, nombreMP3);
            try {                   
                entrada = new BufferedInputStream(getResources().openRawResource(R.raw.dignidad));
                salida = new BufferedOutputStream((new FileOutputStream(f)),4096);
                byte[] buff = new byte[4096];
            while ((len=entrada.read()) > 0){   
                synchronized (buff){
                    salida.write(buff,0,len);
                    salida.flush();}
            }

            } catch (IOException ex) { 
                if( entrada != null){
                    try {
                        entrada.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }}
                if (salida != null) {
              try {
                salida.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }}
            }
            finally{
                if( entrada != null){
                    try {
                        entrada.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }}
                if (salida != null) {
              try {
                salida.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

0 个答案:

没有答案