How to add audio to Recycler View items - android-studio

I'm making an app which will show different types of guitar and when the user will tap on the image(or text) of a particular type of guitar, it will play a riff of that type(eg. tapping on electric guitar will play an electric guitar riff). Here's my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guitar_basics);
recGuitarTypes();
}
private void recGuitarTypes(){
recGtypes = (RecyclerView) findViewById(R.id.viewGuitarTypes);
GuitarTypesRecyclerViewAdapter adapter = new GuitarTypesRecyclerViewAdapter(this);
recGtypes.setAdapter(adapter);
recGtypes.setLayoutManager(new GridLayoutManager(this, 2));
ArrayList<Guitar> arrGuitar = new ArrayList<>();
arrGuitar.add(new Guitar("Nylon String Spanish", "https://inside-guitar.com/wp-content/uploads/2019/05/Savarez-Flamenco-Guitar-Strings.jpg"));
arrGuitar.add(new Guitar("Steel String Acoustic","https://hearthemusicplay.com/wp-content/uploads/2017/05/steelstringguitar-e1494992902529-1140x638.jpg"));
arrGuitar.add(new Guitar("Electric Guitar","https://cdn.mos.cms.futurecdn.net/papiVxAvWm3QaDAD8fSdqS-970-80.jpg.webp"));
adapter.setGuitars(arrGuitar);
}
I wanted to check if the code worked which is why I tried to test it using a single audio file in my "raw" folder applied to all the items in the RecyclerView. It didn't. Here's the code if you wanna check that out:
recGtypes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mediaPlayer = MediaPlayer.create(view.getContext(), R.raw.steel_string);
mediaPlayer.start();
}
});
Thanks for your help in advance.

can you try below code ?
recGtypes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
private MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.steel_string);
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.prepare();
mediaPlayer.start();
}
});

Related

How to add onClickListener to open barcode scanner?

I had write a code on trigger Barcode Scanner. I would like to trigger it after i clink on a button, but it doesn't work when i drop the code inside the onclicklistener. Need help on how to resolve this.
I had also include the button inside.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_isbn = (TextView) findViewById(R.id.tv_display);
btn_camera = (Button) findViewById(R.id.btn_camera);
btn_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
camera();
}
});
}
```

Two differents button for one Animation Drawable

I have a problem with my code I develop a game on android studio and I meet the following concern.
There are two buttons on my app, a Punch button and a Kick Button and an Imageview in which I animate my images.
When I click on the punch button, I create a drawable animation of two images
when I click on the kick button, I create a drawable animation of two images as well.
The problem is that when I execute this code with two drawable animations, the application crashes !!
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private Button btnKick;
private ImageView imageView1 ;
private ImageView imageView2 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackgroundResource(
R.drawable.animationpunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
imageView2 = (ImageView)
findViewById(R.id.imageView2);
imageView2.setBackground
Resource(R.drawable.animatio
nkick);
animationDrawable[1] = (AnimationDrawable)
imageView2.getBackground();
btnPunch.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
});
btnKick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[0].stop()
animationDrawable[1].setOneShot(true);
animationDrawable[1].start();
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
</Code>
I yet use two different imageview for both animations
on the other hand when I remove an animation like the following code, it works well but suddenly I am limited to use the punch button, I would like it to work with both buttons
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private ImageView imageView1 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackground
Resource(R.drawable.animatio
npunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
btnPunch.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
}}
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
click on punch button, it animates my animation unch
I click on kick button, it animates my animation kick
a help?

If Statement Not Executed in AutocompleteTextView

I'm new to Android Studio and currently working on my first app. Thanks to numerous examples on here i have managed to catch up on the basics very quickly. I now have a problem with my autocompletetextview, i want to get the text selected then use it in a if statement to determine what is shown on the "results" textview. The if statement works fine but only for the "else" part and i can't figure out where i went wrong.
I have commented out the onItemClickListener because the "testfoodph" method does the same.
Here is the activity code:
public class phTester extends AppCompatActivity {
Intent intent = getIntent();
AutoCompleteTextView text;
MultiAutoCompleteTextView text1;
TextView results;
String foodies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ph_tester);
String[] foods = getResources().getStringArray(R.array.foodlist);
text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,foods);
text.setAdapter(adapter);
text.setThreshold(1);
results=(TextView)findViewById(R.id.phResults);
foodies = text.getText().toString();
text.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//if (foodies.equalsIgnoreCase("Mango")) {
//results.setText(getString(R.string.phAlk));
}
//else {
//results.setText(getString(R.string.Error));
//}
//}
});
}
public void TestFoodPH(View view) {
if (foodies.equalsIgnoreCase("Mango")) {
results.setText(getString(R.string.phAlk));
}
else {
results.setText(getString(R.string.Error));
}
}

Android Studio and mediaPlayer

I am developing a simple app in that my video is playing proper. but now I want to give "thank" message after finishing my video
How can I do this?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
final MediaPlayer player = MediaPlayer.create(MainActivity.this,R.raw.video);
Button button = (Button)findViewById(R.id.video_Play);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
player.start();
startActivity(new Intent("com.example.lenovo.play.app.VideoPlay"));
}
});
}
and Second VideoPlay.java
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
view = (VideoView)findViewById(R.id.videoView);
String url = "android.resource://"+getPackageName()+"/"+R.raw.video;
view.setVideoURI(Uri.parse(url));
view.start();
int duration = view.getDuration();
}
Implement MediaPlayer.OnCompletionListener then add your message to onCompletion method:
#Override
public void onCompletion(MediaPlayer mp) {
//message
}

What are my mistakes by using sensors?

I slowly make my first tries with using sensors. And i want to show the actual air pressure (My smartphone defently have a airpressuresensor.)
I look at some code and copy some code, and build the app. Now it doesnt work, and I dont know why. Does sensors need sth. in the manifest or so?
There are no errorcodes, just a applicationcrash...
the evil line seems to be, because if I build a try and catch around the app dont crash, but nothing happends. : sMgr.registerListener((SensorEventListener) this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
Here is the code:
public class MainActivity extends ActionBarActivity {
public Handler handler = new Handler();
public double a;
SensorManager sMgr;
Sensor sensor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sMgr = (SensorManager)getSystemService(SENSOR_SERVICE);
List<Sensor> sensors = sMgr.getSensorList(Sensor.TYPE_PRESSURE);
if(sensors.size() > 0) {
sensor = sensors.get(0);
sMgr.registerListener((SensorEventListener) this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float presure = event.values[0];
Log.d("PressureSens", ""+presure);
TextView textView = (TextView)findViewById(R.id.text);
textView.setText(""+ presure);
}

Resources