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

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
    评论
创建一个基于Android Studio的音乐播放器应用通常涉及多个步骤和关键组件。以下是一个简要概述: 1. **项目设置**: - 在Android Studio中,选择"Start a new Android Studio project",选择"Empty Activity"模板,命名你的项目并选择目标Android API。 2. **界面设计**: - 使用XML布局文件(如activity_main.xml)设计主界面,包括一个RecyclerView显示歌曲列表、一个歌词滚动视图、以及播放/暂停按钮等。 3. **数据结构**: - 创建一个Song类,包含属性如title, artist, URI等,用于存储歌曲信息。 4. **音乐播放库**: - 选择一个音乐播放库,如ExoPlayer或Android内置的MediaPlayer。ExoPlayer更强大且易于集成。 5. **播放功能实现**: - 设置播放服务(Service)并实现播放、暂停、上一曲、下一曲、音量控制等功能。 - 当用户点击列表中的歌曲时,从网络或本地加载Song实例,并调用播放方法。 6. **歌曲列表管理**: - 使用SQLite数据库或网络API获取歌曲列表,并将其添加到RecyclerView中。 7. **歌词同步**: - 可能需要使用第三方库或API来获取歌词,然后与音乐播放同步显示。 8. **用户界面状态管理**: - 使用ViewModel或Repository模式来管理UI与数据之间的交互,保持状态的统一。 9. **资源管理**: - 确保处理好音频文件的加载、缓存和解码,防止内存泄漏。 10. **错误处理与调试**: - 添加适当的错误处理代码,如网络请求失败、文件读取错误等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值