Klingelton via ContextMenü von Button einstellen

  • 2 Antworten
  • Letztes Antwortdatum
J

Jajobe

Erfahrenes Mitglied
14
Hallo.
Ich habe eine App programmiert, die Sounds bei einem Button Klick abspielt.
Jetzt möchte ich, dass man die Sounds auch als Klingelton und Notification Ton speichern kann.
Aber ich finde irgendwie keine Lösung wie dies klappen soll.

Hier mal meine Activity:

Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CamaroActivity extends Activity {

	private static MediaPlayer mp;
	Button camaro;
	int buttonPlay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.camaro);
		
		
		buttonPlay = -1;
		mp = MediaPlayer.create(getApplicationContext(), R.raw.camaro);
		
		camaro=(Button) findViewById (R.id.camaro);
        registerForContextMenu(camaro);

		
		camaro.setOnClickListener (new OnClickListener() {
			public void onClick (View v) {
				if(buttonPlay == 0){
					mp = MediaPlayer.create(getApplicationContext(), R.raw.camaro);
					buttonPlay = 1;
					startStopSound();
				}
				else if(buttonPlay == 1){
					startStopSound();
				}
				else {
					startStopSound();
					mp = MediaPlayer.create(getApplicationContext(), R.raw.camaro);
					buttonPlay = 1;
					startStopSound();
				}
			}
		});	
		
		
	}
	
	private void startStopSound(){ 
		
		if(buttonPlay == -1){
			mp.stop();
			mp.reset();
			mp.release();
			buttonPlay = 0;
		}
		else if(mp.isPlaying() == true){
			mp.stop();
			mp.reset();
			mp.release();
			buttonPlay = 0;
		}
		else{
			mp.start();
			}
	}
	@Override
	protected void onPause(){
		super.onPause();
		
		try{
		if(mp.isPlaying() == true){
					mp.stop();
					mp.reset();
					mp.release();
					buttonPlay = 0;
		}
		}catch(IllegalStateException e){}		
	}
	
	
	
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Set as...");
        menu.add(0, v.getId(), 0, "Ringtone");
        menu.add(0, v.getId(), 0, "Notification");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
        else if(item.getTitle()=="Notification"){function2(item.getItemId());}
        else {return false;}
    return true;
    }

    public void function1(int id){
        Toast.makeText(this, "Ringtone Saved", Toast.LENGTH_SHORT).show();
    }
    public void function2(int id){
        Toast.makeText(this, "Notification Saved", Toast.LENGTH_SHORT).show();
    }




boolean saveas(int camaro){
     byte[] buffer=null;
     InputStream fIn = getBaseContext().getResources().openRawResource(camaro);
     int size=36462;

     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/";
     String filename="camaro.mp3";

     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, "Camaro");
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
     values.put(MediaStore.Audio.Media.ARTIST, "we");
     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);

     return false;}}

Permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
 
Keiner eine Idee?
 
Habe jetzt doch ne Lösung gefunden.
Falls jemand mal des gleich Problem hat, hier mein code:

Code:
 @Override   
    public boolean onContextItemSelected(MenuItem item) { 
        if(item.getTitle()=="Ringtone"){function1(item.getItemId());}   
        else if(item.getTitle()=="Notification"){function2(item.getItemId());}  
        else {return false;}
        return true; 
    }

    public void function1(int id){  
        if (savering(R.raw.corvette)) {   
          // Code if successful   
          Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show(); 
        }           
        else           
        { 
          // Code if unsuccessful   
          Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show();
        }

    }
    public void function2(int id){   
        if (savenot(R.raw.corvette)) {   
          // Code if successful   
          Toast.makeText(this, "Saved as Notification", Toast.LENGTH_SHORT).show(); 
        }           
        else           
        { 
          // Code if unsuccessful   
          Toast.makeText(this, "Failed - Check your SDCard", Toast.LENGTH_SHORT).show(); 
        }
    }

    //Save into Ring tone Folder
    public boolean savering(int ressound){
        byte[] buffer=null;
        InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
        int size=50; 

        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/";

        String filename="Corvette Z06"+".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, "Corvette Z06 Motorsound");   
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");   
        values.put(MediaStore.Audio.Media.ARTIST, "weee");   
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);   
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);   
        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; 
    }

    //Save in Notification Folder
    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="/sdcard/media/audio/notifications/";

        String filename="Corvette Z06"+".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, "Corvette Z06 Motorsound");   
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");   
        values.put(MediaStore.Audio.Media.ARTIST, "weee");   
        values.put(MediaStore.Audio.Media.IS_RINGTONE, false);   
        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);

        return true; 
    }
}
 
Zurück
Oben Unten