2014-01-18 2 views
-4

나는 안드로이드에 mp3 커터 응용 프로그램을 개발하려하고있다. 방금이 일을하기 위해 어떤 일을했습니다. 하지만 작동하지 않습니다. 나는 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); 

} 

}

+0

어떻게 정확하게는 "작동하지 않는"입니다 :

여기에 MP3 사양에서보세요? logcat 출력을 첨부하십시오. – Srikanth

+1

'작동하지 않는 것'이 무엇인지 정의하고 해당 부분으로 코드 샘플을 줄이거 나 시도하십시오. – marko

답변

3

는, 가장 중요한이 MP3 파일 형식의 오해입니다 ... 작동하지 않습니다.

MP3 파일에는 오디오 데이터가 들어 있지 않지만 실제로는 각자 고유 한 헤더 및 데이터 섹션이있는 여러 MP3 frames이 있습니다. 따라서 중간에있는 기존 MP3 파일을 잘라내어 두 개의 재생 가능한 MP3 파일로 만들 수는 없습니다. http://en.wikipedia.org/wiki/MP3

관련 문제