ExoPlayer:无法播放以 .aac 结尾的流 url

ExoPlayer: Not able to play streaming url ending with .aac

这里是直播url:

"https://rfcmedia.streamguys1.com/70hits.aac"

这是我的代码:

 val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()

        val hlsMediaSource: HlsMediaSource = HlsMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri("https://rfcmedia.streamguys1.com/70hits.aac"))
        exoPlayer.setMediaSource(hlsMediaSource)
        exoPlayer.prepare()

错误:

原因:com.google.android。exoplayer2.ParserException:输入不以#EXTM3U header。

开头

我错过了什么

出于某种原因,ExoPlayer 似乎没有自动检测到这是一个音频 Uri - 不确定为什么,但下面的代码对我有用:

package com.example.hls_test;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.util.MimeTypes;

public class MainActivity extends AppCompatActivity {
    ExoPlayer player;
    private static final String hlsUri = "https://rfcmedia.streamguys1.com/70hits.aac";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        player = new ExoPlayer.Builder(this).build();
        Button b = findViewById(R.id.playButton);
        TextView t = findViewById(R.id.errormessage);
        b.setOnClickListener(view -> {
            try {
                t.setText("");
                MediaItem item = new MediaItem.Builder()
                        .setUri(hlsUri)
                        .setMimeType(MimeTypes.AUDIO_AAC)
                        .build();
                player.setMediaItem(item);
                player.prepare();
                player.play();
            } catch (Exception e) {
                t.setText("Error occurred playing media item: " + e.getMessage());
            }
        });
    }

    private void releaseResources() {
        if ( player != null ) {
            if ( player.isPlaying() ) {
                player.stop();
            }
            player.release();
            player = null;
        }
    }

    @Override
    public void onBackPressed() {
        releaseResources();
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        releaseResources();
        super.onDestroy();

    }
}