I'm trying to create a very simple app - a online radio player, and I want to get the name of the song currently playing. Here is the code:
我正在尝试创建一个非常简单的应用程序-一个在线广播播放器,我想获得当前正在播放的歌曲的名称。以下是代码:
player = new ExoPlayer.Builder(this).build();
viewPlayer.setPlayer(player);
MediaItem mediaItem = MediaItem.fromUri(link);
player.setMediaItem(mediaItem);
player.prepare();
player.play();
player.addListener(
new Player.Listener() {
@Override
public void onMediaMetadataChanged(MediaMetadata mediaMetadata) {
metadata = player.getMediaMetadata();
if (metadata.title!=null) {
MainActivity.this.title = metadata.albumTitle.toString();
viewSong.setText(MainActivity.this.title);
} else viewSong.setText("NULL");
}
}
);
I think, I have all dependencies needed in build.gradle.
我想,我有build.gradle中需要的所有依赖项。
Player works fine but getMediaMetadata()
gives me this (in debug mode):
播放器工作正常,但getMediaMetadata()提供了以下内容(在调试模式下):
metadata = {MediaMetadata@11693}
albumArtist = null
albumTitle = null
artist = null
..............
..............
genre = Rock Music
..............
..............
station = RADIO 1 ROCK
There is some data, but what I need is 'null'.
有一些数据,但我需要的是‘空’。
I know that metadata contains the title and artist info.
VLC player gives me this:
我知道元数据包含标题和艺术家信息。VLC播放器给了我这个:
I did not find any relevant answer of this problem yet.
我还没有找到这个问题的任何相关答案。
更多回答
优秀答案推荐
It looks like you did not add 'title' anywhere which is why you are getting a 'NULL' value as an output. You can use the code below to add and get the title or any other information from MediaMetadata.
看起来您没有在任何地方添加‘TITLE’,这就是为什么您会得到一个‘NULL’值作为输出。您可以使用下面的代码从MediaMetadata添加和获取标题或任何其他信息。
//Add required info into mediaMetadata
val mediaMetadata = MediaMetadata.Builder()
.setDisplayTitle("Your title here")
.setArtist("Your artist name")
.build()
//Build mediaItem with data added and url of media
val mediaItem = MediaItem.Builder()
.setMediaMetadata(mediaMetadata)
.setUri("Your track URL here")
mediaItem.build()
//Now add this mediaItem into your code like this
player.setMediaItem(mediaItem);
player.prepare();
player.play();
//Now in listener
player.addListener(new Player.Listener() {
@Override
public void onMediaMetadataChanged(MediaMetadata mediaMetadata) {
//Now get posted values like this
println("Title : "+mediaMetadata.displayTitle)
}
}
);
You can find more useful methods in MediaMetadata Builder like setAlbumTitle, setAlbumArtist, setSubtitle, setDescription, setUserRating, etc.
您可以在MediaMetadata Builder中找到更多有用的方法,如setAlumTitle、setAlumArtist、setSubtitle、setDescription、setUserRating等。
One more thing I think your code is in Java but my code at the beginning is in Kotlin so convert it into Java before use.
还有一件事,我认为你的代码是用Java写的,但我开头的代码是用kotlin写的,所以在使用之前把它转换成Java。
更多回答
我是一名优秀的程序员,十分优秀!