Tôi có mã để phát tệp âm thanh .ogg
mà tôi đã tải xuống từ internet. Tôi không có lỗi, vì vậy tôi có thể chạy nó, nhưng sau đó bị treo ứng dụng:Tôi làm cách nào để phát tệp âm thanh trong Android?
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;
}
}
Tôi nghĩ rằng tôi có hai vấn đề:
tôi đặt này trong file
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>
Và tôi không biết nếu nó là chính xác.Tôi đặt tệp
sound1.ogg
trong thư mụcworkspace->SoundPlay2
vì trong thư mục res tôi gặp sự cố và cũng có thể, tôi đã cố gắng đưa nó vào hai thư mục res tồn tại.
Đây là từ giao diện điều khiển của tôi:
[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
tôi lấy ví dụ này từ "Android Sounds - Tutorial". Tôi muốn phát tệp âm thanh, cụ thể hơn là tệp .wav
.
Tôi không biết nơi nào tôi có thể tìm thấy một số thông tin về các tệp được phép trong lớp MediaPlayer và đặc tính của chúng (durantion, sample rate ...) Bạn có thể cho tôi biết tôi có thể tìm thấy ở đâu không ??
Đây là lỗi firt tôi có trong mèo đăng nhập của mình: E/AndroidRuntime (739): java.lang.RuntimeException: Không thể bắt đầu hoạt động ComponentInfo {play.my.sound/play.my.sound.PlaySound2Activity}: java .lang.ClassCastException: play.my.sound.PlaySound2Activity không thể truyền sang android.view.View $ OnClickListener –
tại sao bạn không chỉ cần tìm kiếm trong google để tìm câu trả lời đơn giản. tìm kiếm đơn giản "Định dạng âm thanh được hỗ trợ" trong google mang lại http://developer.android.com/guide/appendix/media-formats.html. http://developer.android.com/guide/topics/media/mediaplayer.html – manjusg