我是android studio的新手,对我的任何误解都要承担责任。我想在一个名为 lyrics_view 的文本视图中显示一串音乐歌词,该视图位于我的 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>
答案 0 :(得分:0)
您需要将查询结果过滤为musixmatch。当我与艺术家The Chainsmokers一起执行curl命令并跟踪Paris时,您会收到musixmatch返回的20首曲目(主要是俱乐部混音)。您必须编写一些代码,才能使用用户文档中提到的过滤器参数(f_输入参数)来将结果查找/过滤到要显示的结果
卷曲: 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”:“ Let's Dance,Vol。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,“工具”:0,“ ex plicit“:0,” has_lyrics“:1,” has_subtitles“:0,” has_richs