Android学习笔记——播放音乐
2015-08-05 14:16
375 查看
界面展示需要两个按钮,一个按钮为开始播放,一个为停止播放:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/startplay"/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/stopplay"
/>
</LinearLayout>String.xml:
<resources>
<string name="app_name">PlayMusicService</string>
<string name="hello">PlayMusicService Activity</string>
<string name="startplay">开始播放</string>
<string name="stopplay">停止播放</string>
</resources>
AndroidManifest.xml中需要定一个Activity以及一个播放音乐的Service。
这是后我们看R.java中已经将资源的目录配置好了:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.playmusicservice;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int start=0x7f070000;
public static final int stop=0x7f070001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class raw {
public static final int test=0x7f040000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello=0x7f050001;
public static final int startplay=0x7f050002;
public static final int stopplay=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
这时候我们增加播放音乐的服务类:
package com.playmusicservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service{
//MediaPlayer对象
private MediaPlayer player;
@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.test);
//开始播放
player.start();
}
public void onDestroy(){
super.onDestroy();
//停止音乐-----停止service
player.stop();
}
}
主界面可以这样写:
package com.playmusicservice;
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 PlayMusicActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
}
private OnClickListener start = new OnClickListener(){
@Override
public void onClick(View arg0) {
startService(new Intent("com.playmusicservice.MUSIC"));
}
};
private OnClickListener stop = new OnClickListener(){
@Override
public void onClick(View arg0) {
stopService(new Intent("com.playmusicservice.MUSIC"));
}
};
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/startplay"/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/stopplay"
/>
</LinearLayout>String.xml:
<resources>
<string name="app_name">PlayMusicService</string>
<string name="hello">PlayMusicService Activity</string>
<string name="startplay">开始播放</string>
<string name="stopplay">停止播放</string>
</resources>
AndroidManifest.xml中需要定一个Activity以及一个播放音乐的Service。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.playmusicservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="PlayMusicActivity" 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> <action android:name="com.playmusicservice.MUSIC" /> <category android:name="android.intent.category.default" /> </intent-filter> </service> </application> </manifest>res目录下增加需要播放的音乐资源:
这是后我们看R.java中已经将资源的目录配置好了:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.playmusicservice;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int start=0x7f070000;
public static final int stop=0x7f070001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class raw {
public static final int test=0x7f040000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello=0x7f050001;
public static final int startplay=0x7f050002;
public static final int stopplay=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
这时候我们增加播放音乐的服务类:
package com.playmusicservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service{
//MediaPlayer对象
private MediaPlayer player;
@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.test);
//开始播放
player.start();
}
public void onDestroy(){
super.onDestroy();
//停止音乐-----停止service
player.stop();
}
}
主界面可以这样写:
package com.playmusicservice;
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 PlayMusicActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
}
private OnClickListener start = new OnClickListener(){
@Override
public void onClick(View arg0) {
startService(new Intent("com.playmusicservice.MUSIC"));
}
};
private OnClickListener stop = new OnClickListener(){
@Override
public void onClick(View arg0) {
stopService(new Intent("com.playmusicservice.MUSIC"));
}
};
}
相关文章推荐
- Android实现表情 抓取新浪表情
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- android开发环境遇到adt无法启动的问题分析及解决方法
- Android开发 旋转屏幕导致Activity重建解决方法
- Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)
- Android开发技巧之ViewStub控件惰性装载
- 分享10个很棒的学习Android开发的网站
- android开发之横向滚动/竖向滚动的ListView(固定列头)
- Android开发技巧之我的菜单我做主(自定义菜单)
- Windows下使用Apache Cordova开发ANDROID之HelloWorld
- 根据根据图片的url怎么取得图片ImageView对象
- [转] ADB server didn't ACK
- android开发的相关链接
- Android数据存储之SharedPreferences 共享首选项/偏好设置
- 基于NDK的Android防破解
- 麦子学院android开发笔记:Android更换RatingBar图片
- android开发入门知识之AIDL详解
- android开发学习如何一体化?从入门到高端开发的路如何走?
- Android入门到精通详解 电子书下载
- --暴走漫画系列--高仿淘宝收货地址(附demo)