安卓基于本地服务,播放音乐

src下放入音乐文件夹raw

1AudioService.java

package edu.smxy.ymw.finalexam_3_20150861213;

 

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.IBinder;

import android.support.annotation.Nullable;

 

/**

 * Created by Administrator on 2018/6/25.

 */

 

public class AudioService extends Service {

    private MediaPlayer mediaPlayer;

    @Nullable

    @Override

    public IBinder onBind(Intent intent) {

        return null;

    }

 

    @Override

    public void onCreate() {

        super.onCreate();

        this.mediaPlayer=MediaPlayer.create(this,R.raw.music);

        this.mediaPlayer.start();

    }

 

    @Override

    public void onDestroy() {

        super.onDestroy();

        this.mediaPlayer.stop();

        this.mediaPlayer.release();

    }

}

 

2 MainActivity.java

package edu.smxy.ymw.finalexam_3_20150861213;

 

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {

    String strServiceName="edu.smxy.ymw.finalexam_3_20150861213.AudioService";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button btnPlay=(Button)findViewById(R.id.btnPlay);

        Button btnStop=(Button)findViewById(R.id.btnStop);

        btnPlay.setOnClickListener(this);

        btnStop.setOnClickListener(this);

    }

 

    @Override

    public void onClick(View view) {

        switch (view.getId()){

            case R.id.btnPlay:

                startService(new Intent(strServiceName));

                break;

            case R.id.btnStop:

                stopService(new Intent(strServiceName));

                break;

            default:

                break;

        }

    }

}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="edu.smxy.ymw.finalexam_3_20150861213.MainActivity">

 

   <Button

       android:id="@+id/btnPlay"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:text="播放"

       />

    <Button

        android:layout_below="@id/btnPlay"

        android:id="@+id/btnStop"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="停止"

        />

 

</RelativeLayout>

 

 

AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="edu.smxy.ymw.finalexam_3_20150861213">

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <service android:name=".AudioService">

            <intent-filter>

                <action android:name="edu.smxy.ymw.finalexam_3_20150861213.AudioService"></action>

            </intent-filter>

        </service>

    </application>

 

</manifest>

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值