我正尝试制作一个简单的应用程序,仅用于学习目的,即录制音频剪辑,然后将文件上传到Firebase,这是我的代码
public class MainActivity extends AppCompatActivity {
private StorageReference mStorage;
Button recordeButton ;
TextView recorderLable ;
private MediaRecorder recorder;
private String fileName = null ;
private static final String LOG_TAG = "AudioRecordTest";
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private MediaPlayer player = null;
ProgressDialog progressDialog ;
// Requesting permission to RECORD_AUDIO
private boolean permissionToRecordAccepted = false;
private String [] permissions = {Manifest.permission.RECORD_AUDIO};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_RECORD_AUDIO_PERMISSION:
permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
break;
}
if (!permissionToRecordAccepted ) finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStorage = FirebaseStorage.getInstance().getReference();
recordeButton = findViewById(R.id.recordeButton) ;
recorderLable = findViewById(R.id.recordeLable) ;
fileName = getExternalCacheDir().getAbsolutePath();
fileName += "/audiorecordtest.3gp";
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
progressDialog = new ProgressDialog(this) ;
recordeButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
startRecording();
recorderLable.setText("recording started");
} else if (event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
recorderLable.setText("recording stopped");
}
return false ;
}
});
}
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(fileName);
try {
recorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
recorder.start();
}
private void stopRecording() {
recorder.stop();
recorder.release();
recorder = null;
uploadAudio();
}
private void uploadAudio() {
progressDialog.setMessage("UPLOADING ...");
progressDialog.show();
String uniqueID = UUID.randomUUID().toString() ;
final StorageReference filePath = mStorage.child("Audio").child(uniqueID + ".mp3") ;
Uri uri = Uri.fromFile(new File(fileName)) ;
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
recorderLable.setText("AUDIO UPLOADED \uD83D\uDE0A");
filePath.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
final Uri path = task.getResult();
Log.i("AUDIO_URI", path.toString());
}
}
}) ;
}
}) ;
}
}
问题是我已经成功上传了文件,但是当我尝试在Firebase中播放文件时,没有任何反应,文件长度为0:00,这意味着录制过程中没有上传问题