两个片段,每个片段中都有一个按钮。如果单击第一个片段中提供的按钮,它将播放音乐并移至第二个片段;如果按第二个片段中所示的按钮,则会重新出现第一个片段,从而开始播放第二首音乐并停止播放第一首音乐。我在这里只提供了代码。该应用程序已成功在AVD和真实设备上安装,但未播放声音。请帮忙。
MainActivity:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer, new Fragment1());
fragmentTransaction.commit();
}
}
第一个片段:
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment1,container,false);
final Button buttonFragment1 = (Button)view.findViewById(R.id.buttonFragmentId1);
final MediaPlayer mp1 = MediaPlayer.create(getContext(), R.raw.a);
buttonFragment1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentContainer, new Fragment2());
ft.commit();
if(mp1.isPlaying()==true){
mp1.pause();
mp1.seekTo(0);
}
}
});
return view;
}
}
第二个片段:
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment2 extends Fragment {
MediaPlayer mp2 = new MediaPlayer();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment2,container,false);
final Button buttonFragment2 = (Button)view.findViewById(R.id.buttonFragmentId2);
final MediaPlayer mp2 = MediaPlayer.create(getContext(), R.raw.a);
buttonFragment2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentContainer, new Fragment1());
ft.commit();
if(mp2.isPlaying()==true){
mp2.pause();
mp2.seekTo(0);
}
}
});
return view;
}
}