【问题标题】:#Reading audio formats and produce sound#读取音频格式并产生声音
【发布时间】:2018-05-08 06:17:56
【问题描述】:

我正在开发一个 android 媒体播放器,我需要使用 WAV 和 MP3 等音频格式。而且我需要在将文件读取为字节后产生声音。任何人都知道如何做到这一点或可以提供帮助!
注意:必须不使用 MediaPlayer 类或其他准备好的类,我需要将文件作为字节读取并产生声音。

【问题讨论】:

    标签: android


    【解决方案1】:

    为什么不能使用 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);
    }
    

    【讨论】:

    • 感谢您的回答,这个程序与我的学习有关,所以我需要构建自己的媒体播放器,支持 wav、mp3 和 avi 等格式。所以我不能使用媒体播放器类之类的东西准备好的课程。我必须将每种格式读取为字节并产生声音。
    猜你喜欢
    • 2021-05-13
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多