首页 > 代码库 > MP3播放器
MP3播放器
学习service的典型例子
在res下建立raw文件夹存放mp3资源
package com.example.mp3player; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MusicService extends Service{ MediaPlayer player = new MediaPlayer(); @Override public IBinder onBind(Intent arg0) { return null; } public void onStart(Intent intent, int startId){ super.onStart(intent, startId); player = MediaPlayer.create(this, R.raw.audio); player.start(); } public void onDestroy(){ super.onDestroy(); player.stop(); } }
package com.example.mp3player; 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 MainActivity extends Activity { Button start = null; Button stop = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.button1); stop = (Button) findViewById(R.id.button2); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub startService(new Intent("com.example.mp3player.MusicService")); } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub stopService(new Intent("com.example.mp3player.MusicService")); } }); } }
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.mp3player.MainActivity" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_marginBottom="36dp" android:text="开始播放" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="停止播放" /> </RelativeLayout>
页面非常简单
要在模拟器听到声音,一定要确保电脑声卡没问题
在AndroidMainfest.xml里定义service
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mp3player" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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=".MusicService"> <intent-filter> <category android:name="android.intent.category.DEFAULT"/> <action android:name="com.example.mp3player.MusicService"/> </intent-filter> </service> </application> </manifest>
源代码下载
MP3播放器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。