0

ボタンをクリックしてAndroidアプリでサウンドを再生したい。ただし、エミュレーターでは、アプリは起動直後に停止します。

public class FullscreenActivity extends Activity {

    private MediaPlayer mediaPlayer = MediaPlayer.create(this.getApplicationContext(), R.raw.sound_file_1);
    ......

    public void playsound(View view){

       mediaPlayer.setLooping(true);
       if(mediaPlayer.isPlaying() ){
           mediaPlayer.pause();
       }
       else{
           mediaPlayer.start();
       }
}

これは、layout-xml でこのボタンによって呼び出される関数です。

<Button android:id="@+id/dummy_button"
                style="?buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick = "playsound"
                android:text="@string/dummy_button" />

ファイルが見つからないのではないかと心配していますが、理由がわかりません。フォルダー「projectname /src/main/res/raw/sound_file_1」にあります 最初、ファイルの末尾は「.mp3」でしたが、これが役立つと思ったので削除しました。

デバイスの logcat には、次のようなエラー メッセージがあります。

09-21 00:00:58.044    2075-2075/com.example.projectname E/Trace﹕ error opening trace file: No such file or directory (2)

ご協力ありがとうございました!

4

0 に答える 0