简体   繁体   中英

Android Java soundboard code only setting first button as ringtone/etc

I've been playing around with this code and I'm obviously trying to make a soundboard that will save ringtones on longclicks and etc.

Though, this is the basic code for one button. I've been duplicating the same code on top of each other, trying to make arrays, just everything.. I can't get this code to work the way I want it to.

When they are multiple buttons and more codes.

I can get the sounds to all play for the buttons. I can get the menu to pop up for each button. Tho, the first button is always the one that gets saved, and the rest just fall off the map.

I assume that my data flow on the following code is not getting cut off and being picked up by the other sound buttons. Either way, it doesn't work. Please help me.

public class Soundboard extends Activity {
    private SoundManager mSoundManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.sound1);

        Button SB = (Button)findViewById(R.id.sound1);

        SB.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
               mSoundManager.playSound(1);                      
            }
        });

        Button btn = (Button) findViewById(R.id.sound1);
        registerForContextMenu(btn);
    }

    @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, "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){  
        if (savering(R.raw.sound1)) {   
          // 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.sound1)) {   
          // 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/audio/ringtones/";

        String filename="ohhh"+".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, "Ohhh Ringtone");   
        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, 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; 
    }

    //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="ohhh"+".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, "Ohhh Notification");   
        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, 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; 
    }
}

I'm just learning Android programming myself, but it seems like this is why you're always saving the first sound:

savering(R.raw.sound1)

and

savenot(R.raw.sound1)

Both of those are explicitly saving the first sound. Unless you've written a separate function1 and function2 for each button. But there's got to be a better way to manage saving than that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM