2017-01-13 4 views
-1

Ich werde beginnen, indem ich den Code, den ich habe.MediaPlayer - Spielen und stoppen, wenn loslassen und auf Knopf drücken

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; 
      } 
     }); 



}} 

Hier ist was ich versuche zu tun. Ich versuche es so klingen zu lassen, bis ich den Knopf loslasse. Ich habe versucht, sound.stop() zu verwenden, aber dann wird es nur einmal funktionieren und dann nicht mehr funktionieren, bis ich die App neu starte. Hat jemand irgendwelche Ideen? Ich möchte versuchen, es so zu verhalten, wie ich es brauche, ohne zu viel hinzuzufügen. Wenn jemand mehr Klarheit darüber benötigt, was ich brauche, lass es mich wissen. Das Folgende ist meine XML-Layout-Datei:

<?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> 
+0

Können Sie auch einfügen, wie Ihre XML aussieht? – Manisha

+0

Ich füge es jetzt hinzu – jweaver

+0

Es ist jetzt hinzugefügt – jweaver

Antwort

-1

Ich konnte es selbst nach einigen Änderungen an den buttonJava Java-Code mit einigen der seekTo tun, Looping, und stoppt, wenn gehen lassen zu Lass es besser funktionieren.

0

Dies sollte den Trick -

MainActivity.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; 
      } 
     }); 
    } 
} 

Auch Taste Wähler Verwenden Sie statt Einstellknopf Hintergrund in onTouchListener wie folgt -

activity_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> 

ziehbar/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> 
Verwandte Themen