<div dir="ltr">Hey all,<div style>i have been working on an android application that records screen activity of the phone along with user's speech. The video is working fine while the audio is very noisy and giving a metallic sound. The code snippet for performing the writing of audio frame by frame :-</div>
<div style><br></div><div style><div>static void write_audio_frame(AVFormatContext *oc, AVStream *st) {</div><div><span class="" style="white-space:pre">   </span>AVCodecContext *c;</div><div><span class="" style="white-space:pre"> </span>AVFrame *frame = avcodec_alloc_frame();</div>
<div><span class="" style="white-space:pre">    </span>AVPacket pkt = { 0 }; // data and size must be 0;</div><div><br></div><div><span class="" style="white-space:pre"> </span>int got_packet, ret;</div><div><span class="" style="white-space:pre">       </span>av_init_packet(&pkt);</div>
<div><span class="" style="white-space:pre">    </span>c = st->codec;</div><div>//<span class="" style="white-space:pre">        </span>get_audio_frame(samples, audio_input_frame_size, c->channels);</div><div><span class="" style="white-space:pre">  </span>frame->nb_samples = audio_input_frame_size;</div>
<div><br></div><div><span class="" style="white-space:pre">   </span>avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,</div><div><span class="" style="white-space:pre">                  </span>(uint8_t *) samples,</div><div>
<span class="" style="white-space:pre">                       </span>audio_input_frame_size * av_get_bytes_per_sample(c->sample_fmt)</div><div><span class="" style="white-space:pre">                                 </span>* c->channels, 1);</div><div><br></div><div>
<span class="" style="white-space:pre">       </span>ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);</div><div><br></div><div><span class="" style="white-space:pre"> </span>if (ret < 0) {</div><div><span class="" style="white-space:pre">          </span>LOGE(3, "Error encoding audio frame: %s\n", av_err2str(ret));</div>
<div><span class="" style="white-space:pre">            </span>return;</div><div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>if (!got_packet)</div><div><span class="" style="white-space:pre">           </span>return;</div>
<div><span class="" style="white-space:pre">    </span>pkt.stream_index = st->index;</div><div><span class="" style="white-space:pre">   </span>/* Write the compressed frame to the media file. */</div><div><span class="" style="white-space:pre">        </span>ret = av_interleaved_write_frame(oc, &pkt);</div>
<div><span class="" style="white-space:pre">    </span>if (ret != 0) {</div><div><span class="" style="white-space:pre">            </span>LOGE(3, "Error while writing audio frame: %s\n", av_err2str(ret));</div><div><span class="" style="white-space:pre">               </span>return;</div>
<div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>av_free(frame);</div><div>}</div><div><br></div><div><br></div><div style>Any guidance will be appreciated.</div>
</div></div>