如何在列表视图中使用多个声音向Android onclick添加音频

时间:2015-05-06 23:41:16

标签: java android audio onclick

好的,我在这里有一个应用程序,我正在努力。我想要做的是基本上播放汽车在有人点击特定汽车时发出的声音。我已经在这里和google查了一下,但是他们都只处理了一个按钮,我有一个按钮,这让我很难决定走哪条路。我有一个声音可以工作,但每个声音都是一样的汽车。我怎样才能让它为特定的汽车点击选择特定的声音?我的java代码是:

    package com.example.listviewadapter;

    import java.util.ArrayList;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnItemClickListener {

        //variables declaration
        ArrayList<Car> arrayCars;
        ListView listViewCars;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 
            arrayCars=new ArrayList<Car>();


            Car bmw=new Car(R.drawable.bmw,"BMW 330ci", "Silver", 13588);
            Car audi=new Car(R.drawable.audi, "Audi A4", "Gray", 18000);
            Car opel=new Car(R.drawable.opel, "Opel Insigna", "Black", 14000);
            Car mercedes=new Car(R.drawable.mercedes, "Mercedes CLS 320","Black", 16000);
            Car ferrari=new Car(R.drawable.ferrari_enzo, "Ferrari Enzo","White", 93000);
            Car fiesta=new Car(R.drawable.ford_fiesta, "Ford Fiesta","Green", 18000);
            Car porshe=new Car(R.drawable.porshe_cayenne,"Porsche Cayenne","Dark Gray", 101000);
            Car lambo=new Car(R.drawable.lamborghini_gallardo, "Lamborghini Gallardo","orange", 100000);
            Car hyundai=new Car(R.drawable.hyundai_i30, "Hyundai i30","blue", 20000);
            Car honda=new Car(R.drawable.honda_accord, "Honda accord","red", 19000);    

            arrayCars.add(bmw);
            arrayCars.add(audi);
            arrayCars.add(opel);
            arrayCars.add(mercedes);
            arrayCars.add(ferrari);
            arrayCars.add(fiesta);
            arrayCars.add(porshe);
            arrayCars.add(lambo);
            arrayCars.add(hyundai);
            arrayCars.add(honda);




            listViewCars = (ListView) findViewById(R.id.list_cars);
            ListCarsAdapter adapter = new ListCarsAdapter(this, arrayCars);
            listViewCars.setAdapter(adapter);
            listViewCars.setOnItemClickListener(this);
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Car selectedCar = arrayCars.get(position);
            Toast.makeText(this,"You've selected " + selectedCar.getModel()+
                    " "+selectedCar.getColor()
                    ,Toast.LENGTH_SHORT).show();

    MediaPlayer  mPlayer = MediaPlayer.create(this, R.raw.audio1);
    mPlayer .start();;

        }

    }

1 个答案:

答案 0 :(得分:0)

你应该看一下Media Playback android tutorial。 您可以将MP3文件放在res/raw文件夹中,并将标识符存储在每个对象中:

public class Car {
    private int audioID;

    public int getAudioID() {
        return audioID;
    }
}

只需使用MediaPlayer

播放
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Car selectedCar = arrayCars.get(position);
    Toast.makeText(this,"You've selected " + selectedCar.getModel()+
            " "+selectedCar.getColor()
            ,Toast.LENGTH_SHORT).show();
    MediaPlayer mediaPlayer = MediaPlayer.create(this, car.getAudioID());
    mediaPlayer.start();
}