我只是尝试在android中实现mp3切割器......但它无法正常工作

时间:2014-01-18 09:59:20

标签: java android

我只是想在android上开发一个mp3切割器应用程序。我刚刚做了一些事情。但它没有用。我很喜欢播放mp3并且我将输入流中存储了mp3,然后我使用输出流来编写mp3。但它没有用......

public class second extends Activity {
SeekBar bar;
String path;
Runnable r;
MediaPlayer mp = new MediaPlayer();
Handler handler = new Handler();
public static final String MEDIA_PATH = new String(Environment
        .getExternalStorageDirectory().getPath() + "/Music/");
int length;
TextView time, time1, time2;
int i = 0;
int start, end;
Button cutMp3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    bar = (SeekBar) findViewById(R.id.seekBar1);
    Bundle b = getIntent().getExtras();
    path = b.getString("path");
    time = (TextView) findViewById(R.id.time);
    time1 = (TextView) findViewById(R.id.textView1);
    time2 = (TextView) findViewById(R.id.textView2);
    cutMp3 = (Button) findViewById(R.id.button1);

    try {
        mp.reset();
        mp.setDataSource(MEDIA_PATH + path);
        mp.prepare();
        mp.start();
        File mp3 = new File(MEDIA_PATH + path);
        InputStream is = new FileInputStream(mp3);
        final BufferedInputStream bis = new BufferedInputStream(is);
        int numBytes = bis.available();
        final byte[] buf = new byte[numBytes];
            Toast.makeText(getApplicationContext(),
                "num of bytes: " + numBytes, Toast.LENGTH_SHORT).show();

        Toast.makeText(getApplicationContext(), "" + mp3.length(),
                Toast.LENGTH_SHORT).show();

        final File file = new File(MEDIA_PATH + path);
        Toast.makeText(getApplicationContext(), "mjmj" + file.length(),
                Toast.LENGTH_SHORT).show();

        length = mp.getDuration();

        bar.setMax(length);
        bar.setClickable(true);
        bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                ++i;
                if (i == 1) {
                    time1.setText("" + mp.getCurrentPosition() / 1000);
                    start = mp.getCurrentPosition();
                }
                if (i == 2) {
                    time2.setText("" + mp.getCurrentPosition() / 1000);
                    end = mp.getCurrentPosition();
                    i = 0;
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar arg0, int arg1,
                    boolean arg2) {
                // TODO Auto-generated method stub
                mp.seekTo(arg1);

            }
        });
        r = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                updateSeekbar();
            }
        };
        r.run();

        cutMp3.setOnClickListener(new View.OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    Log.d("chk", "read bytes");
                    bis.read(buf, start, end - start);
                    Log.d("chk", "end of read");
                Log.d("chk", "toast start");
//  Toast.makeText(getApplicationContext(),"" + bis.read(buf, start,     end - start),
Toast.LENGTH_SHORT).show();
                    Log.d("chk", "toast end");
                    final byte[] buf1 = new byte[end - start];
                    ByteArrayBuffer baf = new ByteArrayBuffer(end - start);
                    try {
                        File file = new File("new.mp3");

                        if (file.createNewFile()) {
                            Toast.makeText(getApplicationContext(),
                                    "file created", Toast.LENGTH_SHORT)
                                    .show();
                        }

                    }

                    catch (Exception e) {

                    }

                    for (byte b : buf1) {
                        FileOutputStream fos = new FileOutputStream(file);
                        fos.write(buf1);
                        fos.close();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        // Toast.makeText(getApplicationContext(), ""+length,
        // Toast.LENGTH_SHORT).show();

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

protected void updateSeekbar() {
    // TODO Auto-generated method stub

    bar.setProgress(mp.getCurrentPosition());
    handler.postDelayed(r, 1000);
    time.setText("" + mp.getCurrentPosition() / 1000);

}

}

1 个答案:

答案 0 :(得分:3)

虽然您的代码中可能存在更多错误,但最关键的是对MP3文件格式的误解。

MP3文件不仅包含音频数据,还包含多个MP3 frames,每个MP3 {{1}}都带有自己的标题和数据部分。因此,您不能只在中间剪切现有的MP3文件,并期望结果是两个可播放的MP3文件。

在这里查看MP3规范:http://en.wikipedia.org/wiki/MP3