activity_main.xml
<LinearLayout 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"
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=".MainActivity"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Low Pitch"
android:id = "@+id/low"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal"
android:id = "@+id/normal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High"
android:id = "@+id/high"
/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
SoundPool sp;
private Button low, normal, high;
float pitch = 0.5f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
low = (Button) findViewById(R.id.low);
normal = (Button) findViewById(R.id.normal);
high = (Button) findViewById(R.id.high);
low.setOnClickListener(this);
normal.setOnClickListener(this);
high.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.low : pitch = 0.5f;playit();break;
case R.id.normal: pitch = 1.0f;playit();break;
case R.id.high: pitch = 2.0f;playit();break;
}
}
private void playit() {
// TODO Auto-generated method stub
sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
final int i = sp.load(this, R.raw.music, 0);
sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
sp.play(i, 1,1,0,0,pitch);
}
});
}
}
on line 46, there is an error . can't resolve R.raw.music
ReplyDeleteDid you put an audio file with the name music on the raw folder?
Delete