簡體   English   中英

MediaPlayer - 松開並按下按鈕時播放和停止

[英]MediaPlayer - Playing and stopping when let go and press on button

我將首先分享我擁有的代碼。

package com.example.onebuttonpressimage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.media.MediaPlayer;
import android.view.MotionEvent;    // new import library for onclick listener


public class MainActivity extends AppCompatActivity {
    // declare the objects and variables
    private ImageButton buttonJava;  // creates the JAVA object that holds button attributes and methods
    private MediaPlayer sound = null; // creates the JAVA object that holds the mediaplayer attributes and methods
    //private boolean pressed=false;

    // Creates the app window ===================================================================================
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sound = MediaPlayer.create(this, R.raw.beep);             // ties the JAVA mediaplayer object to the sound file
        buttonJava = (ImageButton) findViewById(R.id.buttonXML);  // ties the JAVA button object to the XML object


        buttonJava.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    buttonJava.setBackgroundResource(R.drawable.button_pressed);
                    sound.start();
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    buttonJava.setBackgroundResource(R.drawable.button_not_pressed);
                }
             return false;
            }
        });



}}

這就是我想要做的。 我試圖讓它播放聲音,直到我松開按鈕。 我嘗試使用sound.stop()但它只能工作一次,然后在我重新啟動應用程序之前不再工作。 有沒有人有任何想法? 我想嘗試讓它按照我需要的方式運行,而不會添加太多。 如果有人需要更多說明我需要什么,請告訴我。 以下是我的 XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.onebuttonpressedimage.MainActivity">

    <ImageButton
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:id="@+id/buttonXML"
        android:background="@drawable/blue"
        android:layout_centerHorizontal="true"
        android:onClick="onButtonClick" />

</RelativeLayout>

這應該可以解決問題-

主活動.java

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;


public class MainActivity extends AppCompatActivity {
    // declare the objects and variables
    private ImageButton buttonJava;  // creates the JAVA object that holds button attributes and methods
    private MediaPlayer sound = null; // creates the JAVA object that holds the mediaplayer attributes and methods
    //private boolean pressed=false;

    // Creates the app window ===================================================================================
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sound = MediaPlayer.create(this, R.raw.beep);             // ties the JAVA mediaplayer object to the sound file
        sound.setLooping(true);
        buttonJava = (ImageButton) findViewById(R.id.buttonXML);  // ties the JAVA button object to the XML object


        buttonJava.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    buttonJava.setPressed(true);
                    sound.start();

                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    buttonJava.setPressed(false);
                    sound.pause();
                }
                return true;
            }
        });
    }
}

也使用按鈕選擇器而不是在 onTouchListener 中設置按鈕背景,如下所示 -

活動_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mid.test.helpttest.MainActivity">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/button_selector"
        android:id="@+id/buttonXML"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

drawable/button_selector.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_not_pressed" android:state_enabled="false" />
    <item android:drawable="@drawable/button_pressed" android:state_enabled="true" android:state_pressed="true" />
    <item android:drawable="@drawable/button_not_pressed" android:state_enabled="true" android:state_focused="true" />
    <item android:drawable="@drawable/button_not_pressed" android:state_enabled="true" />
</selector>

在對 buttonJava Java 代碼進行了一些修改后,我能夠自己完成一些seekTo、Looping 和在放手時停止以使其更好地工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM