【问题标题】:Sound plays over and over when each time mobile orientation is changed每次更改移动方向时,声音都会反复播放
【发布时间】:2018-10-09 00:52:57
【问题描述】:

当媒体播放器播放声音并且手机的方向每次从纵向更改为横向或横向更改为纵向时,每次更改方向时都会再次播放声音。

如何在不播放新声音的情况下继续播放当前以其他方向播放的相同声音?

这是我的代码:

public void SoundStuff(final Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewpager_example);

    mP = MediaPlayer.create(this, R.raw.c101);

    //btn1 = (Button) findViewById(R.id.btnPlay);
    tgbtn1 = (ToggleButton) findViewById(R.id.imageButton2);
    btnstop1 = (Button) findViewById(R.id.button1);

    tgbtn1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                // The toggle is enabled
                 try {
                     mP.start();
                      //tgbtn1.setVisibility(View.GONE);

                 }
                 catch (Exception exception) {
                     Log.v("exception:play", exception.toString());
                 }
             }
             else {

                 // The toggle is disabled
                 try {
                     mP.pause();
                 }
                 catch (Exception exception) {
                     Log.v("exception:pause", exception.toString());
                 }
             }
         }
     });


     btnstop1.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
             if(mP.isPlaying()){
                 mP.stop();
             }

             SoundStuff(savedInstanceState);
             ZoomImageView mViewPager = (ZoomImageView) findViewById(R.id.view_pager);

             Btn1ImageAdapter adapter = new Btn1ImageAdapter();
             mViewPager.setAdapter(adapter);
         }
     });

【问题讨论】:

  • 在启动 MediaPlayer 之前检查 if(!mP.isPlaying())

标签: java android eclipse


【解决方案1】:

好吧,当方向改变时会重新创建活动,因此您可以通过在清单文件中的活动中添加以下行来手动处理方向变化,

android:configChanges="orientation|screenSize"

这将在重新创建时阻止您的活动,并在更改方向时简单地恢复。试试这个。

当您使用上述解决方案时,您无需专门为横向模式定义布局。

但是如果你需要定义它然后在横向和纵向模式下工作,你可以简单地在下面的方法中定义这些变化。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

【讨论】:

  • 很乐意提供帮助,但也将此行添加到所有活动中,如果方向改变,您的活动将使用任何服务或其他服务
  • 谢谢。这解决了声音问题,但不是为景观活动调用 res/layout-land 布局,而是显示来自 res/layout 的布局
  • 现在方向问题已解决,正在调用布局,但每次更改方向时声音都会反复播放。每次方向改变时都会播放旧声音,同时播放新声音
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多