안 드 로 이 드 음악 재생 사례 공유
효과:
분석:
이전 글 의 구조 와 같 습 니 다.다만 우 리 는 여기에 음악 을 재생 하 는 조작 을 추가 해 야 합 니 다.
사실은 시스템 에서 음악 을 재생 하 는 API 를 호출 하 는 것 일 뿐 입 니 다.서비스 에 쓰 면 됩 니 다.
//
private MediaPlayer player;
public void onCreate() {
File file=new File(Environment.getExternalStorageDirectory(),"a.mp3");
player =new MediaPlayer();
try {
//
player.setDataSource(file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onCreate");
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
try {
// , , onPrepared
player.setOnPreparedListener(new OnPreparedListener() {
@Override
//
public void onPrepared(MediaPlayer arg0) {
//
player.start();
}
});
// ,
// ,
player.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
//
player.stop();
// , ,
player.release();
Log.d("fanfan", "onDestroy");
super.onDestroy();
}
첫 번 째 단 계 는 서비스 류 를 계승 할 종 류 를 찾 는 것 이다.
package fry;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
public class myService extends Service{
//
private MediaPlayer player;
/**
*
*/
@Override
public IBinder onBind(Intent arg0) {
Log.d("fanfan", "onBind");
return null;
}
/**
* service
*/
@Override
public void onCreate() {
File file=new File(Environment.getExternalStorageDirectory(),"a.mp3");
player =new MediaPlayer();
try {
//
player.setDataSource(file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onCreate");
super.onCreate();
}
/**
* service start
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
// , , onPrepared
player.setOnPreparedListener(new OnPreparedListener() {
@Override
//
public void onPrepared(MediaPlayer arg0) {
//
player.start();
}
});
// ,
// ,
player.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
/**
* service
*/
@Override
public void onDestroy() {
//
player.stop();
// , ,
player.release();
Log.d("fanfan", "onDestroy");
super.onDestroy();
}
}
두 번 째 단 계 는 이 설정 의 감청 서비스 도 설정 해 야 합 니 다.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playMusic"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="fry.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>
<activity android:name="fry.Activity01" android:exported="true"></activity>
<service android:name="fry.myService">
</service>
</application>
</manifest>
세 번 째,음악 재생 또는 종료
package fry;
import com.example.playMusic.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Activity01 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
}
public void onClick(View view){
Intent intent=new Intent();
intent.setClass(this, myService.class);
switch(view.getId()){
case R.id.btn_start:// ,
startService(intent);
break;
case R.id.btn_stop:// ,
stopService(intent);
break;
}
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.