나는 인터넷에서 다운로드 한 .ogg
오디오 파일을 재생하는 코드가 있습니다. 나는 오류가없는, 그래서 그것을 실행할 수 있지만 다음 응용 프로그램 충돌 : Android에서 오디오 파일을 재생하려면 어떻게해야합니까?
- 내가
main.xml
파일에 넣고 :<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="Click on the screen to start playing" android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView> </LinearLayout>
그리고 I를package play.my.sound; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.media.SoundPool.OnLoadCompleteListener; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; public class PlaySound2Activity extends Activity { private SoundPool soundPool; private int soundID; boolean loaded = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View view = findViewById(R.id.textView1); view.setOnClickListener((OnClickListener) this); // Set the hardware buttons to control the music this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Load the sound soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load("sound1.ogg", 1); } public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // Getting the user sound settings AudioManager audioManager = (AudioManager) getSystemService (AUDIO_SERVICE); float actualVolume = (float) audioManager .getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float) audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = actualVolume/maxVolume; // Is the sound loaded already? if (loaded) { soundPool.play(soundID, volume, volume, 1, 0, 1f); Log.e("Test", "Played sound"); } } return false; } }
가 나는 두 가지 문제가 있다고 생각 그것이 옳은지 모르겠다.
workspace->SoundPlay2
폴더에 파일을 넣었습니다. 왜냐하면 res 폴더에 문제가 있었고, 존재하는 두 개의 res 폴더에 넣으려고했기 때문입니다.
이 내 콘솔에서이다 :
[2012-01-04 19:38:16 - PlaySound2] Failed to install PlaySound2.apk on device 'emulator-5554': timeout
[2012-01-04 19:38:16 - PlaySound2] Launch canceled!
[2012-01-04 19:47:33 - PlaySound2] Error in an XML file: aborting build.
[2012-01-04 19:52:34 - PlaySound2] res\layout\main.xml:0: error: Resource entry main is already defined.
[2012-01-04 19:52:34 - PlaySound2] res\layout\main.out.xml:0: Originally defined here.
[2012-01-04 19:52:34 - PlaySound2] C:\Users\Natalia\workspace\PlaySound2\res\layout\main.out.xml:1: error: Error parsing XML: no element found
내가 "Android Sounds - Tutorial"에서이 예제를했다. 오디오 파일, 구체적으로 .wav
파일을 재생하고 싶습니다.
MediaPlayer 클래스 및 해당 특성 (durantion, sample rate ...)에서 허용되는 파일에 대한 정보를 어디에서 찾을 수 있는지 알 수 없습니다.이 위치를 알 수 있습니까?
이것은 로그 고양이에있는 오류입니다. E/AndroidRuntime (739) : java.lang.RuntimeException : 활동을 시작할 수 없습니다. ComponentInfo {play.my.sound/play.my.sound.PlaySound2Activity} : java .lang.ClassCastException : play.my.sound.PlaySound2Activity를 android.view.View로 캐스팅 할 수 없습니다. $ OnClickListener –
Google에서 간단한 답변을 찾으려면 간단히 검색하지 않는 것이 좋습니다. Google의 '지원되는 오디오 형식'을 간단히 검색하면 http://developer.android.com/guide/appendix/media-formats.html이 생성되었습니다. http://developer.android.com/guide/topics/media/mediaplayer.html – manjusg