2017-12-18 27 views
0

Ich versuche, ein Resonanzboden zu schaffen und ich bin seit seiner ersten Arbeit auf Action-Leiste stecken.
Also ich habe das Menü erstellt und die Schaltfläche wird auf der Aktionsleiste angezeigt und es werden die Aktivitäten angezeigt, die ich möchte, aber ich weiß nicht, wie man es so macht, wenn man alle Sounds aus dem Sound-Pool drückt.
Wenn ich versuche, ein SoundPool.stop() zu erstellen, bekomme ich "(nicht statische Methode 'Stop (int)' kann nicht aus einem statischen Kontext verwiesen werden") (ich mache es wahrscheinlich falsch, aber ich weiß nicht, wie es geht). Ich würde es wirklich schätzen es, wenn jemand helfen könnte, da ich darüber zu erfahren habe versucht, und ich kann nicht scheinen, alles in der Nähe zu findenWie kann ich Sounds von Soundpool auf Action-Bar stoppen

Hier ist der Code:.

import android.media.AudioManager; 
    import android.media.SoundPool; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.Menu; 
    import android.view.View; 



    public class soundMainjava extends AppCompatActivity { 

    SoundPool mySound; 
    int sound1Id; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.soundMain); 
     setVolumeControlStream(AudioManager.STREAM_MUSIC); 
     mySound = new SoundPool(99, AudioManager.STREAM_MUSIC, 0); 
     sound1Id = mySound.load(this, R.raw.sound1, 1); 
    } 

    public void pb1(View view) { 
     mySound.play(sound1Id, 1, 1, 1, 0, 1); 
    } 

    //inflates the menu; 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu, menu); 
     return true; 
    } 
} 

Hier ist das Menü Code:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
tools:context="com.example.---.testsounds.soundMainjava"> 
<item 
    android:id="@+id/action_sound" 
    android:icon="@drawable/ic_volume_off_black_24dp" 
    android:title="" 
    app:showAsAction="always"/> 
</menu> 

Hier ist der Code th bei nicht funktioniert:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_sound: 
      SoundPool.stop(sound1Id); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
+1

Fügen Sie der Frage eine Fehlerbeschreibung hinzu. – svgrafov

+0

Okay, ich habe es hinzugefügt –

Antwort

0

Das Problem ist also:

switch (item.getItemId()) { 
     case R.id.action_sound: 
// You are calling the static method. 
      SoundPool.stop(sound1Id); 
// You have to call the object created from SoundPool Class 
      return true;` 

dies sollte also

sein
switch (item.getItemId()) { 
     case R.id.action_sound: 
      mySound.stop(sound1Id); 
      return true; 

Hier ist ein voll funktionstüchtiges Beispiel:

 public class SoundPlayer { 

    private static final float leftVol = 1.0f; 
    private static final float rightVol = 1.0f; 
    private static final float rate = 1.0f; 
    private static final int loop = 0; 
    private static final int priority = 1; 
    private static SoundPool soundPool; 

    private static int startListeningSound; 
    private static int stopListeningSound; 

     public SoundPlayer(Context context) { 
     soundPool = new SoundPool.Builder().setMaxStreams(2).build(); 

     startListeningSound = soundPool.load(context, R.raw.google_now_tone, priority); 
     stopListeningSound = soundPool.load(context, R.raw.google_glass_done, priority); 

} 
     public void playStartVoiceSound() { 
     soundPool.play(startListeningSound, leftVol, rightVol, priority, loop, rate); 
    } 

     public void playStopVoiceSound() { 
     soundPool.play(stopListeningSound, leftVol, rightVol, priority, loop, rate); 
    } 

Und wenn Sie Ihre Aktivität aufrufen:

private SoundPlayer soundPlayer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    ... 
     soundPlayer = new SoundPlayer(this); 
    ... 
    // calling soundPlayer to play the sound : 
    int streamId = soundPlayer.playStartVoiceSound(); 


    // calling the soundPlayer stop 

    soundPlayer.stop(streamId); 
    } 
Verwandte Themen