1

好吧,我现在尝试了 4 个小时来制作一个按钮来播放声音。(就像一个音板应用程序)。我是初学者,如果我犯了愚蠢的错误,请见谅。

这是我的 .java 文件。

package com.example.androidtablayout;

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

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
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.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

public class newBoard extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    Toast.makeText(this, "Long touch to save as Ringtones.", Toast.LENGTH_LONG).show();


    // import sound files
    final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound1);
    final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound2);


    // play sound files on clicks
    Button s01 = (Button) findViewById(R.id.btn1); 
    s01.setText(this.getString(R.string.quote01));
    s01.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound01.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound01.start();                
            }

    });
    registerForContextMenu(s01);

    Button s02 = (Button) findViewById(R.id.btn2); 
    s02.setText(this.getString(R.string.quote02));
    s02.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound02.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sound02.start();
        }
    });
}

// perform save functions on long press
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/Notification");    
    }  

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

// detect which button was clicked, save as a ringtone with strings.xml
public boolean function1(int ressound){  

    //----
          String soundname = "";

           switch(ressound){
        case R.id.btn1:
        ressound = R.raw.sound1;
        soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote01));
        break;
        }

           switch(ressound){
        case R.id.btn2:
        ressound = R.raw.sound2;
        soundname = (this.getString(R.string.app_name)) + " - " +          (this.getString(R.string.quote02));
        break;
        }

          //and so on and so on.....
          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/ringtones/";
           String filename=soundname+".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, soundname);
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
           values.put(MediaStore.Audio.Media.ARTIST, "FRIDAY");
           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);


          String i = "Saved as Ringtone.";
           Toast.makeText(getApplicationContext(), i,
           Toast.LENGTH_LONG).show();

         return true;


          }

    //----


}

这是我的布局文件:

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:shrinkColumns="*"  android:stretchColumns="*" android:background="#ffffff"> 

 <!-- Row 2 with 3 columns -->
    <TableRow 
    android:id="@+id/tableRow1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent">  
    <Button 
        android:id="@+id/btn1" 
        android:text="@string/button1.1"
        android:layout_weight="1" 
        android:textColor="#000000"
        android:padding="20dip" 
        android:gravity="center"/>  

    <Button
        android:id="@+id/btn2"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="20dip"
        android:text="@string/button1.2"
        android:textColor="#000000" />

    <Button 
        android:id="@+id/btn6" 
        android:text="@string/button1.3"
        android:layout_weight="1" 
        android:textColor="#000000"
        android:padding="20dip" 
        android:gravity="center"/>
</TableRow> 

    <!-- Row 2 with 3 columns -->
    <TableRow 
        android:id="@+id/tableRow1" 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent">  
        <TextView 
            android:id="@+id/TextView04" 
            android:text="Row 2 column 1"
            android:layout_weight="1" 
            android:textColor="#000000"
            android:padding="20dip" 
            android:gravity="center"/>  
        <TextView 
            android:id="@+id/TextView04" 
            android:text="Row 2 column 2"
            android:layout_weight="1" 
            android:textColor="#000000"
            android:padding="20dip" 
            android:gravity="center"/>
        <TextView 
            android:id="@+id/TextView04" 
            android:text="Row 2 column 3"
            android:layout_weight="1" 
            android:textColor="#000000"
            android:padding="20dip" 
            android:gravity="center"/>
    </TableRow> 
</TableLayout>

我希望这足以找到我的错误。该应用程序正在运行,但没有任何声音播放。

提前致谢!

4

0 回答 0