布局中的声音

时间:2016-10-29 12:16:03

标签: android

我正在开发一个吉他应用程序,实际上当我从一个布局“滑动”到另一个布局时,我试图让两个布局播放不同的声音。 目前,此布局在ACTION_DOWN上播放声音,但在我松开手指时停止播放,而不是在我将手指移出此布局区域时停止播放。

我的java:

public class DisplayActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        LinearLayout layout = (LinearLayout) findViewById(R.id.rootView);
        LinearLayout firstlayout =     (LinearLayout)findViewById(R.id.firstlayout);
        LinearLayout secondlayout =     (LinearLayout)findViewById(R.id.secondlayout);
        final MediaPlayer aPlayer;
        final MediaPlayer bPlayer;
        aPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.a);
        bPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.b);
        firstlayout.setOnTouchListener(new View.OnTouchListener() {


            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // action when on touch action is down pressed
                        aPlayer.start();
                        Log.d(TAG, "ACTION_DOWN, location:x" + x + ",y:" +     y);
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        // action when on touch action is moving
                        Log.d(TAG, "ACTION_MOVE, location:x" + x + ",y:" +     y);
                        return true;

                    case MotionEvent.ACTION_UP:
                        // action when on touch action is up
                        aPlayer.release();
                        Log.d(TAG, "ACTION_UP, location:x" + x + ",y:" + y);
                        return true;
                }
                return false;
            }
        });
        secondlayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // action when on touch action is down pressed
                        bPlayer.start();
                        Log.d(TAG, "ACTION_DOWN, location:x" + x + ",y:" +     y);
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        // action when on touch action is moving
                        Log.d(TAG, "ACTION_MOVE, location:x" + x + ",y:" +     y);
                        return true;

                    case MotionEvent.ACTION_UP:
                        // action when on touch action is up
                        bPlayer.release();
                        Log.d(TAG, "ACTION_UP, location:x" + x + ",y:" + y);
                        return true;
                }
                return false;
            }
        });

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/filename"
android:orientation="horizontal"
tools:context="com.example.android.appname.DisplayActivity">

<LinearLayout
    android:id="@+id/firstlayout"
    android:layout_width="374dp"
    android:layout_height="match_parent"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/filename" />

</LinearLayout>

<LinearLayout
    android:id="@+id/secondlayout"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/filename" />

</LinearLayout>

我需要更改什么才能让我的布局在吉他应用程序中像吉他弦一样播放? (不释放手指播放不同的声音) 任何帮助将不胜感激。

0 个答案:

没有答案