簡體   English   中英

如何在播放新歌之前停止播放媒體播放器?

[英]How to stop a media player before playing a new song?

我正在開發一個應用程序,它包含兩個活動,一個是媒體播放器,另一個是要播放的歌曲列表,mp3播放器工作正常,從列表中的活動我傳遞的名稱歌曲和播放器的工作正常。 我有兩個問題,如果用戶播放歌曲並離開應用程序(歌曲繼續在后台播放,這是假設工作的方式)然后用戶返回應用程序,搜索欄欄設置為0和計時器到0,有沒有辦法“保存”活動“...也就是如果一首歌正在播放而用戶試圖播放另一首歌曲,該歌曲播放在上一首歌曲的頂部,我嘗試通過添加來解決這個問題在我的意圖中一個“關鍵”來識別是否是一個新的音頻然后做這樣的事情:但是沒有用。

if (playerL != null) {

            if (mediaPlayer.isPlaying()) {
               mediaPlayer.stop;
            }
        }

public class AudioPlayer extends Activity {  

    /////////////////////////////////////////////////////////////
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio_player_activity);

        // Header
        Bundle extra = getIntent().getExtras();
        if (extra != null) {
            Intent intent = getIntent();
            id_d    = intent.getStringExtra("Id");
            heading = intent.getStringExtra("Heading"); 
            fileN   = intent.getStringExtra("fileName");
            audioN  = intent.getStringExtra("audioName");
            playerL = intent.getStringExtra("newAudio");

            meet_instructor_round_image = findViewById(R.id.audio_player_img);
            playerHeading = findViewById(R.id.audio_player_heading);
            playerHeading.setText(heading);
            Picasso.with(this).load(imgUrl).transform(new CropCircleTransformation()).into(round_image);
            createNotificationChannel();
            activateNotification(id_d, heading, imgUrl, bio);
        } else {
            Intent intent = new Intent(getApplicationContext(), com.starvizn.newstarvizn.COMMON.Activities.MainActivity.class);
            startActivity(intent);
        }

        pause = findViewById(R.id.btnAudioSubpause);
        play  = findViewById(R.id.btnAudioSubPlay);  

        songName = findViewById(R.id.workoutName);
        initialTime = findViewById(R.id.initialTime); 

        songName.setText(audioN);

        Uri uri = Uri.parse(getApplicationContext().getFilesDir()+"/Downloads/"+fileN+".mp3"); 

        mediaPlayer = MediaPlayer.create(this, uri);
        seekBar = findViewById(R.id.seekBar);
        seekBar.setClickable(false);
        pause.setVisibility(View.INVISIBLE);

        // Open lesson view
        lessons_layout = findViewById(R.id.player_lessons);
        lessons_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "Aqui");
                Intent intent = new Intent(getApplicationContext(), com.myApp.MainActivity.class); 
                intent.putExtra("Id", id_d); 
                startActivity(intent);
            }
        });

        if (playerL != null) {

            if (mediaPlayer.isPlaying()) {

            }
        }
    }

    public void player_play(View view) {
        play.setVisibility(View.INVISIBLE);
        pause.setVisibility(View.VISIBLE);
        mediaPlayer.start();
        finalTime = mediaPlayer.getDuration();
        startTime = mediaPlayer.getCurrentPosition();

        if (oneTimeOnly == 0) {
            seekBar.setMax((int) finalTime);
            oneTimeOnly = 1;
        } 

        initialTime.setText(String.format("%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime)))
        );

        seekBar.setProgress((int) startTime);
        myHandler.postDelayed(UpdateSongTime, 100);
        seekBar.setClickable(false);
        pause.setVisibility(View.VISIBLE);
    }

    public void player_pause(View view) {
        pause.setVisibility(View.INVISIBLE);
        play.setVisibility(View.VISIBLE);
        int temp = (int) startTime;

        mediaPlayer.pause();
    }

    public void player_fwd(View view) {
        int temp = (int) startTime;

        if ((temp + fwdTime) <= finalTime) {
            startTime = startTime + fwdTime;
            mediaPlayer.seekTo((int) startTime);
        } else {
            Toast.makeText(getApplicationContext(), "Cannot jump forward 5 seconds!", Toast.LENGTH_LONG).show();
        }
    }

    public void player_back(View view) {
        int temp = (int) startTime;

        if ((temp - backTime) > 0) {
            startTime = startTime - backTime;
            mediaPlayer.seekTo((int) startTime);
        } else {
            Toast.makeText(getApplicationContext(), "Cannot jump backward 5 seconds", Toast.LENGTH_LONG).show();
        }
    }

    private Runnable UpdateSongTime = new Runnable() {
        public void run() {
            startTime = mediaPlayer.getCurrentPosition();
            initialTime.setText(String.format("%02d:%02d",
                    TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                    TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                                    toMinutes((long) startTime)))
            );
            seekBar.setProgress((int) startTime);
            myHandler.postDelayed(this, 100);
        }
    };

    @Override
    public void onBackPressed() {

        builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("End Player").setMessage("Exit").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
                NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
                notificationManager.cancelAll();
                Intent intent = new Intent(getApplicationContext(), com.myapp.MainActivity.class); 
                intent.putExtra("Id", id_d); 
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.show();
    } 


}

所以我想你只需要添加假設處理該過程的邏輯。 正如文檔所說,你有兩個方法getCurrentPosition()seekTo(long, int)所以我認為你假設保存當前位置,例如在prefs中,並在seekTo方法的幫助下恢復你之前保存的數據。 因此,借助於此,您可以管理狀態

當我使用播放器應用程序時,我使用了ExoPlayer,它更靈活,更易於使用。 您可以嘗試以下鏈接https://codelabs.developers.google.com/codelabs/exoplayer-intro/#0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM