我阅读了“Hello Android”一书并且我编写了播放声音的程序,但是当程序运行时,它不播放声音。程序在页面中显示“Hello World”。为什么它不播放声音??
Audio.java
package org.example.audio;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
public class Audio extends Activity{
private MediaPlayer up;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up=MediaPlayer.create(this,R.raw.temp);
}
@Override
public boolean onKeyDown(int keyCode,KeyEvent event){
MediaPlayer mp;
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_UP:
mp=up;
break;
default:
return super.onKeyDown(keyCode, event);
}
mp.seekTo(0);
mp.start();
return true;
}
}
我没有对程序的其他部分以及程序的其他文件进行任何更改。
我更改了程序的代码以使代码更容易,但它仍然没有播放声音。 我复制了程序日志。请阅读此内容并告诉我问题出在哪里:(
Audio.java
package org.example.audio;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
public class Audio extends Activity{
private MediaPlayer up;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up=MediaPlayer.create(this,R.raw.temp);
up.start();
}
}
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.audio"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
logcat的:
09-26 10:29:10.747: DEBUG/WifiLocator(8286): Too many no-location APs. Will not compute a location nor go to the server. hasLocation=0 noLocation=0 cacheMiss=0
09-26 10:29:10.747: DEBUG/CellLocator(8286): Primary cell miss in cache. Need server request.
09-26 10:29:10.755: INFO/System.out(8286): [INFO:88897555]: LogSource: Running flush
09-26 10:29:10.763: INFO/System.out(8286): [INFO:88897557]: LogSource: Sending payload [bytes=337]
09-26 10:29:10.763: INFO/System.out(8286): [SEVERE:88897563]: LogSource: AsyncHttpRequestImpl.run(): exception thrown : Unable to resolve host "www.google.com": No address associated with hostname: java.net.UnknownHostException
09-26 10:29:10.771: ERROR/GlsClient-query(8286): requestFailed
09-26 10:29:10.771: DEBUG/WifiLocator(8286): Too many no-location APs. Will not compute a location nor go to the server. hasLocation=0 noLocation=0 cacheMiss=0
09-26 10:29:10.771: DEBUG/CellLocator(8286): Primary cell miss in cache. Need server request.
09-26 10:29:11.388: DEBUG/AndroidRuntime(31054): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
09-26 10:29:11.388: DEBUG/AndroidRuntime(31054): CheckJNI is OFF
09-26 10:29:11.567: DEBUG/AndroidRuntime(31054): Calling main entry com.android.commands.pm.Pm
09-26 10:29:11.575: DEBUG/AndroidRuntime(31054): Shutting down VM
09-26 10:29:11.583: DEBUG/dalvikvm(31054): GC_CONCURRENT freed 101K, 89% free 463K/4096K, paused 1ms+0ms
09-26 10:29:11.583: DEBUG/jdwp(31054): Got wake-up signal, bailing out of select
09-26 10:29:11.583: DEBUG/dalvikvm(31054): Debugger has detached; object registry had 1 entries
09-26 10:29:11.927: DEBUG/AndroidRuntime(31067): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
09-26 10:29:11.927: DEBUG/AndroidRuntime(31067): CheckJNI is OFF
09-26 10:29:12.091: DEBUG/AndroidRuntime(31067): Calling main entry com.android.commands.am.Am
09-26 10:29:12.099: INFO/ActivityManager(177): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.example.audio/.MainActivity} from pid 31067
09-26 10:29:12.208: DEBUG/AndroidRuntime(31067): Shutting down VM
09-26 10:29:12.208: INFO/AndroidRuntime(31067): NOTE: attach of thread 'Binder Thread #3' failed
09-26 10:29:12.208: INFO/WindowManager(177): createSurface Window{42329bb8 org.example.audio/org.example.audio.MainActivity paused=false}: DRAW NOW PENDING
09-26 10:29:12.216: DEBUG/dalvikvm(31067): GC_CONCURRENT freed 102K, 89% free 484K/4096K, paused 1ms+1ms
09-26 10:29:12.216: DEBUG/jdwp(31067): Got wake-up signal, bailing out of select
09-26 10:29:12.216: DEBUG/dalvikvm(31067): Debugger has detached; object registry had 1 entries
09-26 10:29:12.294: DEBUG/OpenGLRenderer(438): Flushing caches (mode 0)
09-26 10:29:12.474: DEBUG/OpenGLRenderer(438): Flushing caches (mode 1)
09-26 10:29:30.927: DEBUG/dalvikvm(30908): GC_CONCURRENT freed 391K, 4% free 14072K/14599K, paused 6ms+1ms