媒体记录器的声音质量较差

问题描述

我想做的是在将声音上传到Firebase之后录制声音 然后检索到reciclerview我可以控制 播放/停止和进度。像whatsApp语音消息一样

在Main活动中通过按下a来调用AudioPlayer类 按钮并返回值以将文件上传到firebase中。 但音质非常差,只能在浏览器或 直接在源文件夹中,否则我无法以其他格式播放它 当我直接在源文件夹中播放THREE_GPP时。

public class StartRecordMessage extends AppCompatActivity implements Runnable {

    private static String fileName = null;
    private MediaRecorder myAudioRecorder;
    private MediaPlayer player = null;
    private boolean permissionToRecordAccepted = false;
    private String[] permissions = {Manifest.permission.RECORD_AUdio};
    private final String TAG = "StartRecordMessageClass";
    private String audioName = null;
    private String externalStoragePath,externalOutputPath,storagePath;

    public StartRecordMessage(MediaRecorder myAudioRecorder) {

        this.myAudioRecorder = myAudioRecorder;
    }


    @Override
    public void run() {

        startRecording();
    }


    public String startRecording (){
        myAudioRecorder = new MediaRecorder();
        myAudioRecorder.reset();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
     myAudioRecorder.setoutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            externalOutputPath = externalStoragePath + File.separator + "/Android/data/test.mp3";
            myAudioRecorder.setoutputFile(externalOutputPath);
        }
        else
        {
            storagePath = Environment.getDataDirectory().getAbsolutePath();
            myAudioRecorder.setoutputFile(storagePath + "/Android/data/test.mp3");
        }


        try {
            Log.i(TAG,"--------------recording started confirmed" + externalOutputPath + storagePath );
            myAudioRecorder.prepare();
            myAudioRecorder.start();
        } catch (IllegalStateException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        } catch (IOException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }

        if (externalOutputPath.isEmpty()){
            return storagePath;
        }else {
            return externalOutputPath;
        }
    }

关注方法在主要活动中

@Override
public void onRecordingCompleted() {
    Log.i(TAG,"---------completed");
    startThread = new Thread(new StartRecordMessage(myAudioRecorder));
    startThread.start();

        startRecordMessage = new StartRecordMessage(myAudioRecorder);
        audioMessage = startRecordMessage.startRecording();

        Log.i(TAG,"recordingCompleted--------------" + audioMessage);

       UploadSound(audioMessage);

}
    messagelistadapter.notifyDataSetChanged();
}

AudioPlayerClass

public class AudioPlayer {

    public static void play(Context ctx,String path) {
        try {
            final AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUdio_SERVICE);
            final int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

            final MediaPlayer mediaPlayer = MediaPlayer.create(ctx,Uri.parse(path));

            mediaPlayer.setonCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if (!audioManager.isWiredHeadseton()) {
                        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,currentVolume,AudioManager.FLAG_VIBRATE);
                    }
                    mp.release();
                }
            });

            mediaPlayer.setonPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {

                    if (!audioManager.isWiredHeadseton()) {
                        int volume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / 2;
                        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,volume,AudioManager.FLAG_VIBRATE);
                    }
                    mp.start();

                }

            });

        }      catch(
                Throwable e)

        {
            Log.i("AudioUtil","Unable to play: " + path,e);
        }
    }
}

AdapterClass

            holder.playStop.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                clickCount++;

                if (clickCount==1) {
                    isPlayng=true;
                    holder.playStop.setBackgroundResource(R.drawable.ic_pause);

                    Log.i(TAG,"audio url---------------" + audio);

                    audioPlayer = new AudioPlayer();
                    audioPlayer.play(context,audio);

                    Log.i(TAG,"Media player ------------"+ clickCount + " is play:  " +isPlayng);
                }

                if (clickCount>1){
                    isPlayng=false;
                    clickCount=0;
                    holder.playStop.setBackgroundResource(R.drawable.ic_play);
                    Log.i(TAG,"Media player ------------" + clickCount + " is play:  " + isPlayng);


                }
            }

        });

当我想播放声音时,将触发跟随错误。班级 在Adapter类中调用了音频播放器类,但问题是 网址。

https://firebasestorage.googleapis.com/v0/b/chat-b18d3.appspot.com/o/Audio%....。 2020-11-12 22:59:44.739 2621-2621 I / AudioUtil:无法播放:https://firebasestorage.googleapis.com/v0/b/chat-b18d3.appspot.com/o/Audio%....java.lang.NullPointerException:尝试调用虚拟方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'上的null 对象参考 在com.example .... ChatRecord.AudioPlayer.play(AudioPlayer.java:13) 在com.example.chatMapOriginal.Profile.Chat.MyGroups.GroupChatAdapter $ 1.onClick(GroupChatAdapter.java:137) 在android.view.View.performClick(View.java:7192) 在android.view.View.performClickInternal(View.java:7166) 在android.view.View.access $ 3500(View.java:824) 在android.view.View $ PerformClick.run(View.java:27592) 在android.os.Handler.handleCallback(Handler.java:888) 在android.os.Handler.dispatchMessage(Handler.java:100) 在android.os.Looper.loop(Looper.java:213) 在android.app.ActivityThread.main(ActivityThread.java:8178) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:513) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

解决方法

尝试在媒体文件中添加值。

例如。

// Setup values for the media file
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE,"audio file");
values.put(MediaStore.Audio.Media.DATE_ADDED,(int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE,"audio/3gpp");
values.put(MediaStore.Audio.Media.DATA,audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
// Construct uris
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base,values);
// Trigger broadcast to add
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,newUri));
Toast.makeText(this,"Added File " + newUri,Toast.LENGTH_LONG).show();

此代码来自here