我是android studio的新手,如有任何误解请多多包涵。我想在名为 lyrics_view 的 TextView 中显示一串音乐歌词,该 TextView 位于我的 fragment_lyrics.xml 文件中。我环顾四周并遵循了一些类似的例子,但没有运气解决我的问题。这是我的一些代码。当我点击播放按钮时,一首歌曲通过 Spotify api 播放,我还想使用 musixMatch api 将这些歌词显示到 lyrics_view 中。如果需要,我可以提供更多代码。我什至要正确显示这个字符串吗?或者我应该采取完全不同的方式?感谢您为这篇长帖子所做的努力。
playSongButton() 位于我的 MainActivity 类中。它负责播放歌曲并调用calculateLyrics()方法。
public void playSongButton(View view) {
try {
mPlayer.playUri(null, "spotify:track:15vzANxN8G9wWfwAJLLMCg", 0, 0);
//where i believe i am having issues
TextView textView = null;
TextView displayTextView = (TextView) findViewById(R.id.lyrics_view);
String lyricString = songLyrics.calculateLyrics();
displayTextView.setText(lyricString);
} catch (Exception e) {
e.printStackTrace();
}
}
CalculateLyrics() 属于 SongLyrics 类
public SongLyrics(String songName, String artistName) {
this.songName = "Paris";
this.artistName = "The Chainsmokers";
}
public String calculateLyrics() {
try {
musixMatch = new MusixMatch(key.getKey());
track = musixMatch.getMatchingTrack(songName, artistName);
trackData = track.getTrack();
trackId = trackData.getTrackId();
lyrics = musixMatch.getLyrics(trackId);
lyricString = lyrics.getLyricsBody();
} catch (Exception e) {
e.printStackTrace();
}
return lyricString;
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0">
<EditText
android:id="@+id/songName"
android:layout_width="0dp"
android:layout_height="56dp"
android:ems="10"
android:hint="@string/enter_song"
android:inputType="text"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="6dp" />
<EditText
android:id="@+id/artistName"
android:layout_width="0dp"
android:layout_height="55dp"
android:ems="10"
android:hint="@string/enter_artist"
android:inputType="text"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintTop_toBottomOf="@+id/songName" />
<TextView
android:text="Enter Song Info"
android:layout_width="wrap_content"
android:layout_height="69dp"
android:id="@+id/enterSongInfo"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="80dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="80dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="1.0"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playButton"
android:onClick="playSongButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pauseButton"
android:onClick="pauseSongButton"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/playButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp" />
<fragment
android:id="@+id/lyrics"
android:layout_width="353dp"
android:layout_height="329dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginTop="208dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.71"
class="com.example.joeberg.jams.LyricsFragment"/>
/>
</android.support.constraint.ConstraintLayout>
fragment_lyrics.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:text="Lyrics"
android:layout_width="93dp"
android:layout_height="59dp"
android:id="@+id/lyrics_heading"
class="com.example.joeberg.jams.LyricsFragment"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="280dp"
android:textColor="@color/common_google_signin_btn_text_dark_focused" />
<TextView
class="com.example.joeberg.jams.LyricsFragment"
android:layout_width="335dp"
android:layout_height="271dp"
android:id="@+id/lyrics_view"
app:layout_constraintTop_toBottomOf="@+id/lyrics_heading"
android:textAppearance="@style/TextAppearance.AppCompat"
android:layout_marginStart="32dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="32dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintHorizontal_bias="0.52" />
</android.support.constraint.ConstraintLayout>
最佳答案
您需要将查询结果过滤到 musixmatch。当我与艺术家 The Chainsmokers 一起运行 curl 命令并跟踪 Paris 时,您会收到 musixmatch 返回的 20 首轨道(大部分是俱乐部混音)。您必须编写一些代码来使用用户文档中提到的过滤器参数(f_输入参数)查找/过滤结果到您想要显示的结果
<小时/>curl : curl --url“http://api.musixmatch.com/ws/1.1/track.search?q_artist=The%20Chainsmokers&q_track=Paris&apikey
<小时/>部分输出: {"message":{"header":{"status_code":200,"execute_time":0.019639015197754,"available":20},"body":{"track_list":[{"track":{"track_id":173322884,"track_name":"巴黎(混合)","track_name_translation_list":[ ],"track_ rating":1,"commontrack_id":96669304,"instrumental":0,"explicit":0,"has_lyrics":0,"has_subtitles":0,"has_richsync":0,"num_favourite":0,"album_id":32901374,"album_name":"让我们跳舞吧,第 2 卷(DJ Mix)","artist_id":26490468,"artist_name":"The Chainsmokers","track_share_url":"https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed?utm_source=application&utm_campaign=api&utm_medium=","track_edit_url":"https://www.musixmatch.com/lyrics/The -Chainsmokers/Paris-Mixed/edit?utm_source=application&utm_campaign=api&utm_medium=","restricted":0,"updated_time":"2019-06-12T15:47:28Z","primary_genres":{"music_genre_list":[]}}},{"track":{"track_id":126251071 ,"track_name":"巴黎 (Sbm X Geru Remix)","track_name_translation_list":[],"track_ rating":5,"commontrack_id":69979021,"instrumental":0,"explicit":0,"has_lyrics":1,"has_subtitles":0,"has_richs
关于java - 在 TextView 上显示字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42183149/