我在编写添加背景音乐的程序时,参考了这里的代码http://apps.hi.baidu.com/share/detail/18496230我的工程中有两个类,一个是music类,继承activity,另一个是musicServer类,集成service。music类如下:
package mx.music;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class music extends Activity {
/** Called when the activity is first created. */
private Intent intent = new Intent("mx.music.MUSIC");
private Button startButton;
private Button stopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.startButton);
stopButton = (Button) findViewById(R.id.stopButton);
startButton.setOnClickListener(new startListener());
stopButton.setOnClickListener(new stopListener());
}
class startListener implements OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
startService(intent);
}
}
class stopListener implements OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
stopService(intent);
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
musicserver类如下:
package mx.music;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;public class musicServer extends Service{ private MediaPlayer mp;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
if(mp==null){
mp=MediaPlayer.create(this, R.raw.mmp);
mp.start();/*显示这里空指针异常,但是我在上句都已经定义了啊,并且在raw中存在mmp的MP3文件*/
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.stop();
}
}
package mx.music;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class music extends Activity {
/** Called when the activity is first created. */
private Intent intent = new Intent("mx.music.MUSIC");
private Button startButton;
private Button stopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.startButton);
stopButton = (Button) findViewById(R.id.stopButton);
startButton.setOnClickListener(new startListener());
stopButton.setOnClickListener(new stopListener());
}
class startListener implements OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
startService(intent);
}
}
class stopListener implements OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
stopService(intent);
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
musicserver类如下:
package mx.music;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;public class musicServer extends Service{ private MediaPlayer mp;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
if(mp==null){
mp=MediaPlayer.create(this, R.raw.mmp);
mp.start();/*显示这里空指针异常,但是我在上句都已经定义了啊,并且在raw中存在mmp的MP3文件*/
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.stop();
}
}
解决方案 »
- 再开一帖,继续给大家送分
- android推送 androidpn 服务器端运行不起来
- 求指点:android程序通过网络打开自己电脑摄像头实现访问功能
- 关于使用Socket向PC机发送图片的问题!
- 模拟器上试了试蜂巢 结果崩溃。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 怎么样在ContentResolver中把扫描Video和Audio文件的语句结合为一句话?
- 用过MQTT+APOLLO1.7的求助
- 关于WIFI自动连接
- 怎么更换widget中的图片
- termux打印心形
- android:background="@drawable/border_view_finder"
- android是一个怎么样的学习过程
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mx.music"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".music"
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=".musicServer">
<intent-filter>
<action android:name="mx.music.MUSIC"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
</application>
</manifest>
mp=MediaPlayer.create(this, R.raw.mmp);
MediaPlayer我用的时候也在prepare那报错,不过不是空指针
我后来这样搞定的
FileInputStream audioFis = null;
try {
audioFis = new FileInputStream(filepath);
mp.setDataSource(audioFis.getFD()); mp.prepare();
mp.start();
}
Returns
a MediaPlayer object, or null if creation failed
看来是creation failed