合并MP3文件 - 将Java代码转换为Android

3

我正在尝试制作一个可以将两个MP3文件合并并保存到安卓SD卡上的程序。我有一段Java代码是有效的,但当我尝试将其转换为安卓时,会出现一些错误。

以下是Java代码。它运行得非常完美。

import java.io.*;
public class TuneDoorJava {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fistream1 = new FileInputStream("F:\\aa.mp3");  // first source file
        FileInputStream fistream2 = new FileInputStream("F:\\bb.mp3");//second source file
        SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
        FileOutputStream fostream = new FileOutputStream("F:\\final.mp3");//destinationfile

        int temp;

        while( ( temp = sistream.read() ) != -1)
        {
            // System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write(temp);   // to write to file
        }
        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
    }

在Android中,我想做的是:
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      //  FileOutputStream fostream=null;
        FileInputStream fist=(FileInputStream)getResources().openRawResource(R.raw.t);
        FileInputStream fist2=(FileInputStream)getResources().openRawResource(R.raw.v);

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/dir1");
        dir.mkdirs();
        File file = new File(dir, "filename");

       //FileInputStream fistream1 = new FileInputStream();  // first source file
        //FileInputStream fistream2 = new FileInputStream("F:\\bb.mp3");//second source file
        SequenceInputStream sistream = new SequenceInputStream(fist, fist2);

        FileOutputStream fostream = new FileOutputStream(file);

        int temp;
        while( ( temp = sistream.read() ) != -1)
        {
            // System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write(temp);   // to write to file
        }
        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
}

5
“Some error”... 是什么错误?提供完整的堆栈跟踪信息会更有助于您的问题。 - FThompson
"以下是Java代码。它完美地运行着。" 真的吗?我很惊讶将两个MP3文件的字节连接起来竟然能产生一个有效的输出文件! - Andrew Thompson
是的,它正在工作!……认真的。 - Rafi ullah
2个回答

1

- 给予此权限 WRITE_EXTERNAL_STORAGE

以下是我项目中的可用代码:

public class ConcateSongActivity extends Activity {
    Button mbutt;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mbutt = (Button)findViewById(R.id.button_Click_Karo);
        mbutt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                try {
                    FileInputStream fis1 = new FileInputStream("/sdcard/viv0.wav");
                    FileInputStream fis2 = new FileInputStream("/sdcard/viv1.wav");
                    SequenceInputStream sis = new SequenceInputStream(fis1,fis2);


                    FileOutputStream fos = new FileOutputStream(new File("/sdcard/vis.wav"));

                    int temp;

                    try {
                        while ((temp = sis.read())!= -1){

                            fos.write(temp);

                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }




                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
        });
    }
}

2
"这是我项目中的可用代码:" 该实现与原始代码一样脆弱。当 viv0.wav 是22.05千赫单声道8位,而 viv1.wav 是11.025千赫16位立体声时,它将失败。 - Andrew Thompson
MP3文件也是一样的吗? - Rafi ullah

0

您需要在清单文件中添加以下行,以授予您的应用程序写入SD卡的权限:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接