#Reading音频格式并产生声音

时间:2018-05-07 19:24:48

标签: android

我正在开发一个Android媒体播放器,我需要使用WAV和MP3等音频格式。我需要在将文件读取为字节后生成声音。有人知道怎么做或者可以帮忙!
注意:不需要使用MediaPlayer类或其他现成的类,我需要将文件作为字节读取并产生声音。

1 个答案:

答案 0 :(得分:0)

为什么不能使用MediaPlayer?

也许尝试exoplayer https://developer.android.com/guide/topics/media/exoplayer
https://github.com/google/ExoPlayer

来自ExoPlayer - play local mp4 file in SD card

的示例代码
private void prepareExoPlayerFromFileUri(Uri uri){
    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(null), new DefaultLoadControl());
    exoPlayer.addListener(eventListener);

    DataSpec dataSpec = new DataSpec(uri);
    final FileDataSource fileDataSource = new FileDataSource();
    try {
        fileDataSource.open(dataSpec);
    } catch (FileDataSource.FileDataSourceException e) {
        e.printStackTrace();
    }

    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            return fileDataSource;
        }
    };
    MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
            factory, new DefaultExtractorsFactory(), null, null);

    exoPlayer.prepare(audioSource);
    exoPlayer.setPlayWhenReady(true);
}
相关问题