I'm trying to develop an Android App using Java that enables me to start/stop an internet radio-stream using the MediaPlayer. My next goal is to implement a function to showcase the album, title and the artists name of the current song played inside the app. So far starting and stopping works fine, but when it comes to showcasing the metdata, my TextView shows "null". Inside Logcat it also shows "null"
App Screenshot
Logcat
Here is the code:
public class MainActivity extends AppCompatActivity {
Button button;
Button buttonstp;
TextView textView;
String url = "https://streams.deltaradio.de/EDM/mp3-128/";
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
);
button = findViewById(R.id.btn);
buttonstp = findViewById(R.id.btn_stop);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Radio started", Toast.LENGTH_SHORT).show();
try {
mediaPlayer.setDataSource(url);
} catch (IOException e) {
throw new RuntimeException(e);
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
// Jetzt, wo der Stream gestartet ist, kannst du die Metadaten abrufen
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(url);
String artist = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String title = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
String album = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
Log.d("Metadata", "Artist: " + artist);
Log.d("Metadata", "Title: " + title);
Log.d("Metadata", "Album: " + album);
// Set metadata to TextView
String metadataText = "Artist: " + artist + "\nTitle: " + title + "\nAlbum: " + album;
textView.setText(metadataText);
try {
retriever.release();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
mediaPlayer.prepareAsync(); // Asynchron vorbereiten, um das UI nicht zu blockieren
}
});
buttonstp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Radio stopped", Toast.LENGTH_SHORT).show();
mediaPlayer.stop();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
I tried looking up different options to stream the music. Went through different audio links, put them into VLC Mediaplayer to see if they showcase Metadata. The one that worked I put into my project. I'm fairly new to android development so I'm not quite sure where to go from here.
Here is what I thought could be the problem:
The way the metadata is shared isn't being supported by MediaPlayer
MediaPlayer isn't meant for these type of streams and metadata can only be portrayed when local files are being played
My code just sucks and I have no idea what I'm doing