gpt4 book ai didi

java - 按下按钮可根据微调器选择播放不同的 .mp3

转载 作者:行者123 更新时间:2023-12-01 15:45:41 25 4
gpt4 key购买 nike

包 com.RaffDev.RaffApp;

导入 android.app.Activity;导入 android.media.MediaPlayer;导入 android.os.Bundle;导入 android.view.View;导入 android.widget.Toast;导入 android.widget.AdapterView;导入 android.widget.ArrayAdapter;导入 android.widget.Spinner;

公共(public)类 RaffAppActivity 扩展 Activity 实现 View.OnClickListener,``AdapterView.OnItemSelectedListener{

/*Regular Variables*/
String copyright = "Raff App \n\nVersion: Alpha 0.0.1\n\n(c) Copyright Raff App contributors and others 2011. All rights reserverd";
public int soundSelection;
MediaPlayer mp;

/*Called at the beginning of the application*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.soundEntries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}

/*Button Functions*/
public void onClick(View v){}

public void infoClick(View v){

Toast.makeText(this, copyright, Toast.LENGTH_LONG).show();
}

public void pressPlay(View v){
switch(soundSelection){
case 1:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(), R.raw.pleaseprettyface);
mp.start();
break;
case 2:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.sheeesh);
mp.start();
break;
case 3:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.heaintdodat);
mp.start();
break;
case 4:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.thedousky);
mp.start();
break;
case 5:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.nmouth);
mp.start();
break;


}

}



/*Spinner Functions*/
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
soundSelection = (Integer) parent.getItemAtPosition(pos);
}

public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}

}

最佳答案

第一次运行此代码并从微调器中选择某些内容时,

mp 为 null。在 pressPlay 中添加空检查,或者在 onCreate() 中创建 mp 实例。

关于java - 按下按钮可根据微调器选择播放不同的 .mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7070946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com