Android开发中,要想使用MediaPlayer构建一个音频合并的工具,可以通过以下步骤进行实现:
实现的示例代码如下:
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {
private MediaPlayer mMediaPlayer1, mMediaPlayer2;
private boolean mIsMediaPlayer1Prepared = false, mIsMediaPlayer2Prepared = false;
private boolean mIsPlayingAudio = false;
private Visualizer mVisualizer;
private AudioTrack mTrack;
private int mSamplingRate;
private int mBufferSize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSamplingRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
mBufferSize = AudioTrack.getMinBufferSize(mSamplingRate, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);
mTrack = new AudioTrack(AudioManager.STREAM_MUSIC, mSamplingRate, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, mBufferSize, AudioTrack.MODE_STREAM);
mMediaPlayer1 = new MediaPlayer();
mMediaPlayer1.setDataSource("http://soundbible.com/grab.php?id=1066&type=wav");
mMediaPlayer1.setOnPreparedListener(this);
mMediaPlayer1.prepareAsync();
mMediaPlayer2 = new MediaPlayer();
mMediaPlayer2.setDataSource("http://soundbible.com/grab.php?id=29&type=wav");
mMediaPlayer2.setOnPre
上一篇:androidx.lifecycle.Transformations未被导入。
下一篇:androidx.mediarouter.app.MediaRouteButton由于半透明背景抛出android.view.InflateException异常。