Wie Spiele ich eine audio-Datei in Android?

Ich habe code, um eine .ogg audio-Datei, die ich aus dem internet heruntergeladen. Ich habe keine Fehler, also ich kann es starten, aber dann stürzt die app:

package play.my.sound;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class PlaySound2Activity extends Activity {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View view = findViewById(R.id.textView1);
    view.setOnClickListener((OnClickListener) this);
    //Set the hardware buttons to control the music
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    //Load the sound
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                int status) {
            loaded = true;
        }
    });
    soundID = soundPool.load("sound1.ogg", 1);

}

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        //Getting the user sound settings
        AudioManager audioManager = (AudioManager) getSystemService    (AUDIO_SERVICE);
        float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;
        //Is the sound loaded already?
        if (loaded) {
            soundPool.play(soundID, volume, volume, 1, 0, 1f);
            Log.e("Test", "Played sound");
        }
    }
    return false;
}
}

Ich glaube, ich habe zwei Probleme:

  1. Ich habe dies im main.xml - Datei:

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:text="Klicken Sie auf den Bildschirm, um anfangen zu spielen" 
    android:id="@+id/textView1" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"></TextView> 
    </LinearLayout> 
    

    Und ich weiß nicht, ob es richtig ist.

  2. Ich die Datei sound1.ogg im workspace->SoundPlay2 Ordner, da in den res-Ordner hatte ich Probleme, und auch ich versuchte, es in den beiden res Ordner vorhanden sind.

Dies ist aus meiner Konsole:

[2012-01-04 19:38:16 - PlaySound2] Failed to install PlaySound2.apk on device 'emulator-5554': timeout
[2012-01-04 19:38:16 - PlaySound2] Launch canceled!
[2012-01-04 19:47:33 - PlaySound2] Error in an XML file: aborting build.
[2012-01-04 19:52:34 - PlaySound2] res\layout\main.xml:0: error: Resource entry main is already defined.
[2012-01-04 19:52:34 - PlaySound2] res\layout\main.out.xml:0: Originally defined here.
[2012-01-04 19:52:34 - PlaySound2] C:\Users\Natalia\workspace\PlaySound2\res\layout\main.out.xml:1: error: Error parsing XML: no element found

Ich habe dieses Beispiel aus "Android Klingt - Tutorial".
Ich möchte eine audio-Datei wiederzugeben, genauer gesagt, eine .wav Datei.

Ich nicht' wissen, wo finde ich einige Informationen über die Dateien, die berechtigt sind, in die MediaPlayer-Klasse und Ihrer charakteristischen (durantion, sample-rate...) Könnten Sie mir sagen, wo ich das finde??

  • Dies ist den ersten Fehler habe ich in meinem log cat: E/AndroidRuntime(739): java.lang.RuntimeException: Unable to start activity ComponentInfo{spielen.meine.sound/spielen.meine.sound.PlaySound2Activity}: java.lang.Classcastexception-Fehler: spielen.meine.sound.PlaySound2Activity kann nicht gewirkt werden, um android.Blick.View$OnClickListener
  • warum gehst du nicht einfach google-Suche finden Sie einfache Antworten. eine einfache Suche "Unterstützt, audio-Formate" in google ergab developer.android.com/guide/appendix/media-formats.html. developer.android.com/guide/topics/media/mediaplayer.html
InformationsquelleAutor Natiya | 2012-01-04
Schreibe einen Kommentar