《Google Android开发入门与实战》第十章,音乐播放器例子,没有语法错误,能运行,但没有任何反映,请大家帮忙看看哪里出了问题?
关键代码如下:
PlayService.java
public class PlayService extends Activity {
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button1 = (Button)findViewById(R.id.start);
        button1.setOnClickListener(startIt);
         }
      private OnClickListener startIt = new OnClickListener()
    {
        public void onClick(View v)
        {           
            startService(new Intent("com.iceskysl.PlayService.START_AUDIO_SERVICE"));
        }
    };Music.java
public class Music extends Service {
    private MediaPlayer player;
        public IBinder onBind(Intent intent) {
                return null;
        }
        public void onStart(Intent intent, int startId) {       
        super.onStart(intent, startId);
        player = MediaPlayer.create(this, R.raw.gequ);
        player.start();
    }
   public void onDestroy() {
        super.onDestroy();
        player.stop();
    }
}
*.xml:
<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PlayService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
                <Service android:name=".Music">
                        <intent-filter>
                                <action android:name="com.iceskysl.PlayService.START_AUDIO_SERVICE"/>
                                <category android:name="Android.intent.category.default"/>
                        </intent-filter>
                </Service>
    </application>gequ.mp3文件放在res/raw目录下。初学,还请多多指教啊。

解决方案 »

  1.   

    换成
    startService(new Intent(this, Music.this));
    试试
      

  2.   

    试了,不行啊。换成startService(new Intent(PlayService.this,Music.class));也不行。
      

  3.   

    ComponentName component = new ComponentName(this, Music.class);
    Intent intent = new Intent();
    intent.setComponent(component);
    startService(intent);
      

  4.   

    Manifest.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.hb"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".PlayService"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    <Service android:name=".Music">
    <intent-filter>
    <action android:name="com.iceskysl.PlayService.START_AUDIO_SERVICE"/>
    <category android:name="Android.intent.category.default"/>
    </intent-filter>
    </Service>
        </application>
    </manifest> 
      

  5.   

    LogCat信息如下:
    11-30 21:37:59.885: ERROR/MediaPlayer(584): Unable to to create media player
    11-30 21:37:59.896: WARN/AudioService(584): MediaPlayer IOException: java.io.IOException: setDataSource failed.: status=0x80000000
    11-30 21:37:59.935: WARN/ActivityManager(584): Unable to start service Intent { comp={com.hb/com.hb.Music} }: not found
      

  6.   

    模拟器自带的Music能播放音乐。
      

  7.   

    这里有个融合android自带的音乐播放器,还显示播放列表。
    http://download.csdn.net/source/2872751
      

  8.   

    工程所在的包名是com.iceskysl?