2014-01-06 55 views
1

我的音響板應用程序出現問題。當沒有任何聲音播放,你嘗試設置鈴聲的應用程序停止工作。 我認爲這與變量未被初始化有關,因爲沒有選擇聲音。 現在的任何想法,我可以防止這種情況發生?音板應用程序鈴聲

感謝您的期待。 :-)

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Intent; 
import android.content.res.AssetFileDescriptor; 
import android.content.res.Resources; 
import android.media.MediaPlayer; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.ContextMenu; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 
import com.sherlocksoundboard.R; 


@SuppressLint("SdCardPath") 
public class Content extends Activity { 


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 back to you 
    final int[] buttonIds = { R.id.Button01, R.id.Button02, R.id.Button03, R.id.Button04, R.id.Button05, R.id.Button06,R.id.button1}; 
    final int[] soundIds = { R.raw.deargod, R.raw.donttalk, R.raw.lookat, R.raw.stop, R.raw.text,R.raw.john, R.raw.sherlock, }; 



    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 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 
    } 

    String path="/sdcard/sounds/"; 
    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 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 

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

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); 
    getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null); 
    Uri newUri = getContentResolver().insert(uri, values); 
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri); 



    return true; 

} 

//Save in Notification Folder 

@SuppressLint("SdCardPath") 
public boolean savenot(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 
    } 

    String path="/sdcard/sounds/"; 
    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 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 

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




    Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); 
    getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null); 
    Uri newUri = getContentResolver().insert(uri, values); 
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri); 



return true; 



} 
} 

回答

0

下面的「if」語句是一種「糟糕的編程」,但沒有它,應用程序將崩潰。 它基本上規定,如果沒有選擇按鈕,則使用陣列中的第一個聲音。

if(soundButton!=null) 
{ 
    selectedSoundId = soundIds[1]; 
} 

我把 「如果」 語句以上在此之後 「for」 循環

for(int i = 0; i < buttonIds.length; i++) { 
     Button soundButton = (Button)findViewById(buttonIds[i]); 
     registerForContextMenu(soundButton); 
     soundButton.setOnClickListener(listener); 
相關問題