0

3 つの異なるボタンで構成されるページがあります。一時停止 | 止まる。プレイ | STOP は正常に動作していますが、特定のインスタンスで一時停止できません。一時停止ボタンを押して、インスタンスで再生を一時停止したい。そして、もう一度 [再生] ボタンを押して、保存したインスタンスから再開します。

以下はSound.javaファイルのコードです

public class Sound extends Activity {

    MediaPlayer mp = null;
    String play = "Play!";      
    String stop = "Stop!";
    String pause = "Pause";     
    int length;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sound);

        final ImageButton play = (ImageButton) findViewById(R.id.idplay);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                managerOfSound("play");
                Toast toast_play = Toast.makeText(getBaseContext(),
                        "Playing First", Toast.LENGTH_SHORT);
                toast_play.show();

            } // END onClick()
        });

final ImageButton pause = (ImageButton) findViewById(R.id.idpause);
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                managerOfSound("pause");
                Toast toast_pause = Toast.makeText(getBaseContext(),
"Pausing Morning Bhajan-Aarati", Toast.LENGTH_SHORT);
                toast_pause.show();

            } // END onClick()
        });


        final ImageButton stop = (ImageButton) findViewById(R.id.idstop);
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.stop();
                    mp.reset();
                    Toast toast_stop = Toast.makeText(getBaseContext(),
                            "Stopping First",
                            Toast.LENGTH_SHORT);
                    toast_stop.show();
                }
            } // END onClick()
        });


    /** Manager of Sounds **/
    protected void managerOfSound(String theText) {
if (mp != null){
        mp.reset();
        mp.release();
        }
        if (theText == "play")
            mp = MediaPlayer.create(this, R.raw.goodbye);
        else if (theText == "pause" && mp.isPlaying())
mp.pause();
            length = mp.getCurrentPosition();
            mp.seekTo(length);
            mp.start();
    }

これが私のsound.xmlファイルです。

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/idplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="65dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/image_play" />
        <!-- android:text="@string/idplay" /> -->

        <ImageButton
            android:id="@+id/idpause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_toLeftOf="@+id/idstop"
            android:background="@drawable/image_pause" />

        <ImageButton
            android:id="@+id/idstop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:layout_toRightOf="@+id/idplay"
            android:background="@drawable/image_stop" />

    </RelativeLayout>

あなたの助けに感謝します..!!

4

1 に答える 1

0

あなたの問題はこの行だと思います:

if (theText == "play")
  mp = MediaPlayer.create(this, R.raw.goodbye);
else if (theText == "pause" && mp.isPlaying())
  mp.pause();
  length = mp.getCurrentPosition();
  mp.seekTo(length);
  mp.start();´

一時停止ボタンをクリックして長さを設定していますが、この if 句で直接再開します。

メディアが以前に再生されていた場合、再生ボタンがクリックされたかどうかを確認する必要があります。

if(length > 0)

指定された位置でプレーヤーを開始します。

編集:さらに、演算子を使用し==て文字列を比較しないでください。使ったほうがいいstring1.equals(string2)

それが役に立てば幸い

幸運をお祈りしています

于 2013-10-06T17:35:13.543 に答える