首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android中播发mp3文件

2012-06-26 
Android中播放mp3文件在Service中播放,可以在后台进行播放?package cn.edu.designimport android.app.Act

Android中播放mp3文件

在Service中播放,可以在后台进行播放

?

package cn.edu.design;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;import cn.design.service.PlayService;public class BroadCastActivity extends Activity {private Button button=null;/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                button=(Button)findViewById(R.id.play);        button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setClass(BroadCastActivity.this, PlayService.class);startService(intent);}                });            }}

?

继承Service,播放Mp3文件

?

package cn.design.service;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.net.Uri;import android.os.Environment;import android.os.IBinder;public class PlayService extends Service{@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubMediaPlayer medPlay=MediaPlayer.create(this,Uri.parse("/sdcard/music/6.mp3"));medPlay.start();return super.onStartCommand(intent, flags, startId);}}

?

在AndroidManifest.xml中添加如下信息

?

<service android:name="cn.design.service.PlayService">

</service>

热点排行