单击列表中的选定歌曲然后播放歌曲时,如何将活动更改为“现在播放”类型

问题描述

我正在尝试创建流式应用程序。我已经能够使用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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...