问题描述
我正在尝试创建流式应用程序。我已经能够使用PLAY / PAUSE按钮播放歌曲。但是我无法从歌曲列表中播放歌曲,也无法更改DISPLAYING ACTIVITY以显示带有play-pause-stop-etc的歌曲。
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class playerInterface extends AppCompatActivity {
private ImageView imagePlayPause;
private TextView textCurrentTime,textTotalDuration;
private SeekBar playerSeekBar;
private MediaPlayer mediaPlayer;
private Handler handler = new Handler();
private static String toPlay;
@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_interface);
imagePlayPause = findViewById(R.id.imagePlayPause);
textCurrentTime = findViewById(R.id.textCurrentTime);
textTotalDuration = findViewById(R.id.textTotalTimeDuration);
playerSeekBar = findViewById(R.id.playerSeekBar);
mediaPlayer = new MediaPlayer();
playerSeekBar.setMax(100);
imagePlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mediaPlayer.isPlaying()){
handler.removeCallbacks(updater);
mediaPlayer.pause();
imagePlayPause.setImageResource(R.drawable.ic_play);
}else{
mediaPlayer.start();
imagePlayPause.setImageResource(R.drawable.ic_pause);
updateSeekBar();
}
}
});
prepareMediaPlayer();
}
// this method aims at selecting a song when called from another java class
public static void prepareMediaPlayer(int number){
switch (number){
case 1: toPlay = "https://www.blablabla1.mp3";
break;
case 2: toPlay = "https://www.blablabla2.mp3";
break;
case 3: toPlay = "https://www.blablabla3.mp3";
break;
default: toPlay = "https://www.blablabla4.mp3";
break;
}
}
// this method plays the song from the internet
public void prepareMediaPlayer(){
try {
mediaPlayer.setDataSource(toPlay); // url of music file
mediaPlayer.prepare();
textTotalDuration.setText(milliSecondsToTimer(mediaPlayer.getDuration()));
}catch (Exception exception){
Toast.makeText(this,exception.getMessage(),Toast.LENGTH_SHORT).show();
}
}
我尝试了下面的代码(来自另一项活动),但是它不起作用。
public void egwuOsadabe(View view){
playerInterface.prepareMediaPlayer(3); // this aims at selecting a particular song upon click egwuOsadebe
redirectActivity(this,playerInterface.class); // this aims at changing the activity to playerInterface.class (the code above).
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)