码迷,mamicode.com
首页 > 其他好文 > 详细

MP3播放器

时间:2015-01-23 23:04:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

学习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播放器

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/43063883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!