有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java声卡铃声/通知音

我想能够设置的声音,我在声卡作为铃声和通知音。我在这个网站上查过了,似乎找不到解决办法。有什么想法吗

感谢您的关注。:-)

我的代码是

  package com.sherlock;

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

import 安卓.app.Activity;
import 安卓.content.ContentValues;
import 安卓.content.Intent;
import 安卓.content.res.AssetFileDescriptor;
import 安卓.content.res.Resources;
import 安卓.media.MediaPlayer;
import 安卓.media.RingtoneManager;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.os.Environment;
import 安卓.provider.MediaStore;
import 安卓.view.ContextMenu;
import 安卓.view.ContextMenu.ContextMenuInfo;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.Toast;
import com.sherlocksoundboard.R;

public class Content extends Activity {

     int savetype = RingtoneManager.TYPE_RINGTONE;

        int selectedSoundId;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.content);

            final MediaPlayer player = new MediaPlayer();
            final Resources res = getResources();

            //just keep them in the same order, e.g. button01 is tied to backtoyou
            final int[] buttonIds = { R.id.button1, R.id.Button01, R.id.Button02, R.id.Button03, R.id.Button04,R.id.Button05,R.id.Button06,};
            final int[] soundIds = { R.raw.sherlock, R.raw.deargod, R.raw.donttalk, R.raw.lookat, R.raw.stop,R.raw.text,R.raw.john, };

            View.OnClickListener listener = new View.OnClickListener() {
                public void onClick(View v) {
                    //find the index that matches the button's ID, and then reset
                    //the MediaPlayer instance, set the data source to the corresponding
                    //sound effect, prepare it, and start it playing.
                    for(int i = 0; i < buttonIds.length; i++) {
                        if(v.getId() == buttonIds[i]) {
                            selectedSoundId = soundIds[i];
                            AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
                            player.reset();
                            try {
                                player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                            } catch (IllegalArgumentException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IllegalStateException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            try {
                                player.prepare();
                            } catch (IllegalStateException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            player.start();
                            break;
                        }
                    }
                }
            };



            //set the same listener for every button ID, no need
            //to keep a reference to every button
            for(int i = 0; i < buttonIds.length; i++) {
                Button soundButton = (Button)findViewById(buttonIds[i]);
                registerForContextMenu(soundButton);
                soundButton.setOnClickListener(listener);

            }



        }

        @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(selectedSoundId)){   
              // 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(selectedSoundId)){   
              // 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 selectedSoundId){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(selectedSoundId);
         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=Environment.getExternalStorageDirectory().getPath()+"/media/ringtone/";


         String filename="Benton"+".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, "Benton");   
         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, true);   
         values.put(MediaStore.Audio.Media.IS_MUSIC, false);    

         Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
         Uri newUri = getContentResolver().insert(uri, values); 
         RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);




         return true; 
        }








        //Save in Notification Folder

        public boolean savenot(int selectedSoundId){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(selectedSoundId);
         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().getPath()+"/media/ringtone/";


         String filename= "Sherlock"+".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_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

         File k = new File(path, filename);   
         ContentValues values = new ContentValues();   
         values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
         values.put(MediaStore.MediaColumns.TITLE, "Sherlock");
         values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
         values.put(MediaStore.Audio.Media.ARTIST, "Elvis");  
         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
         Uri newUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

         // set as ringtone
         RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri);


         return true; 



        }







        }

共 (1) 个答案

  1. # 1 楼答案

    To set any audio file as ringtone it should be stored into database. //then only it is setted otherwise it wouldn't give any error but not set as default ringtone. //you can add more details BY content.put() method.

    String filepath ="/sdcard/play2.mp3";
    File ringtoneFile = new File(filepath);
    
    ContentValues content = new ContentValues();
    content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
    content.put(MediaStore.MediaColumns.TITLE, "chinnu");
    content.put(MediaStore.MediaColumns.SIZE, 215454);
    content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
    content.put(MediaStore.Audio.Media.DURATION, 230);
    content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    content.put(MediaStore.Audio.Media.IS_ALARM, false);
    content.put(MediaStore.Audio.Media.IS_MUSIC, false);
    
    
    //Insert it into the database
    Log.i(TAG, "the absolute path of the file is :"+
    ringtoneFile.getAbsolutePath());
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(
    ringtoneFile.getAbsolutePath());
    Uri newUri = context.getContentResolver().insert(uri, content);
    ringtoneUri = newUri; 
    Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
    RingtoneManager.setActualDefaultRingtoneUri(context,
    RingtoneManager.TYPE_RINGTONE,newUri);