11package tech .oom .androidpcmresample ;
22
3+ import android .app .ProgressDialog ;
34import android .media .AudioFormat ;
45import android .os .Bundle ;
56import android .os .Handler ;
1819import tech .oom .resample .ReSampleListener ;
1920
2021public class MainActivity extends AppCompatActivity implements View .OnClickListener {
22+ private ProgressDialog dialog ;
2123 private Handler mHandler = new Handler () {
22- @ Override
23- public void handleMessage (Message msg ) {
24+ @ Override
25+ public void handleMessage (Message msg ) {
2426
25- }
26- };
27+ }
28+ };
2729
2830 @ Override
2931 protected void onCreate (Bundle savedInstanceState ) {
3032 super .onCreate (savedInstanceState );
3133 setContentView (R .layout .activity_main );
3234 copyAssetsToFile ();
3335 findViewById (R .id .resample_btn ).setOnClickListener (this );
36+ dialog = new ProgressDialog (this );
37+ dialog .setMessage ("resample in progress,please waite for a while" );
3438 }
3539
3640 private void copyAssetsToFile () {
@@ -51,10 +55,15 @@ private void copyAssetsToFile() {
5155
5256
5357 private void resample () {
58+ //需要重新采样的源文件路径
5459 File file = new File (getExternalFilesDir (null ), "50waystosaygoodbye.pcm" );
60+ //重新采样后生成的文件路径
5561 File targetWaveFile = new File (getExternalFilesDir (null ), "50waystosaygoodbye_8.pcm" );
62+ //源文件的采样率、通道数、采样位数
5663 PcmResample .AudioFileConfig srcFileConfig = new PcmResample .AudioFileConfig (file .getAbsolutePath (), 44100 , AudioFormat .CHANNEL_IN_STEREO , AudioFormat .ENCODING_PCM_16BIT );
64+ //生成文件的采样率、通道数(必须和源文件一样)、采样位数(必须和源文件一样)
5765 PcmResample .AudioFileConfig targetFileConfig = new PcmResample .AudioFileConfig (targetWaveFile .getAbsolutePath (), 8000 , AudioFormat .CHANNEL_IN_STEREO , AudioFormat .ENCODING_PCM_16BIT );
66+ //开始重新采样 采样回调在工作线程 非UI线程
5867 PcmResample .resample (srcFileConfig , targetFileConfig , new ReSampleListener () {
5968 @ Override
6069 public void onResampleError (String error ) {
@@ -64,11 +73,12 @@ public void onResampleError(String error) {
6473 @ Override
6574 public void onResampleStart () {
6675 System .out .println ("start" );
76+
6777 mHandler .post (new Runnable () {
6878 @ Override
6979 public void run () {
7080 Toast .makeText (MainActivity .this , "Resample start" , Toast .LENGTH_SHORT ).show ();
71-
81+ dialog . show ();
7282 }
7383 });
7484 }
@@ -80,7 +90,7 @@ public void onResampleFinish(final PcmResample.AudioFileConfig targetConfig) {
8090 @ Override
8191 public void run () {
8292 Toast .makeText (MainActivity .this , "Finish !The saved file path " + targetConfig .getFilePath (), Toast .LENGTH_SHORT ).show ();
83-
93+ dialog . dismiss ();
8494 }
8595 });
8696 }
0 commit comments