2014-01-18 41 views
-4

我只是想在android上開發一個mp3切割器應用程序。我只是做了一些事情來做到這一點。但它不起作用。我打包播放MP3,我在輸入流中存儲洞mp3,然後我使用輸出流來寫mp3。但它不工作...我只是試圖在android中實現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); 

} 

}

+0

在哪裏,究竟是「不工作」?附加logcat輸出。 – Srikanth

+1

請定義「不工作」由什麼組成,並嘗試將代碼示例縮減到相關部分。 – marko

回答

3

雖然有可能在你的代碼甚至更多的bug,最關鍵的是該MP3文件格式的誤解。

MP3文件並不只包含音頻數據,但實際上多個MP3 frames每個都用自己的頭和數據部分。因此,你不能在中間切割現有的MP3文件,並期望結果是兩個可播放的MP3文件。

看一看MP3的規格在這裏:http://en.wikipedia.org/wiki/MP3

相關問題