簡體   English   中英

媒體錄音機聲音質量差

[英]Media recorder sound have poor quality

我嘗試做的是在將聲音上傳到 firebase 后錄制聲音,然后檢索到 reciclerview,我可以控制播放/停止和進度。 喜歡 whatsApp 音頻消息

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

遵循方法在主要活動中

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


                }
            }

        });

當我想播放聲音時會觸發跟隨錯誤。 class 音頻播放器 class 在適配器 class 中被調用,但問題是 url。

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

嘗試在您的媒體文件中添加值。

例如。

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

這段代碼來自這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM