Android 从单击按钮后播放音频时停止的位置重新启动音频

Android 从单击按钮后播放音频时停止的位置重新启动音频,android,audio,Android,Audio,我正在播放一段音频(mySound)作为背景音乐。单击一个按钮,背景音频应该停止,另一个音频(mySound11)应该开始播放。完成此音频后,背景音乐应自动从原来的位置恢复。我已经尝试过这段代码,但是mySound11结束后背景音频不会恢复。请帮忙。提前谢谢 MainActivity.java 活动\u main.xml 我在线程的帮助下解决了你的问题。 我按你的意愿测试了它 int mySound11Duration; MediaPlayer mySound; MediaPlayer m

我正在播放一段音频(mySound)作为背景音乐。单击一个按钮,背景音频应该停止,另一个音频(mySound11)应该开始播放。完成此音频后,背景音乐应自动从原来的位置恢复。我已经尝试过这段代码,但是mySound11结束后背景音频不会恢复。请帮忙。提前谢谢

MainActivity.java
活动\u main.xml


我在线程的帮助下解决了你的问题。 我按你的意愿测试了它

int mySound11Duration;
MediaPlayer mySound;
MediaPlayer mySound11;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mySound11 = MediaPlayer.create(this, R.raw.tiktik);
    mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
    mySound.setLooping(true);
    mySound.start();

}
 public void playMusic(View view) {
        if (mySound.isPlaying()) { //check mysound is playing or not
            mySound11Duration = mySound11.getDuration();
            Thread thread=new Thread() {
                @Override
                public void run() {
                    try {
                        mySound.pause();
                        int mySoundPausePosition = mySound.getCurrentPosition();//get the time where the song is paused.
                        mySound11.start();
                        sleep(mySound11Duration);//sleep method causes the currently executing thread to sleep for specified number of time(miliseconds). 
                        mySound.seekTo(mySoundPausePosition);
                        mySound.start();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();

        }
      }
    }

嗨,你还没有告诉我你的问题是否解决了。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button Dabaa be"
    android:onClick="playMusic"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="72dp" />
int mySound11Duration;
MediaPlayer mySound;
MediaPlayer mySound11;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mySound11 = MediaPlayer.create(this, R.raw.tiktik);
    mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
    mySound.setLooping(true);
    mySound.start();

}
 public void playMusic(View view) {
        if (mySound.isPlaying()) { //check mysound is playing or not
            mySound11Duration = mySound11.getDuration();
            Thread thread=new Thread() {
                @Override
                public void run() {
                    try {
                        mySound.pause();
                        int mySoundPausePosition = mySound.getCurrentPosition();//get the time where the song is paused.
                        mySound11.start();
                        sleep(mySound11Duration);//sleep method causes the currently executing thread to sleep for specified number of time(miliseconds). 
                        mySound.seekTo(mySoundPausePosition);
                        mySound.start();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();

        }
      }
    }