package com.ccgames.pfsoundboardplus;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.ccgames.pfsoundboardplus.R;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
public class newBoard extends Activity {
int currentPlayingID=-1;
/** Called when the activity is first created. */
// import sound files
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// play sound files on clicks
Button s01 = (Button) findViewById(R.id.button01);
s01.setText(this.getString(R.string.quote01));
s01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(1);
}
});
registerForContextMenu(s01);
Button s02 = (Button) findViewById(R.id.button02);
s02.setText(this.getString(R.string.quote02));
s02.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(2);
}
});
registerForContextMenu(s02);
Button s03 = (Button) findViewById(R.id.button03);
s03.setText(this.getString(R.string.quote03));
s03.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(3);
}
});
registerForContextMenu(s03);
Button s04 = (Button) findViewById(R.id.button04);
s04.setText(this.getString(R.string.quote04));
s04.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(4);
}
});
registerForContextMenu(s04);
Button s05 = (Button) findViewById(R.id.button05);
s05.setText(this.getString(R.string.quote05));
s05.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(5);
}
});
registerForContextMenu(s05);
Button s06 = (Button) findViewById(R.id.button06);
s06.setText(this.getString(R.string.quote06));
s06.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(6);
}
});
registerForContextMenu(s06);
Button s07 = (Button) findViewById(R.id.button07);
s07.setText(this.getString(R.string.quote07));
s07.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(7);
}
});
registerForContextMenu(s07);
Button s08 = (Button) findViewById(R.id.button08);
s08.setText(this.getString(R.string.quote08));
s08.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(8);
}
});
registerForContextMenu(s08);
Button s09 = (Button) findViewById(R.id.button09);
s09.setText(this.getString(R.string.quote09));
s09.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(9);
}
});
registerForContextMenu(s09);
Button s10 = (Button) findViewById(R.id.button10);
s10.setText(this.getString(R.string.quote10));
s10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(10);
}
});
registerForContextMenu(s10);
Button s11 = (Button) findViewById(R.id.button11);
s11.setText(this.getString(R.string.quote11));
s11.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(11);
}
});
registerForContextMenu(s11);
Button s12 = (Button) findViewById(R.id.button12);
s12.setText(this.getString(R.string.quote12));
s12.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(12);
}
});
registerForContextMenu(s12);
Button s13 = (Button) findViewById(R.id.button13);
s13.setText(this.getString(R.string.quote13));
s13.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(13);
}
});
registerForContextMenu(s13);
Button s14 = (Button) findViewById(R.id.button14);
s14.setText(this.getString(R.string.quote14));
s14.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(14);
}
});
registerForContextMenu(s14);
Button s15 = (Button) findViewById(R.id.button15);
s15.setText(this.getString(R.string.quote15));
s15.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(15);
}
});
registerForContextMenu(s15);
Button s16 = (Button) findViewById(R.id.button16);
s16.setText(this.getString(R.string.quote16));
s16.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(16);
}
});
registerForContextMenu(s16);
Button s17 = (Button) findViewById(R.id.button17);
s17.setText(this.getString(R.string.quote17));
s17.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(17);
}
});
registerForContextMenu(s17);
Button s18 = (Button) findViewById(R.id.button18);
s18.setText(this.getString(R.string.quote18));
s18.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(18);
}
});
registerForContextMenu(s18);
Button s19 = (Button) findViewById(R.id.button19);
s19.setText(this.getString(R.string.quote19));
s19.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(19);
}
});
registerForContextMenu(s19);
Button s20 = (Button) findViewById(R.id.button20);
s20.setText(this.getString(R.string.quote20));
s20.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(20);
}
});
registerForContextMenu(s20);
Button s21 = (Button) findViewById(R.id.button21);
s21.setText(this.getString(R.string.quote21));
s21.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(21);
}
});
registerForContextMenu(s21);
Button s22 = (Button) findViewById(R.id.button22);
s22.setText(this.getString(R.string.quote22));
s22.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(22);
}
});
registerForContextMenu(s22);
Button s23 = (Button) findViewById(R.id.button23);
s23.setText(this.getString(R.string.quote23));
s23.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(23);
}
});
registerForContextMenu(s23);
Button s24 = (Button) findViewById(R.id.button24);
s24.setText(this.getString(R.string.quote24));
s24.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(24);
}
});
registerForContextMenu(s24);
Button s25 = (Button) findViewById(R.id.button25);
s25.setText(this.getString(R.string.quote25));
s25.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(25);
}
});
registerForContextMenu(s25);
Button s26 = (Button) findViewById(R.id.button26);
s26.setText(this.getString(R.string.quote26));
s26.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(26);
}
});
registerForContextMenu(s26);
Button s27 = (Button) findViewById(R.id.button27);
s27.setText(this.getString(R.string.quote27));
s27.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(27);
}
});
registerForContextMenu(s27);
Button s28 = (Button) findViewById(R.id.button28);
s28.setText(this.getString(R.string.quote28));
s28.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(28);
}
});
registerForContextMenu(s28);
Button s29 = (Button) findViewById(R.id.button29);
s29.setText(this.getString(R.string.quote29));
s29.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(29);
}
});
registerForContextMenu(s29);
Button s30 = (Button) findViewById(R.id.button30);
s30.setText(this.getString(R.string.quote30));
s30.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(30);
}
});
registerForContextMenu(s30);
Button s31 = (Button) findViewById(R.id.button31);
s31.setText(this.getString(R.string.quote31));
s31.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(31);
}
});
registerForContextMenu(s31);
Button s32 = (Button) findViewById(R.id.button32);
s32.setText(this.getString(R.string.quote32));
s32.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(32);
}
});
registerForContextMenu(s32);
Button s33 = (Button) findViewById(R.id.button33);
s33.setText(this.getString(R.string.quote33));
s33.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(33);
}
});
registerForContextMenu(s33);
Button s34 = (Button) findViewById(R.id.button34);
s34.setText(this.getString(R.string.quote34));
s34.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(34);
}
});
registerForContextMenu(s34);
Button s35 = (Button) findViewById(R.id.button35);
s35.setText(this.getString(R.string.quote35));
s35.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playSound(35);
}
});
registerForContextMenu(s35);
WebView webview;
}
MediaPlayer m=null;
private void playSound(int id){
stopCurrentPlayer();
try {
switch(id){
case 1:
m=MediaPlayer.create(this, R.raw.sound01);
break;
case 2:
m=MediaPlayer.create(this, R.raw.sound02);
break;
case 3:
m=MediaPlayer.create(this, R.raw.sound03);
break;
case 4:
m=MediaPlayer.create(this, R.raw.sound04);
break;
case 5:
m=MediaPlayer.create(this, R.raw.sound05);
break;
case 6:
m=MediaPlayer.create(this, R.raw.sound06);
break;
case 7:
m=MediaPlayer.create(this, R.raw.sound07);
break;
case 8:
m=MediaPlayer.create(this, R.raw.sound08);
break;
case 9:
m=MediaPlayer.create(this, R.raw.sound09);
break;
case 10:
m=MediaPlayer.create(this, R.raw.sound10);
break;
case 11:
m=MediaPlayer.create(this, R.raw.sound11);
break;
case 12:
m=MediaPlayer.create(this, R.raw.sound12);
break;
case 13:
m=MediaPlayer.create(this, R.raw.sound13);
break;
case 14:
m=MediaPlayer.create(this, R.raw.sound14);
break;
case 15:
m=MediaPlayer.create(this, R.raw.sound15);
break;
case 16:
m=MediaPlayer.create(this, R.raw.sound16);
break;
case 17:
m=MediaPlayer.create(this, R.raw.sound17);
break;
case 18:
m=MediaPlayer.create(this, R.raw.sound18);
break;
case 19:
m=MediaPlayer.create(this, R.raw.sound19);
break;
case 20:
m=MediaPlayer.create(this, R.raw.sound20);
break;
case 21:
m=MediaPlayer.create(this, R.raw.sound21);
break;
case 22:
m=MediaPlayer.create(this, R.raw.sound22);
break;
case 23:
m=MediaPlayer.create(this, R.raw.sound23);
break;
case 24:
m=MediaPlayer.create(this, R.raw.sound24);
break;
case 25:
m=MediaPlayer.create(this, R.raw.sound25);
break;
case 26:
m=MediaPlayer.create(this, R.raw.sound26);
break;
case 27:
m=MediaPlayer.create(this, R.raw.sound27);
break;
case 28:
m=MediaPlayer.create(this, R.raw.sound28);
break;
case 29:
m=MediaPlayer.create(this, R.raw.sound29);
break;
case 30:
m=MediaPlayer.create(this, R.raw.sound30);
break;
case 31:
m=MediaPlayer.create(this, R.raw.sound31);
break;
case 32:
m=MediaPlayer.create(this, R.raw.sound32);
break;
case 33:
m=MediaPlayer.create(this, R.raw.sound33);
break;
case 34:
m=MediaPlayer.create(this, R.raw.sound34);
break;
case 35:
m=MediaPlayer.create(this, R.raw.sound35);
break;
}
try {
m.prepare();
} catch (Exception e) {
// TODO: handle exception
}
m.start();
} catch (Exception e) {
// TODO: handle exception
}
}
private void stopCurrentPlayer(){
try {
if(m != null){
m.stop();
m.release();
}
} catch (Exception e) {
// TODO: handle exception
}
}
// perform save functions on long press
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, v.getId(), 0, "Ringtone/Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone/Notification"){function1(item.getItemId());}
else {return false;}
return true;
}
// detect which button was clicked, save as a ringtone with strings.xml
public boolean function1(int ressound){
//----
String soundname = "";
switch(ressound){
case R.id.button01:
ressound = R.raw.sound01;
soundname = (this.getString(R.string.quote01));
break;
}
switch(ressound){
case R.id.button02:
ressound = R.raw.sound02;
soundname = (this.getString(R.string.quote02));
break;
}
switch(ressound){
case R.id.button03:
ressound = R.raw.sound03;
soundname = (this.getString(R.string.quote03));
break;
}
switch(ressound){
case R.id.button04:
ressound = R.raw.sound04;
soundname = (this.getString(R.string.quote04));
break;
}
switch(ressound){
case R.id.button05:
ressound = R.raw.sound05;
soundname = (this.getString(R.string.quote05));
break;
}
switch(ressound){
case R.id.button06:
ressound = R.raw.sound06;
soundname = (this.getString(R.string.quote06));
break;
}
switch(ressound){
case R.id.button07:
ressound = R.raw.sound07;
soundname = (this.getString(R.string.quote07));
break;
}
switch(ressound){
case R.id.button08:
ressound = R.raw.sound08;
soundname = (this.getString(R.string.quote08));
break;
}
switch(ressound){
case R.id.button09:
ressound = R.raw.sound09;
soundname = (this.getString(R.string.quote09));
break;
}
switch(ressound){
case R.id.button10:
ressound = R.raw.sound10;
soundname = (this.getString(R.string.quote10));
break;
}
switch(ressound){
case R.id.button11:
ressound = R.raw.sound11;
soundname = (this.getString(R.string.quote11));
break;
}
switch(ressound){
case R.id.button12:
ressound = R.raw.sound12;
soundname = (this.getString(R.string.quote12));
break;
}
switch(ressound){
case R.id.button13:
ressound = R.raw.sound13;
soundname = (this.getString(R.string.quote13));
break;
}
switch(ressound){
case R.id.button14:
ressound = R.raw.sound14;
soundname = (this.getString(R.string.quote14));
break;
}
switch(ressound){
case R.id.button15:
ressound = R.raw.sound15;
soundname = (this.getString(R.string.quote15));
break;
}
switch(ressound){
case R.id.button16:
ressound = R.raw.sound16;
soundname = (this.getString(R.string.quote16));
break;
}
switch(ressound){
case R.id.button17:
ressound = R.raw.sound17;
soundname = (this.getString(R.string.quote17));
break;
}
switch(ressound){
case R.id.button18:
ressound = R.raw.sound18;
soundname = (this.getString(R.string.quote18));
break;
}
switch(ressound){
case R.id.button19:
ressound = R.raw.sound19;
soundname = (this.getString(R.string.quote19));
break;
}
switch(ressound){
case R.id.button20:
ressound = R.raw.sound20;
soundname = (this.getString(R.string.quote20));
break;
}
switch(ressound){
case R.id.button21:
ressound = R.raw.sound21;
soundname = (this.getString(R.string.quote21));
break;
}
switch(ressound){
case R.id.button22:
ressound = R.raw.sound22;
soundname = (this.getString(R.string.quote22));
break;
}
switch(ressound){
case R.id.button23:
ressound = R.raw.sound23;
soundname = (this.getString(R.string.quote23));
break;
}
switch(ressound){
case R.id.button24:
ressound = R.raw.sound24;
soundname = (this.getString(R.string.quote24));
break;
}
switch(ressound){
case R.id.button25:
ressound = R.raw.sound25;
soundname = (this.getString(R.string.quote25));
break;
}
switch(ressound){
case R.id.button26:
ressound = R.raw.sound26;
soundname = (this.getString(R.string.quote26));
break;
}
switch(ressound){
case R.id.button27:
ressound = R.raw.sound27;
soundname = (this.getString(R.string.quote27));
break;
}
switch(ressound){
case R.id.button28:
ressound = R.raw.sound28;
soundname = (this.getString(R.string.quote28));
break;
}
switch(ressound){
case R.id.button29:
ressound = R.raw.sound29;
soundname = (this.getString(R.string.quote29));
break;
}
switch(ressound){
case R.id.button30:
ressound = R.raw.sound30;
soundname = (this.getString(R.string.quote30));
break;
}
switch(ressound){
case R.id.button31:
ressound = R.raw.sound31;
soundname = (this.getString(R.string.quote31));
break;
}
switch(ressound){
case R.id.button32:
ressound = R.raw.sound32;
soundname = (this.getString(R.string.quote32));
break;
}
switch(ressound){
case R.id.button33:
ressound = R.raw.sound33;
soundname = (this.getString(R.string.quote33));
break;
}
switch(ressound){
case R.id.button34:
ressound = R.raw.sound34;
soundname = (this.getString(R.string.quote34));
break;
}
switch(ressound){
case R.id.button35:
ressound = R.raw.sound35;
soundname = (this.getString(R.string.quote35));
break;
}
//and so on and so on.....
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
String path="/sdcard/media/audio/ringtones/";
String filename=soundname+".ogg";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, soundname);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "FRIDAY");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
String i = "Saved as Ringtone.";
Toast.makeText(getApplicationContext(), i,
Toast.LENGTH_LONG).show();
return true;
}
//----
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
stopCurrentPlayer();
}
return super.onKeyDown(keyCode, event);
}
}
和清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ccgames.pfsoundboardplus"
android:versionCode="6"
android:versionName="1.5"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<supports-screens android:xlargeScreens="true" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".newBoard"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:debuggable="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation" />
</application>
</manifest>
我只更改了清单,特别是屏幕布局和电话。在主要活动中我只添加了按钮。在Java中,我添加了与新按钮相对应的事件(真正简单的复制和粘贴)。更新的主要目的是添加更多声音。它适用于我测试的其他所有手机,我有一些客户报告,当他们按下按钮时没有声音播放。但是只有GALXY S3才有我缺少的东西?