Guest User

Untitled

a guest
Jul 20th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. import com.bliynd.myapp.R.raw;
  8.  
  9. import android.app.Activity;
  10. import android.content.ContentValues;
  11. import android.content.Intent;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.os.Environment;
  15. import android.provider.MediaStore;
  16. import android.media.MediaPlayer;
  17. import android.view.ContextMenu;
  18. import android.view.ContextMenu.ContextMenuInfo;
  19. import android.view.MenuItem;
  20. import android.view.View;
  21. import android.view.View.OnClickListener;
  22. import android.widget.AdapterView.AdapterContextMenuInfo;
  23. import android.widget.Toast;
  24. public class MyAppActivity extends Activity implements OnClickListener {
  25. private MediaPlayer mp;
  26.  
  27. @Override
  28.  
  29. public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
  30. super.onCreateContextMenu(menu, v, menuInfo);
  31. menu.setHeaderTitle("Save As:");
  32. menu.add(0, v.getId(), 0, "Notification");
  33.  
  34. }
  35.  
  36. @Override
  37. public boolean onContextItemSelected(MenuItem item) {
  38. if(item.getTitle()=="Notification"){function1(item.getItemId());}
  39.  
  40. else {return false;}
  41. return true;
  42. }
  43.  
  44. public void function1(int id){
  45. if (savenot(????????)){ //<<?????? needs to be the sound file of the button pressed
  46. // Code if successful
  47. Toast.makeText(this, "Notification Saved", Toast.LENGTH_SHORT).show();
  48. }
  49. else
  50. {
  51. // Code if unsuccessful
  52. Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show(); }
  53. }
  54.  
  55. public boolean savenot(int ressound){
  56. byte[] buffer=null;
  57. InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
  58. int size=0;
  59.  
  60. try {
  61. size = fIn.available();
  62. buffer = new byte[size];
  63. fIn.read(buffer);
  64. fIn.close();
  65. } catch (IOException e) {
  66. // TODO Auto-generated catch block
  67. return false;
  68. }
  69. String path= Environment.getExternalStorageDirectory() + "/media/audio/notifications/";
  70. String filename="sound"+".mp3"; //<<< needs to be sound1.mp3 or sound2.mp3 ect..
  71.  
  72. boolean exists = (new File(path)).exists();
  73. if (!exists){new File(path).mkdirs();}
  74.  
  75. FileOutputStream save;
  76. try {
  77. save = new FileOutputStream(path+filename);
  78. save.write(buffer);
  79. save.flush();
  80. save.close();
  81. } catch (FileNotFoundException e) {
  82. // TODO Auto-generated catch block
  83. return false;
  84. } catch (IOException e) {
  85. // TODO Auto-generated catch block
  86. return false;
  87. }
  88.  
  89. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
  90.  
  91. File k = new File(path, filename);
  92.  
  93. ContentValues values = new ContentValues();
  94. values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
  95. values.put(MediaStore.MediaColumns.TITLE, "sound");
  96. values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
  97. values.put(MediaStore.Audio.Media.ARTIST, "bliynd");
  98. values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
  99. values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
  100. values.put(MediaStore.Audio.Media.IS_ALARM, false);
  101. values.put(MediaStore.Audio.Media.IS_MUSIC, false);
  102.  
  103. //Insert it into the database
  104. this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
  105.  
  106.  
  107. return true;
  108. }
  109. @Override
  110. public void onCreate(Bundle savedInstanceState) {
  111. super.onCreate(savedInstanceState);
  112. setContentView(R.layout.main);
  113.  
  114. View button1 = findViewById(R.id.sound1);
  115. registerForContextMenu(button1);
  116. View button2 = findViewById(R.id.sound2);
  117. registerForContextMenu(button2);
  118.  
  119. registerForContextMenu(button51);
  120. button1.setOnClickListener(this);
  121. button2.setOnClickListener(this);
  122.  
  123. }
  124.  
  125. @Override
  126. public void onClick(View v) {
  127. // TODO Auto-generated method stub
  128.  
  129. // Play only one sound at a time
  130. if(mp != null) mp.release();
  131.  
  132. // Find which ImageButton was pressed and take appropriate action
  133.  
  134. switch(v.getId()){
  135.  
  136. // sound1
  137. case R.id.sound1:
  138. mp = MediaPlayer.create(this, R.raw.sound1);
  139. break;
  140.  
  141. case R.id.sound2:
  142. mp = MediaPlayer.create(this, R.raw.sound2);
  143. break;
  144.  
  145.  
  146.  
  147. }
  148. mp.seekTo(0);
  149. mp.start();
  150. }
  151. @Override
  152. public void onPause() {
  153. super.onPause();
  154. // Release the MediaPlayer if going into background
  155. if(mp != null) mp.release();
  156. }
Add Comment
Please, Sign In to add comment