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

测试播放器

时间:2017-05-25 01:23:08      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:ring   click   sch   return   ble   version   播放器   rate   service   

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mediaplayer"
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="com.example.mediaplayer.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="com.example.mediaplayer.MusicService">
</service>
</application>

</manifest>

===============================================================================

布局文件

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:text="我是测试播放器" />


<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="play"
android:text="播放了"/>

<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="暂停了"/>

</LinearLayout>

 

 

====================================================

Activity 页面

package com.example.mediaplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

private MusicServiceConn conn;
private IMusicService mService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent service = new Intent(this, MusicService.class);
startService(service);

conn = new MusicServiceConn();
bindService(service, conn, BIND_AUTO_CREATE);

}

// 连接通道
class MusicServiceConn implements ServiceConnection {

public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub

mService = (IMusicService) service;

}

public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

}

public void play(View v) {

mService.play();

}

public void stop(View v) {

mService.pause();

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

unbindService(conn);
}

}

============================================================

服务

package com.example.mediaplayer;

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

public class MusicService extends Service {

private MediaPlayer mediaPlayer;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new Music();
}

class Music extends Binder implements IMusicService {

public void play() {
// TODO Auto-generated method stub

playMusic();

}

public void pause() {
// TODO Auto-generated method stub
pauseMusic();
}

}

public void playMusic() {
// TODO Auto-generated method stub
//MediaPlayer player = new MediaPlayer();

mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.xin);
mediaPlayer.start();

}

public void pauseMusic() {
// TODO Auto-generated method stub

mediaPlayer.pause();
}

}

=============================

 

接口文件

 

package com.example.mediaplayer;

public interface IMusicService {
public void play();

public void pause();


}

 

测试播放器

标签:ring   click   sch   return   ble   version   播放器   rate   service   

原文地址:http://www.cnblogs.com/yangjies145/p/6901545.html

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