有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java I jsut尝试在安卓中实现mp3 cutter。。。但它不起作用

我只是想在安卓上开发一个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) 个答案

  1. # 1 楼答案

    虽然代码中可能有更多的bug,但最关键的是对MP3文件格式的误解

    一个MP3文件不仅包含音频数据,实际上还包含多个MP3 frames,每个MP3都有自己的头和数据部分。因此,不能只在中间剪断现有的MP3文件,并期望结果是两个可播放的MP3文件。p>

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