-1

Как сделать на кнопке ImageButton остановку песни,при повторном её нажатии?

Мой Java код:

package com.example.killerlii.nedaibogzafeilit;

import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.FocusFinder;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class main extends AppCompatActivity {

    TextView t;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_logo);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        t = (TextView) findViewById(R.id.nazvanie);
        Typeface meCustomFont1 = Typeface.createFromAsset(getAssets(), "fonts/LegionaryBold.otf");
        t.setTypeface(meCustomFont1);

        t = (TextView) findViewById(R.id.predpisanie);
        Typeface meCustomFont2 = Typeface.createFromAsset(getAssets(), "fonts/LegionaryItalic.otf");
        t.setTypeface(meCustomFont2);

        t = (TextView) findViewById(R.id.opisan);
        Typeface meCustomFont3 = Typeface.createFromAsset(getAssets(), "fonts/Legionary.otf");
        t.setTypeface(meCustomFont3);


        RelativeLayout mainpage = (RelativeLayout) findViewById(R.id.mainpage);
        ScrollView Opisanie = (ScrollView) findViewById(R.id.Opisanie);
        final ImageView imageView = (ImageView) findViewById(R.id.ImageView);
        final TextView nazvanie = (TextView) findViewById(R.id.nazvanie);
        final TextView predpisanie = (TextView) findViewById(R.id.predpisanie);
        final TextView opisan = (TextView) findViewById(R.id.opisan);
        ScrollView Perecheslenie = (ScrollView) findViewById(R.id.Perecheslenie);
        final ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
        final ImageButton imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
        final ImageButton imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
        final ImageButton imageButton4 = (ImageButton) findViewById(R.id.imageButton4);
        final ImageButton imageButton5 = (ImageButton) findViewById(R.id.imageButton5);
        final ImageButton imageButton6 = (ImageButton) findViewById(R.id.imageButton6);
        final ImageButton imageButton7 = (ImageButton) findViewById(R.id.imageButton7);
        final ImageButton imageButton8 = (ImageButton) findViewById(R.id.imageButton8);
        final ImageButton imageButton9 = (ImageButton) findViewById(R.id.imageButton9);
        final ImageButton imageButton10 = (ImageButton) findViewById(R.id.imageButton10);




        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayer ring= MediaPlayer.create(main.this,R.raw.firsts);
                ring.start();

                nazvanie.setText(getResources().getString(R.string.imageButton1));
                predpisanie.setText(getResources().getString(R.string.imageButton1_1));
                opisan.setText(getResources().getText(R.string.imageButton1_2));

                image = (ImageView)findViewById(R.id.imageView);
                image.setImageResource(R.drawable.ic_launcher);
            }
        });
}
}

1 ответ 1

0
MediaPlayer ring;

В onCreate()

 ring = MediaPlayer.create(main.this, R.raw.firsts);

Слушатель

imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
if(ring.isPlaying()) ring.pause();
else ring.start();
            nazvanie.setText(getResources().getString(R.string.imageButton1));
            predpisanie.setText(getResources().getString(R.string.imageButton1_1));
            opisan.setText(getResources().getText(R.string.imageButton1_2));
//эту строку в onCreate
            //image = (ImageView)findViewById(R.id.imageView);
            image.setImageResource(R.drawable.ic_launcher);
        }
    });
7
  • Очень вам благодарен =)
    – Артём
    18 янв 2017 в 15:18
  • Не за что, думаю додумаетесь сами как картинку менять на паузу/плэй? :)
    – user194625
    18 янв 2017 в 15:21
  • Сергей,а у меня каплю не в этом задача ,но додумаюсь =) Всё что мне надо было я узнал,большое вам спасибо!
    – Артём
    18 янв 2017 в 15:24
  • Ой не заметил. Вынесите строку инициализации ImageView в onCreate. В слушателей это не пишут
    – user194625
    18 янв 2017 в 15:41
  • Спасибо,учёл =)
    – Артём
    18 янв 2017 в 15:46

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.