简体   繁体   English

媒体录音机声音质量差

[英]Media recorder sound have poor quality

What i try to do is to record a sound after to upload it into firebase and to retrieve then into reciclerview were i can control the play/stop and the progress.我尝试做的是在将声音上传到 firebase 后录制声音,然后检索到 reciclerview,我可以控制播放/停止和进度。 Like whatsApp audio messages喜欢 whatsApp 音频消息

The class AudioPlayer its called in the Main activity by pressing a button and return the value to upload the file in firebase.Its work but the sound quality its very poor and its work just in browser or directly in source folder or i can t play it in other formats like THREE_GPP just when i play it directly in the source folder. class 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;
        }
    }

Follow method is in the main activity遵循方法在主要活动中

@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);


                }
            }

        });

The follow error is triggered when i want to play the sound.当我想播放声音时会触发跟随错误。 The class Audio player class is called in Adapter class but the problem is the url. class 音频播放器 class 在适配器 class 中被调用,但问题是 url。

https://firebasestorage.googleapis.com/v0/b/chat-b18d3.appspot.com/o/Audio%.... . https://firebasestorage.googleapis.com/v0/b/chat-b18d3.appspot.com/o/Audio%..... 2020-11-12 22:59:44.739 2621-2621 I/AudioUtil: Unable to play: 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: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at com.example....ChatRecord.AudioPlayer.play(AudioPlayer.java:13) at com.example.chatMapOriginal.Profile.Chat.MyGroups.GroupChatAdapter$1.onClick(GroupChatAdapter.java:137) at android.view.View.performClick(View.java:7192) at android.view.View.performClickInternal(View.java:7166) at android.view.View.access$3500(View.java:824) at android.view.View$PerformClick.run(View.java:27592) at android.os.Handler.handleCallback(Handler.java:888) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread.java:8178) at 8821324 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at com.example....ChatRecord.AudioPlayer.play (AudioPlayer.java:13) at com.example.chatMapOriginal.Profile.Chat.MyGroups.GroupChatAdapter$1.onClick(GroupChatAdapter.java:137) at android.view.View.performClick(View.java:7192) at android.view .View.performClickInternal(View.java:7166) at android.view.View.access$3500(View.java:824) at android.view.View$PerformClick.run(View.java:27592) at android.os.Handler .handleCallback(Handler.java:888) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread. java:8178) 在 8821324 6945888.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 6945888.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

try add values in your media file.尝试在您的媒体文件中添加值。

exmaple.例如。

// 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();

this code from here这段代码来自这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM