我有一个奇怪的问题,我的媒体播放器工作,并且当媒体播放器代码在我的片段的onCreateView方法中时播放正常。
class HomeFragment1 extends Fragment implements OnClickListener{
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState){
final View view= inflater.inflate(R.layout.homefragment1, container, false);
try {
System.out.println("Should Play");
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://10.0.2.2/users/" + user_id + "/tracks/track1.mp3");
player.prepare();
if (!player.isPlaying()) {
player.start();
}
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
此代码有效,玩家可以正常使用。
但是,我尝试根据片段的可见性启动/停止音乐。因此,当我使用相同的代码块并将其移动到setUserVisibleHint方法时,它会中断。活动需要很长时间才能加载,音乐永远不会播放,尽管它会打印出应该播放。当我滑动到另一个片段时,它打印应该停止。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser)
{
try {
System.out.println("Should Play");
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://10.0.2.2/users/" + user_id + "/tracks/track1.mp3");
player.prepare();
if (!player.isPlaying()) {
player.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
System.out.println("Should Stop");
//Enter stop logic here
}
}
Here is my error:
01-13 12:54:27.317 1458-1458/arufolo.trapster I/System.out﹕ Should Play
01-13 12:54:58.278 1458-1477/arufolo.trapster E/MediaPlayer﹕ error (1, -2147483648)
01-13 12:54:58.279 1458-1458/arufolo.trapster W/System.err﹕ java.io.IOException: Prepare failed.: status=0x1
01-13 12:54:58.283 1458-1458/arufolo.trapster W/System.err﹕ at android.media.MediaPlayer._prepare(Native Method)
01-13 12:54:58.284 1458-1458/arufolo.trapster W/System.err﹕ at android.media.MediaPlayer.prepare(MediaPlayer.java:1125)
01-13 12:54:58.284 1458-1458/arufolo.trapster W/System.err﹕ at arufolo.trapster.HomeFragment1.setUserVisibleHint(HomeActivity.java:221)
01-13 12:54:58.284 1458-1458/arufolo.trapster W/System.err﹕ at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
01-13 12:54:58.284 1458-1458/arufolo.trapster W/System.err﹕ at android.support.v4.view.ViewPager.populate(ViewPager.java:1071)
01-13 12:54:58.295 1458-1458/arufolo.trapster W/System.err﹕ at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
01-13 12:54:58.296 1458-1458/arufolo.trapster W/System.err﹕ at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
01-13 12:54:58.296 1458-1458/arufolo.trapster W/System.err﹕ at android.view.View.measure(View.java:17430)
01-13 12:54:58.296 1458-1458/arufolo.trapster W/System.err﹕ at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
01-13 12:54:58.296 1458-1458/arufolo.trapster W/System.err﹕ at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
01-13 12:54:58.296 1458-1458/arufolo.trapster W/System.err﹕ at android.view.View.measure(View.java:17430)
01-13 12:54:58.296 1458-1458/arufolo.trapster W/System.err﹕ at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
01-13 12:54:58.297 1458-1458/arufolo.trapster W/System.err﹕ at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
01-13 12:54:58.297 1458-1458/arufolo.trapster W/System.err﹕ at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
01-13 12:54:58.297 1458-1458/arufolo.trapster W/System.err﹕ at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
01-13 12:54:58.297 1458-1458/arufolo.trapster W/System.err﹕ at android.view.View.measure(View.java:17430)
01-13 12:54:58.297 1458-1458/arufolo.trapster W/System.err﹕ at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
关于这一点的最奇怪的事情......当您滑动到一个新片段时,然后向后滑动到音乐应该播放的片段,它可以正常播放而没有错误。
任何人都可以帮我吗?