码迷,mamicode.com
首页 > 移动开发 > 详细

android之MP3播放器

时间:2016-07-30 00:17:58      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

布局文件


布局文件中设置了三个按钮分别来进行播放、暂停和继续播放

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="开始播放"
    android:onClick="play"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="暂停播放"
    android:onClick="pause"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="继续播放"
    android:onClick="continuePlay"/>
</LinearLayout>

Activity


在Activity中开启服务, 在服务中实现MP3的播放功能,然后通过一个Binder对象将该功能暴露给Activity。则可以在Activity中调用Service中的方法。

package xidian.dy.com.chujia;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    IMusic music;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        bindService(intent, new MyConnection(), BIND_AUTO_CREATE);
    }

    public void play(View v){
        music.play();
    }

    public void pause(View v){
        music.pause();
    }
    public void continuePlay(View v){
        music.continuePlay();
    }

    class MyConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            music = (IMusic) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}

Service


在Service中创建一个MediaPlayer来控制音乐播放

package xidian.dy.com.chujia;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.IOException;

/**
 * Created by dy on 2016/7/29.
 */
public class MusicService extends Service {
    MediaPlayer player;

    @Override
    public void onCreate() {
        player = new MediaPlayer();
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public void continuePlay(){
        player.start();
    }

    public void pause(){
        player.pause();
    }

    public void play(){
        player.reset();
        try {
            player.setDataSource("/sdcard/bc.mp3");
            player.prepare();
            player.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        player.stop();
        player.release();
    }

    class MyBinder extends Binder implements IMusic{
        @Override
        public void play() {
            MusicService.this.play();
        }

        @Override
        public void pause() {
            MusicService.this.pause();

        }

        @Override
        public void continuePlay() {
            MusicService.this.continuePlay();
        }
    }
}

IMusic


IMusic是一个接口类,主要是为了能在Activity中调用service中的方法。

package xidian.dy.com.chujia;

/**
 * Created by dy on 2016/7/29.
 */
public interface IMusic {
    void play();
    void pause();
    void continuePlay();
}

清单文件


在清单文件中需要对服务进行注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.dy.com.chujia">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="主界面">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MusicService"/>
</application>
</manifest>

 

android之MP3播放器

标签:

原文地址:http://www.cnblogs.com/xidongyu/p/5719893.html

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