Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import com.bliynd.myapp.R.raw;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.media.MediaPlayer;
- import android.view.ContextMenu;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView.AdapterContextMenuInfo;
- import android.widget.Toast;
- public class MyAppActivity extends Activity implements OnClickListener {
- private MediaPlayer mp;
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- menu.setHeaderTitle("Save As:");
- menu.add(0, v.getId(), 0, "Notification");
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- if(item.getTitle()=="Notification"){function1(item.getItemId());}
- else {return false;}
- return true;
- }
- public void function1(int id){
- if (savenot(????????)){ //<<?????? needs to be the sound file of the button pressed
- // Code if successful
- Toast.makeText(this, "Notification Saved", Toast.LENGTH_SHORT).show();
- }
- else
- {
- // Code if unsuccessful
- Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show(); }
- }
- public boolean savenot(int ressound){
- 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= Environment.getExternalStorageDirectory() + "/media/audio/notifications/";
- String filename="sound"+".mp3"; //<<< needs to be sound1.mp3 or sound2.mp3 ect..
- 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, "sound");
- values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
- values.put(MediaStore.Audio.Media.ARTIST, "bliynd");
- values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
- values.put(MediaStore.Audio.Media.IS_ALARM, false);
- values.put(MediaStore.Audio.Media.IS_MUSIC, false);
- //Insert it into the database
- this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
- return true;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- View button1 = findViewById(R.id.sound1);
- registerForContextMenu(button1);
- View button2 = findViewById(R.id.sound2);
- registerForContextMenu(button2);
- registerForContextMenu(button51);
- button1.setOnClickListener(this);
- button2.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // Play only one sound at a time
- if(mp != null) mp.release();
- // Find which ImageButton was pressed and take appropriate action
- switch(v.getId()){
- // sound1
- case R.id.sound1:
- mp = MediaPlayer.create(this, R.raw.sound1);
- break;
- case R.id.sound2:
- mp = MediaPlayer.create(this, R.raw.sound2);
- break;
- }
- mp.seekTo(0);
- mp.start();
- }
- @Override
- public void onPause() {
- super.onPause();
- // Release the MediaPlayer if going into background
- if(mp != null) mp.release();
- }
Add Comment
Please, Sign In to add comment