我在 3 天前开始为 Android 编程,今天我想使用 android Api 中的一些类来做一些更困难的事情。我找到了 Visualizer 类,起初我遇到了问题。我在不同论坛上阅读了许多有相同问题的人的帖子:无法初始化可视化引擎。
我将需要使用权限添加到 Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.program.fourier"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".mainFFT"
android:label="@string/title_activity_main_fft" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
接下来我试图解决这个问题,但我不能。这是我的完整代码:
package org.program.fourier;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.media.audiofx.Visualizer;
import android.media.audiofx.Visualizer.OnDataCaptureListener;
import android.media.MediaPlayer;
import android.media.AudioManager;
public class mainFFT extends Activity {
MediaPlayer mPlayer;
Visualizer vis;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fft);
mPlayer = MediaPlayer.create(this, R.raw.sight);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try
{
mPlayer.prepare();
}
catch(Exception ex){ Log.w("ExCePtIoN", ex+""); }
try
{
vis = new Visualizer(mPlayer.getAudioSessionId());
//vis.setDataCaptureListener(this, 20, true, true);
}
catch(Exception ex){ Log.w("ExCePtIoN", ex+""); }
mPlayer.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_fft, menu);
return true;
}
}
最后一个问题是当我想执行 mPlayer.prepare() 方法时它会抛出 IllegalStateException 。
这是完整的 LogCat 消息:
07-06 18:33:17.141: E/Trace(833): error opening trace file: No such file or directory (2)
07-06 18:33:17.591: E/MediaPlayer(833): prepareAsync called in state 8
07-06 18:33:17.591: W/ExCePtIoN(833): java.lang.IllegalStateException
07-06 18:33:17.621: E/AudioEffect(833): set(): AudioFlinger could not create effect, status: -22
07-06 18:33:17.621: E/visualizers-JNI(833): Visualizer initCheck failed -4
07-06 18:33:17.621: E/Visualizer-JAVA(833): Error code -4 when initializing Visualizer.
07-06 18:33:17.621: W/ExCePtIoN(833): java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4
07-06 18:33:18.482: I/Choreographer(833): Skipped 337 frames! The application may be doing too much work on its main thread.
07-06 18:33:18.551: D/gralloc_goldfish(833): Emulator without GPU emulation detected.