0
     public void getSound(String file) {
                    MediaPlayer mp;
                        try{
                            String SoundFile=file;
                            System.out.println("Here is the File"+SoundFile);
                            mp = MediaPlayer.create(getActivity().getBaseContext(),Uri.parse("android.resource://himanshu.sound/raw/"+file)); 
                            mp.start(); 
                            System.out.println("PLAYED");
                        }catch(Exception e){
                            System.out.println("Exception->"+e);
                        }   
                }

これは私のコード ファイル名で、アセットに由来する game.mp3 です。

今、私はオーディオを再生したい

これは私のxmlです

    <ImageView
                        android:id="@+id/soundbtn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:gravity="right"
                        android:onClick="getSound"
                        android:paddingLeft="5dp"
                        android:paddingRight="15dp"
                        android:paddingBottom="5dp"
                        android:src="@drawable/audio" />



mp = MediaPlayer.create(getActivity().getBaseContext(),Uri.parse("android.resource://himanshu.sound/raw/"+file));

この行でエラーが発生しています

getActivity().getBaseContext() 適切なものがそこにあるに違いないと思います

4

1 に答える 1

0

あなた』は、ImageButtonを持っている方が良いです

                  <ImageButton
                    android:id="@+id/soundbtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:gravity="right"
                    android:onClick="onClick"
                    android:paddingLeft="5dp"
                    android:paddingRight="15dp"
                    android:paddingBottom="5dp"
                    android:src="@drawable/audio" />

OnClickメソッドは別の署名を使用する必要があります

public final void onClick(View v) {

    // todo ............
     }
于 2013-03-08T08:41:07.430 に答える