1
1zu0
Neues Mitglied
- 2
Moinmoin, ich hätt da gen mal ein Problem
Hab mir das hier abgeschaut:
Handling Media with Android - Tutorial
--> 3. Tutorials: Play sounds via SoundPool
Das setOnLoadComplete wird angemeckert ;(
Mein Code der MainActivity.java:
Code der activity_main.xml:
Vielen Dank im voraus
Hab mir das hier abgeschaut:
Handling Media with Android - Tutorial
--> 3. Tutorials: Play sounds via SoundPool
Das setOnLoadComplete wird angemeckert ;(
Mein Code der MainActivity.java:
Code:
package com.example.soundpooltest_1;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity implements OnTouchListener {
private ImageButton ibPlayMusic;
private SoundPool soundpool;
private int soundID;
boolean loaded = false;
private MenuItem menExit, menHelp;
//=======================================================
private void init()
{
ibPlayMusic = (ImageButton)findViewById(R.id.ibPlayMusic);
ibPlayMusic.setOnTouchListener(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)
{
// TODO Auto-generated method stub
}
});
// setOnLoadCompleteListener(new OnLoadCompleteListener()
// {
//
// public void onLoadComplete(SoundPool soundpool, int sampleId, int status)
// {
// loaded = true;
// }
// });
soundID = soundpool.load(this, R.raw.sound2, 1);
}
//=======================================================
// public boolean onTouch(View v, MotionEvent event) {
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;
}
//=======================================================
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public boolean onOptionsItemSelected(MenuItem item)
{
if(item == menExit)
{
Toast.makeText(this,"In Hamburg sagt man Tschühüüs ;)", Toast.LENGTH_SHORT).show();
finish();
}
if(item == menHelp)
{
Toast.makeText(this, "zu Hüüüülföö", Toast.LENGTH_SHORT).show();
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menExit = menu.add("Beenden");
menHelp = menu.add("Info");
return true;
}
//=======================================================
}
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/ibPlayMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@null"
android:contentDescription="MukkeIcon"
android:scaleType="fitCenter"
android:src="@drawable/note2" />
<ImageView
android:id="@+id/ivLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:src="@drawable/soundmachine" />
</RelativeLayout>
Vielen Dank im voraus