媒体录音机声音质量差

Posted

技术标签:

【中文标题】媒体录音机声音质量差【英文标题】:Media recorder sound have poor quality 【发布时间】:2020-11-12 22:15:00 【问题描述】:

我尝试做的是在将声音上传到 Firebase 后录制声音 然后检索到recclerview,我可以控制 播放/停止和进度。喜欢WhatsApp的音频消息

AudioPlayer 类在 Main 活动中通过按 按钮并返回值以在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 方法在主要活动中

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

音频播放器类

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

适配器类

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


                
            

        );

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

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)' 为空 对象引用 在 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)

【问题讨论】:

请不要对您的文本使用强调/引用模式 【参考方案1】:

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

例子。

// 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

【讨论】:

以上是关于媒体录音机声音质量差的主要内容,如果未能解决你的问题,请参考以下文章

Android 开发 如何实现高质量的录音

比较Java中的两个声音(有容差)[关闭]

Swift - 制作一个录音机(声音的录制与播放)

win10语音录音机录不了声音,讯飞输入法语音输入用不了,声音疑难解答出现意外错误?

什么录音软件可以录制电脑内部播放的声音

录音时候音频的外放